1 //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===// 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 is the internal per-translation-unit state used for llvm translation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef CLANG_CODEGEN_CODEGENMODULE_H 15 #define CLANG_CODEGEN_CODEGENMODULE_H 16 17 #include "clang/Basic/ABI.h" 18 #include "clang/Basic/LangOptions.h" 19 #include "clang/AST/Attr.h" 20 #include "clang/AST/DeclCXX.h" 21 #include "clang/AST/DeclObjC.h" 22 #include "clang/AST/GlobalDecl.h" 23 #include "clang/AST/Mangle.h" 24 #include "CGVTables.h" 25 #include "CodeGenTypes.h" 26 #include "llvm/Module.h" 27 #include "llvm/ADT/DenseMap.h" 28 #include "llvm/ADT/StringMap.h" 29 #include "llvm/ADT/StringSet.h" 30 #include "llvm/ADT/SmallPtrSet.h" 31 #include "llvm/Support/ValueHandle.h" 32 33 namespace llvm { 34 class Module; 35 class Constant; 36 class ConstantInt; 37 class Function; 38 class GlobalValue; 39 class TargetData; 40 class FunctionType; 41 class LLVMContext; 42 } 43 44 namespace clang { 45 class TargetCodeGenInfo; 46 class ASTContext; 47 class FunctionDecl; 48 class IdentifierInfo; 49 class ObjCMethodDecl; 50 class ObjCImplementationDecl; 51 class ObjCCategoryImplDecl; 52 class ObjCProtocolDecl; 53 class ObjCEncodeExpr; 54 class BlockExpr; 55 class CharUnits; 56 class Decl; 57 class Expr; 58 class Stmt; 59 class StringLiteral; 60 class NamedDecl; 61 class ValueDecl; 62 class VarDecl; 63 class LangOptions; 64 class CodeGenOptions; 65 class Diagnostic; 66 class AnnotateAttr; 67 class CXXDestructorDecl; 68 class MangleBuffer; 69 70 namespace CodeGen { 71 72 class CallArgList; 73 class CodeGenFunction; 74 class CodeGenTBAA; 75 class CGCXXABI; 76 class CGDebugInfo; 77 class CGObjCRuntime; 78 class BlockFieldFlags; 79 class FunctionArgList; 80 81 struct OrderGlobalInits { 82 unsigned int priority; 83 unsigned int lex_order; 84 OrderGlobalInits(unsigned int p, unsigned int l) 85 : priority(p), lex_order(l) {} 86 87 bool operator==(const OrderGlobalInits &RHS) const { 88 return priority == RHS.priority && 89 lex_order == RHS.lex_order; 90 } 91 92 bool operator<(const OrderGlobalInits &RHS) const { 93 if (priority < RHS.priority) 94 return true; 95 96 return priority == RHS.priority && lex_order < RHS.lex_order; 97 } 98 }; 99 100 struct CodeGenTypeCache { 101 /// void 102 llvm::Type *VoidTy; 103 104 /// i8, i32, and i64 105 llvm::IntegerType *Int8Ty, *Int32Ty, *Int64Ty; 106 107 /// int 108 llvm::IntegerType *IntTy; 109 110 /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size. 111 union { 112 llvm::IntegerType *IntPtrTy; 113 llvm::IntegerType *SizeTy; 114 llvm::IntegerType *PtrDiffTy; 115 }; 116 117 /// void* in address space 0 118 union { 119 llvm::PointerType *VoidPtrTy; 120 llvm::PointerType *Int8PtrTy; 121 }; 122 123 /// void** in address space 0 124 union { 125 llvm::PointerType *VoidPtrPtrTy; 126 llvm::PointerType *Int8PtrPtrTy; 127 }; 128 129 /// The width of a pointer into the generic address space. 130 unsigned char PointerWidthInBits; 131 132 /// The alignment of a pointer into the generic address space. 133 unsigned char PointerAlignInBytes; 134 }; 135 136 struct RREntrypoints { 137 RREntrypoints() { memset(this, 0, sizeof(*this)); } 138 /// void objc_autoreleasePoolPop(void*); 139 llvm::Constant *objc_autoreleasePoolPop; 140 141 /// void *objc_autoreleasePoolPush(void); 142 llvm::Constant *objc_autoreleasePoolPush; 143 }; 144 145 struct ARCEntrypoints { 146 ARCEntrypoints() { memset(this, 0, sizeof(*this)); } 147 148 /// id objc_autorelease(id); 149 llvm::Constant *objc_autorelease; 150 151 /// id objc_autoreleaseReturnValue(id); 152 llvm::Constant *objc_autoreleaseReturnValue; 153 154 /// void objc_copyWeak(id *dest, id *src); 155 llvm::Constant *objc_copyWeak; 156 157 /// void objc_destroyWeak(id*); 158 llvm::Constant *objc_destroyWeak; 159 160 /// id objc_initWeak(id*, id); 161 llvm::Constant *objc_initWeak; 162 163 /// id objc_loadWeak(id*); 164 llvm::Constant *objc_loadWeak; 165 166 /// id objc_loadWeakRetained(id*); 167 llvm::Constant *objc_loadWeakRetained; 168 169 /// void objc_moveWeak(id *dest, id *src); 170 llvm::Constant *objc_moveWeak; 171 172 /// id objc_retain(id); 173 llvm::Constant *objc_retain; 174 175 /// id objc_retainAutorelease(id); 176 llvm::Constant *objc_retainAutorelease; 177 178 /// id objc_retainAutoreleaseReturnValue(id); 179 llvm::Constant *objc_retainAutoreleaseReturnValue; 180 181 /// id objc_retainAutoreleasedReturnValue(id); 182 llvm::Constant *objc_retainAutoreleasedReturnValue; 183 184 /// id objc_retainBlock(id); 185 llvm::Constant *objc_retainBlock; 186 187 /// void objc_release(id); 188 llvm::Constant *objc_release; 189 190 /// id objc_storeStrong(id*, id); 191 llvm::Constant *objc_storeStrong; 192 193 /// id objc_storeWeak(id*, id); 194 llvm::Constant *objc_storeWeak; 195 196 /// A void(void) inline asm to use to mark that the return value of 197 /// a call will be immediately retain. 198 llvm::InlineAsm *retainAutoreleasedReturnValueMarker; 199 }; 200 201 /// CodeGenModule - This class organizes the cross-function state that is used 202 /// while generating LLVM code. 203 class CodeGenModule : public CodeGenTypeCache { 204 CodeGenModule(const CodeGenModule&); // DO NOT IMPLEMENT 205 void operator=(const CodeGenModule&); // DO NOT IMPLEMENT 206 207 typedef std::vector<std::pair<llvm::Constant*, int> > CtorList; 208 209 ASTContext &Context; 210 const LangOptions &Features; 211 const CodeGenOptions &CodeGenOpts; 212 llvm::Module &TheModule; 213 const llvm::TargetData &TheTargetData; 214 mutable const TargetCodeGenInfo *TheTargetCodeGenInfo; 215 Diagnostic &Diags; 216 CGCXXABI &ABI; 217 CodeGenTypes Types; 218 CodeGenTBAA *TBAA; 219 220 /// VTables - Holds information about C++ vtables. 221 CodeGenVTables VTables; 222 friend class CodeGenVTables; 223 224 CGObjCRuntime* Runtime; 225 CGDebugInfo* DebugInfo; 226 ARCEntrypoints *ARCData; 227 RREntrypoints *RRData; 228 229 // WeakRefReferences - A set of references that have only been seen via 230 // a weakref so far. This is used to remove the weak of the reference if we ever 231 // see a direct reference or a definition. 232 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences; 233 234 /// DeferredDecls - This contains all the decls which have definitions but 235 /// which are deferred for emission and therefore should only be output if 236 /// they are actually used. If a decl is in this, then it is known to have 237 /// not been referenced yet. 238 llvm::StringMap<GlobalDecl> DeferredDecls; 239 240 /// DeferredDeclsToEmit - This is a list of deferred decls which we have seen 241 /// that *are* actually referenced. These get code generated when the module 242 /// is done. 243 std::vector<GlobalDecl> DeferredDeclsToEmit; 244 245 /// LLVMUsed - List of global values which are required to be 246 /// present in the object file; bitcast to i8*. This is used for 247 /// forcing visibility of symbols which may otherwise be optimized 248 /// out. 249 std::vector<llvm::WeakVH> LLVMUsed; 250 251 /// GlobalCtors - Store the list of global constructors and their respective 252 /// priorities to be emitted when the translation unit is complete. 253 CtorList GlobalCtors; 254 255 /// GlobalDtors - Store the list of global destructors and their respective 256 /// priorities to be emitted when the translation unit is complete. 257 CtorList GlobalDtors; 258 259 /// MangledDeclNames - A map of canonical GlobalDecls to their mangled names. 260 llvm::DenseMap<GlobalDecl, llvm::StringRef> MangledDeclNames; 261 llvm::BumpPtrAllocator MangledNamesAllocator; 262 263 std::vector<llvm::Constant*> Annotations; 264 265 llvm::StringMap<llvm::Constant*> CFConstantStringMap; 266 llvm::StringMap<llvm::Constant*> ConstantStringMap; 267 llvm::DenseMap<const Decl*, llvm::Value*> StaticLocalDeclMap; 268 269 /// CXXGlobalInits - Global variables with initializers that need to run 270 /// before main. 271 std::vector<llvm::Constant*> CXXGlobalInits; 272 273 /// When a C++ decl with an initializer is deferred, null is 274 /// appended to CXXGlobalInits, and the index of that null is placed 275 /// here so that the initializer will be performed in the correct 276 /// order. 277 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition; 278 279 /// - Global variables with initializers whose order of initialization 280 /// is set by init_priority attribute. 281 282 llvm::SmallVector<std::pair<OrderGlobalInits, llvm::Function*>, 8> 283 PrioritizedCXXGlobalInits; 284 285 /// CXXGlobalDtors - Global destructor functions and arguments that need to 286 /// run on termination. 287 std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors; 288 289 /// CFConstantStringClassRef - Cached reference to the class for constant 290 /// strings. This value has type int * but is actually an Obj-C class pointer. 291 llvm::Constant *CFConstantStringClassRef; 292 293 /// ConstantStringClassRef - Cached reference to the class for constant 294 /// strings. This value has type int * but is actually an Obj-C class pointer. 295 llvm::Constant *ConstantStringClassRef; 296 297 /// Lazily create the Objective-C runtime 298 void createObjCRuntime(); 299 300 llvm::LLVMContext &VMContext; 301 302 /// @name Cache for Blocks Runtime Globals 303 /// @{ 304 305 const VarDecl *NSConcreteGlobalBlockDecl; 306 const VarDecl *NSConcreteStackBlockDecl; 307 llvm::Constant *NSConcreteGlobalBlock; 308 llvm::Constant *NSConcreteStackBlock; 309 310 const FunctionDecl *BlockObjectAssignDecl; 311 const FunctionDecl *BlockObjectDisposeDecl; 312 llvm::Constant *BlockObjectAssign; 313 llvm::Constant *BlockObjectDispose; 314 315 llvm::Type *BlockDescriptorType; 316 llvm::Type *GenericBlockLiteralType; 317 318 struct { 319 int GlobalUniqueCount; 320 } Block; 321 322 /// @} 323 public: 324 CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts, 325 llvm::Module &M, const llvm::TargetData &TD, Diagnostic &Diags); 326 327 ~CodeGenModule(); 328 329 /// Release - Finalize LLVM code generation. 330 void Release(); 331 332 /// getObjCRuntime() - Return a reference to the configured 333 /// Objective-C runtime. 334 CGObjCRuntime &getObjCRuntime() { 335 if (!Runtime) createObjCRuntime(); 336 return *Runtime; 337 } 338 339 /// hasObjCRuntime() - Return true iff an Objective-C runtime has 340 /// been configured. 341 bool hasObjCRuntime() { return !!Runtime; } 342 343 /// getCXXABI() - Return a reference to the configured C++ ABI. 344 CGCXXABI &getCXXABI() { return ABI; } 345 346 ARCEntrypoints &getARCEntrypoints() const { 347 assert(getLangOptions().ObjCAutoRefCount && ARCData != 0); 348 return *ARCData; 349 } 350 351 RREntrypoints &getRREntrypoints() const { 352 assert(RRData != 0); 353 return *RRData; 354 } 355 356 llvm::Value *getStaticLocalDeclAddress(const VarDecl *VD) { 357 return StaticLocalDeclMap[VD]; 358 } 359 void setStaticLocalDeclAddress(const VarDecl *D, 360 llvm::GlobalVariable *GV) { 361 StaticLocalDeclMap[D] = GV; 362 } 363 364 CGDebugInfo *getModuleDebugInfo() { return DebugInfo; } 365 366 ASTContext &getContext() const { return Context; } 367 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } 368 const LangOptions &getLangOptions() const { return Features; } 369 llvm::Module &getModule() const { return TheModule; } 370 CodeGenTypes &getTypes() { return Types; } 371 CodeGenVTables &getVTables() { return VTables; } 372 Diagnostic &getDiags() const { return Diags; } 373 const llvm::TargetData &getTargetData() const { return TheTargetData; } 374 const TargetInfo &getTarget() const { return Context.Target; } 375 llvm::LLVMContext &getLLVMContext() { return VMContext; } 376 const TargetCodeGenInfo &getTargetCodeGenInfo(); 377 bool isTargetDarwin() const; 378 379 bool shouldUseTBAA() const { return TBAA != 0; } 380 381 llvm::MDNode *getTBAAInfo(QualType QTy); 382 383 static void DecorateInstruction(llvm::Instruction *Inst, 384 llvm::MDNode *TBAAInfo); 385 386 /// getSize - Emit the given number of characters as a value of type size_t. 387 llvm::ConstantInt *getSize(CharUnits numChars); 388 389 /// setGlobalVisibility - Set the visibility for the given LLVM 390 /// GlobalValue. 391 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const; 392 393 /// TypeVisibilityKind - The kind of global variable that is passed to 394 /// setTypeVisibility 395 enum TypeVisibilityKind { 396 TVK_ForVTT, 397 TVK_ForVTable, 398 TVK_ForConstructionVTable, 399 TVK_ForRTTI, 400 TVK_ForRTTIName 401 }; 402 403 /// setTypeVisibility - Set the visibility for the given global 404 /// value which holds information about a type. 405 void setTypeVisibility(llvm::GlobalValue *GV, const CXXRecordDecl *D, 406 TypeVisibilityKind TVK) const; 407 408 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) { 409 switch (V) { 410 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility; 411 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility; 412 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility; 413 } 414 llvm_unreachable("unknown visibility!"); 415 return llvm::GlobalValue::DefaultVisibility; 416 } 417 418 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) { 419 if (isa<CXXConstructorDecl>(GD.getDecl())) 420 return GetAddrOfCXXConstructor(cast<CXXConstructorDecl>(GD.getDecl()), 421 GD.getCtorType()); 422 else if (isa<CXXDestructorDecl>(GD.getDecl())) 423 return GetAddrOfCXXDestructor(cast<CXXDestructorDecl>(GD.getDecl()), 424 GD.getDtorType()); 425 else if (isa<FunctionDecl>(GD.getDecl())) 426 return GetAddrOfFunction(GD); 427 else 428 return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl())); 429 } 430 431 /// CreateOrReplaceCXXRuntimeVariable - Will return a global variable of the given 432 /// type. If a variable with a different type already exists then a new 433 /// variable with the right type will be created and all uses of the old 434 /// variable will be replaced with a bitcast to the new variable. 435 llvm::GlobalVariable * 436 CreateOrReplaceCXXRuntimeVariable(llvm::StringRef Name, llvm::Type *Ty, 437 llvm::GlobalValue::LinkageTypes Linkage); 438 439 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 440 /// given global variable. If Ty is non-null and if the global doesn't exist, 441 /// then it will be greated with the specified type instead of whatever the 442 /// normal requested type would be. 443 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, 444 llvm::Type *Ty = 0); 445 446 447 /// GetAddrOfFunction - Return the address of the given function. If Ty is 448 /// non-null, then this function will use the specified type if it has to 449 /// create it. 450 llvm::Constant *GetAddrOfFunction(GlobalDecl GD, 451 llvm::Type *Ty = 0, 452 bool ForVTable = false); 453 454 /// GetAddrOfRTTIDescriptor - Get the address of the RTTI descriptor 455 /// for the given type. 456 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false); 457 458 /// GetAddrOfThunk - Get the address of the thunk for the given global decl. 459 llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk); 460 461 /// GetWeakRefReference - Get a reference to the target of VD. 462 llvm::Constant *GetWeakRefReference(const ValueDecl *VD); 463 464 /// GetNonVirtualBaseClassOffset - Returns the offset from a derived class to 465 /// a class. Returns null if the offset is 0. 466 llvm::Constant * 467 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, 468 CastExpr::path_const_iterator PathBegin, 469 CastExpr::path_const_iterator PathEnd); 470 471 /// A pair of helper functions for a __block variable. 472 class ByrefHelpers : public llvm::FoldingSetNode { 473 public: 474 llvm::Constant *CopyHelper; 475 llvm::Constant *DisposeHelper; 476 477 /// The alignment of the field. This is important because 478 /// different offsets to the field within the byref struct need to 479 /// have different helper functions. 480 CharUnits Alignment; 481 482 ByrefHelpers(CharUnits alignment) : Alignment(alignment) {} 483 virtual ~ByrefHelpers(); 484 485 void Profile(llvm::FoldingSetNodeID &id) const { 486 id.AddInteger(Alignment.getQuantity()); 487 profileImpl(id); 488 } 489 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0; 490 491 virtual bool needsCopy() const { return true; } 492 virtual void emitCopy(CodeGenFunction &CGF, 493 llvm::Value *dest, llvm::Value *src) = 0; 494 495 virtual bool needsDispose() const { return true; } 496 virtual void emitDispose(CodeGenFunction &CGF, llvm::Value *field) = 0; 497 }; 498 499 llvm::FoldingSet<ByrefHelpers> ByrefHelpersCache; 500 501 /// getUniqueBlockCount - Fetches the global unique block count. 502 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; } 503 504 /// getBlockDescriptorType - Fetches the type of a generic block 505 /// descriptor. 506 llvm::Type *getBlockDescriptorType(); 507 508 /// getGenericBlockLiteralType - The type of a generic block literal. 509 llvm::Type *getGenericBlockLiteralType(); 510 511 /// GetAddrOfGlobalBlock - Gets the address of a block which 512 /// requires no captures. 513 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *); 514 515 /// GetStringForStringLiteral - Return the appropriate bytes for a string 516 /// literal, properly padded to match the literal type. If only the address of 517 /// a constant is needed consider using GetAddrOfConstantStringLiteral. 518 std::string GetStringForStringLiteral(const StringLiteral *E); 519 520 /// GetAddrOfConstantCFString - Return a pointer to a constant CFString object 521 /// for the given string. 522 llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal); 523 524 /// GetAddrOfConstantString - Return a pointer to a constant NSString object 525 /// for the given string. Or a user defined String object as defined via 526 /// -fconstant-string-class=class_name option. 527 llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal); 528 529 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a constant array 530 /// for the given string literal. 531 llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S); 532 533 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 534 /// array for the given ObjCEncodeExpr node. 535 llvm::Constant *GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *); 536 537 /// GetAddrOfConstantString - Returns a pointer to a character array 538 /// containing the literal. This contents are exactly that of the given 539 /// string, i.e. it will not be null terminated automatically; see 540 /// GetAddrOfConstantCString. Note that whether the result is actually a 541 /// pointer to an LLVM constant depends on Feature.WriteableStrings. 542 /// 543 /// The result has pointer to array type. 544 /// 545 /// \param GlobalName If provided, the name to use for the global 546 /// (if one is created). 547 llvm::Constant *GetAddrOfConstantString(llvm::StringRef Str, 548 const char *GlobalName=0); 549 550 /// GetAddrOfConstantCString - Returns a pointer to a character array 551 /// containing the literal and a terminating '\0' character. The result has 552 /// pointer to array type. 553 /// 554 /// \param GlobalName If provided, the name to use for the global (if one is 555 /// created). 556 llvm::Constant *GetAddrOfConstantCString(const std::string &str, 557 const char *GlobalName=0); 558 559 /// GetAddrOfCXXConstructor - Return the address of the constructor of the 560 /// given type. 561 llvm::GlobalValue *GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor, 562 CXXCtorType ctorType, 563 const CGFunctionInfo *fnInfo = 0); 564 565 /// GetAddrOfCXXDestructor - Return the address of the constructor of the 566 /// given type. 567 llvm::GlobalValue *GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor, 568 CXXDtorType dtorType, 569 const CGFunctionInfo *fnInfo = 0); 570 571 /// getBuiltinLibFunction - Given a builtin id for a function like 572 /// "__builtin_fabsf", return a Function* for "fabsf". 573 llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD, 574 unsigned BuiltinID); 575 576 llvm::Function *getIntrinsic(unsigned IID, llvm::ArrayRef<llvm::Type*> Tys = 577 llvm::ArrayRef<llvm::Type*>()); 578 579 /// EmitTopLevelDecl - Emit code for a single top level declaration. 580 void EmitTopLevelDecl(Decl *D); 581 582 /// AddUsedGlobal - Add a global which should be forced to be 583 /// present in the object file; these are emitted to the llvm.used 584 /// metadata global. 585 void AddUsedGlobal(llvm::GlobalValue *GV); 586 587 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); } 588 589 /// AddCXXDtorEntry - Add a destructor and object to add to the C++ global 590 /// destructor function. 591 void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) { 592 CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object)); 593 } 594 595 /// CreateRuntimeFunction - Create a new runtime function with the specified 596 /// type and name. 597 llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty, 598 llvm::StringRef Name, 599 llvm::Attributes ExtraAttrs = 600 llvm::Attribute::None); 601 /// CreateRuntimeVariable - Create a new runtime global variable with the 602 /// specified type and name. 603 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty, 604 llvm::StringRef Name); 605 606 ///@name Custom Blocks Runtime Interfaces 607 ///@{ 608 609 llvm::Constant *getNSConcreteGlobalBlock(); 610 llvm::Constant *getNSConcreteStackBlock(); 611 llvm::Constant *getBlockObjectAssign(); 612 llvm::Constant *getBlockObjectDispose(); 613 614 ///@} 615 616 // UpdateCompleteType - Make sure that this type is translated. 617 void UpdateCompletedType(const TagDecl *TD); 618 619 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e); 620 621 /// EmitConstantExpr - Try to emit the given expression as a 622 /// constant; returns 0 if the expression cannot be emitted as a 623 /// constant. 624 llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType, 625 CodeGenFunction *CGF = 0); 626 627 /// EmitNullConstant - Return the result of value-initializing the given 628 /// type, i.e. a null expression of the given type. This is usually, 629 /// but not always, an LLVM null constant. 630 llvm::Constant *EmitNullConstant(QualType T); 631 632 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV, 633 const AnnotateAttr *AA, unsigned LineNo); 634 635 /// Error - Emit a general error that something can't be done. 636 void Error(SourceLocation loc, llvm::StringRef error); 637 638 /// ErrorUnsupported - Print out an error that codegen doesn't support the 639 /// specified stmt yet. 640 /// \param OmitOnError - If true, then this error should only be emitted if no 641 /// other errors have been reported. 642 void ErrorUnsupported(const Stmt *S, const char *Type, 643 bool OmitOnError=false); 644 645 /// ErrorUnsupported - Print out an error that codegen doesn't support the 646 /// specified decl yet. 647 /// \param OmitOnError - If true, then this error should only be emitted if no 648 /// other errors have been reported. 649 void ErrorUnsupported(const Decl *D, const char *Type, 650 bool OmitOnError=false); 651 652 /// SetInternalFunctionAttributes - Set the attributes on the LLVM 653 /// function for the given decl and function info. This applies 654 /// attributes necessary for handling the ABI as well as user 655 /// specified attributes like section. 656 void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F, 657 const CGFunctionInfo &FI); 658 659 /// SetLLVMFunctionAttributes - Set the LLVM function attributes 660 /// (sext, zext, etc). 661 void SetLLVMFunctionAttributes(const Decl *D, 662 const CGFunctionInfo &Info, 663 llvm::Function *F); 664 665 /// SetLLVMFunctionAttributesForDefinition - Set the LLVM function attributes 666 /// which only apply to a function definintion. 667 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F); 668 669 /// ReturnTypeUsesSRet - Return true iff the given type uses 'sret' when used 670 /// as a return type. 671 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI); 672 673 /// ReturnTypeUsesSret - Return true iff the given type uses 'fpret' when used 674 /// as a return type. 675 bool ReturnTypeUsesFPRet(QualType ResultType); 676 677 /// ConstructAttributeList - Get the LLVM attributes and calling convention to 678 /// use for a particular function type. 679 /// 680 /// \param Info - The function type information. 681 /// \param TargetDecl - The decl these attributes are being constructed 682 /// for. If supplied the attributes applied to this decl may contribute to the 683 /// function attributes and calling convention. 684 /// \param PAL [out] - On return, the attribute list to use. 685 /// \param CallingConv [out] - On return, the LLVM calling convention to use. 686 void ConstructAttributeList(const CGFunctionInfo &Info, 687 const Decl *TargetDecl, 688 AttributeListType &PAL, 689 unsigned &CallingConv); 690 691 llvm::StringRef getMangledName(GlobalDecl GD); 692 void getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer, 693 const BlockDecl *BD); 694 695 void EmitTentativeDefinition(const VarDecl *D); 696 697 void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired); 698 699 llvm::GlobalVariable::LinkageTypes 700 getFunctionLinkage(const FunctionDecl *FD); 701 702 void setFunctionLinkage(const FunctionDecl *FD, llvm::GlobalValue *V) { 703 V->setLinkage(getFunctionLinkage(FD)); 704 } 705 706 /// getVTableLinkage - Return the appropriate linkage for the vtable, VTT, 707 /// and type information of the given class. 708 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD); 709 710 /// GetTargetTypeStoreSize - Return the store size, in character units, of 711 /// the given LLVM type. 712 CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const; 713 714 /// GetLLVMLinkageVarDefinition - Returns LLVM linkage for a global 715 /// variable. 716 llvm::GlobalValue::LinkageTypes 717 GetLLVMLinkageVarDefinition(const VarDecl *D, 718 llvm::GlobalVariable *GV); 719 720 std::vector<const CXXRecordDecl*> DeferredVTables; 721 722 private: 723 llvm::GlobalValue *GetGlobalValue(llvm::StringRef Ref); 724 725 llvm::Constant *GetOrCreateLLVMFunction(llvm::StringRef MangledName, 726 llvm::Type *Ty, 727 GlobalDecl D, 728 bool ForVTable, 729 llvm::Attributes ExtraAttrs = 730 llvm::Attribute::None); 731 llvm::Constant *GetOrCreateLLVMGlobal(llvm::StringRef MangledName, 732 llvm::PointerType *PTy, 733 const VarDecl *D, 734 bool UnnamedAddr = false); 735 736 /// SetCommonAttributes - Set attributes which are common to any 737 /// form of a global definition (alias, Objective-C method, 738 /// function, global variable). 739 /// 740 /// NOTE: This should only be called for definitions. 741 void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV); 742 743 /// SetFunctionDefinitionAttributes - Set attributes for a global definition. 744 void SetFunctionDefinitionAttributes(const FunctionDecl *D, 745 llvm::GlobalValue *GV); 746 747 /// SetFunctionAttributes - Set function attributes for a function 748 /// declaration. 749 void SetFunctionAttributes(GlobalDecl GD, 750 llvm::Function *F, 751 bool IsIncompleteFunction); 752 753 /// EmitGlobal - Emit code for a singal global function or var decl. Forward 754 /// declarations are emitted lazily. 755 void EmitGlobal(GlobalDecl D); 756 757 void EmitGlobalDefinition(GlobalDecl D); 758 759 void EmitGlobalFunctionDefinition(GlobalDecl GD); 760 void EmitGlobalVarDefinition(const VarDecl *D); 761 void EmitAliasDefinition(GlobalDecl GD); 762 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D); 763 void EmitObjCIvarInitializations(ObjCImplementationDecl *D); 764 765 // C++ related functions. 766 767 bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target); 768 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D); 769 770 void EmitNamespace(const NamespaceDecl *D); 771 void EmitLinkageSpec(const LinkageSpecDecl *D); 772 773 /// EmitCXXConstructors - Emit constructors (base, complete) from a 774 /// C++ constructor Decl. 775 void EmitCXXConstructors(const CXXConstructorDecl *D); 776 777 /// EmitCXXConstructor - Emit a single constructor with the given type from 778 /// a C++ constructor Decl. 779 void EmitCXXConstructor(const CXXConstructorDecl *D, CXXCtorType Type); 780 781 /// EmitCXXDestructors - Emit destructors (base, complete) from a 782 /// C++ destructor Decl. 783 void EmitCXXDestructors(const CXXDestructorDecl *D); 784 785 /// EmitCXXDestructor - Emit a single destructor with the given type from 786 /// a C++ destructor Decl. 787 void EmitCXXDestructor(const CXXDestructorDecl *D, CXXDtorType Type); 788 789 /// EmitCXXGlobalInitFunc - Emit the function that initializes C++ globals. 790 void EmitCXXGlobalInitFunc(); 791 792 /// EmitCXXGlobalDtorFunc - Emit the function that destroys C++ globals. 793 void EmitCXXGlobalDtorFunc(); 794 795 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, 796 llvm::GlobalVariable *Addr); 797 798 // FIXME: Hardcoding priority here is gross. 799 void AddGlobalCtor(llvm::Function *Ctor, int Priority=65535); 800 void AddGlobalDtor(llvm::Function *Dtor, int Priority=65535); 801 802 /// EmitCtorList - Generates a global array of functions and priorities using 803 /// the given list and name. This array will have appending linkage and is 804 /// suitable for use as a LLVM constructor or destructor array. 805 void EmitCtorList(const CtorList &Fns, const char *GlobalName); 806 807 void EmitAnnotations(void); 808 809 /// EmitFundamentalRTTIDescriptor - Emit the RTTI descriptors for the 810 /// given type. 811 void EmitFundamentalRTTIDescriptor(QualType Type); 812 813 /// EmitFundamentalRTTIDescriptors - Emit the RTTI descriptors for the 814 /// builtin types. 815 void EmitFundamentalRTTIDescriptors(); 816 817 /// EmitDeferred - Emit any needed decls for which code generation 818 /// was deferred. 819 void EmitDeferred(void); 820 821 /// EmitLLVMUsed - Emit the llvm.used metadata used to force 822 /// references to global which may otherwise be optimized out. 823 void EmitLLVMUsed(void); 824 825 void EmitDeclMetadata(); 826 827 /// EmitCoverageFile - Emit the llvm.gcov metadata used to tell LLVM where 828 /// to emit the .gcno and .gcda files in a way that persists in .bc files. 829 void EmitCoverageFile(); 830 831 /// MayDeferGeneration - Determine if the given decl can be emitted 832 /// lazily; this is only relevant for definitions. The given decl 833 /// must be either a function or var decl. 834 bool MayDeferGeneration(const ValueDecl *D); 835 836 /// SimplifyPersonality - Check whether we can use a "simpler", more 837 /// core exceptions personality function. 838 void SimplifyPersonality(); 839 }; 840 } // end namespace CodeGen 841 } // end namespace clang 842 843 #endif 844