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