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 "CGVTables.h" 18 #include "CodeGenTypes.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 "clang/Basic/ABI.h" 25 #include "clang/Basic/LangOptions.h" 26 #include "clang/Basic/Module.h" 27 #include "llvm/ADT/DenseMap.h" 28 #include "llvm/ADT/SetVector.h" 29 #include "llvm/ADT/SmallPtrSet.h" 30 #include "llvm/ADT/StringMap.h" 31 #include "llvm/IR/CallingConv.h" 32 #include "llvm/IR/Module.h" 33 #include "llvm/Support/ValueHandle.h" 34 #include "llvm/Transforms/Utils/SpecialCaseList.h" 35 36 namespace llvm { 37 class Module; 38 class Constant; 39 class ConstantInt; 40 class Function; 41 class GlobalValue; 42 class DataLayout; 43 class FunctionType; 44 class LLVMContext; 45 } 46 47 namespace clang { 48 class TargetCodeGenInfo; 49 class ASTContext; 50 class AtomicType; 51 class FunctionDecl; 52 class IdentifierInfo; 53 class ObjCMethodDecl; 54 class ObjCImplementationDecl; 55 class ObjCCategoryImplDecl; 56 class ObjCProtocolDecl; 57 class ObjCEncodeExpr; 58 class BlockExpr; 59 class CharUnits; 60 class Decl; 61 class Expr; 62 class Stmt; 63 class InitListExpr; 64 class StringLiteral; 65 class NamedDecl; 66 class ValueDecl; 67 class VarDecl; 68 class LangOptions; 69 class CodeGenOptions; 70 class DiagnosticsEngine; 71 class AnnotateAttr; 72 class CXXDestructorDecl; 73 class MangleBuffer; 74 class Module; 75 76 namespace CodeGen { 77 78 class CallArgList; 79 class CodeGenFunction; 80 class CodeGenTBAA; 81 class CGCXXABI; 82 class CGDebugInfo; 83 class CGObjCRuntime; 84 class CGOpenCLRuntime; 85 class CGCUDARuntime; 86 class BlockFieldFlags; 87 class FunctionArgList; 88 class PGOProfileData; 89 90 struct OrderGlobalInits { 91 unsigned int priority; 92 unsigned int lex_order; 93 OrderGlobalInits(unsigned int p, unsigned int l) 94 : priority(p), lex_order(l) {} 95 96 bool operator==(const OrderGlobalInits &RHS) const { 97 return priority == RHS.priority && 98 lex_order == RHS.lex_order; 99 } 100 101 bool operator<(const OrderGlobalInits &RHS) const { 102 if (priority < RHS.priority) 103 return true; 104 105 return priority == RHS.priority && lex_order < RHS.lex_order; 106 } 107 }; 108 109 struct CodeGenTypeCache { 110 /// void 111 llvm::Type *VoidTy; 112 113 /// i8, i16, i32, and i64 114 llvm::IntegerType *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty; 115 /// float, double 116 llvm::Type *FloatTy, *DoubleTy; 117 118 /// int 119 llvm::IntegerType *IntTy; 120 121 /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size. 122 union { 123 llvm::IntegerType *IntPtrTy; 124 llvm::IntegerType *SizeTy; 125 llvm::IntegerType *PtrDiffTy; 126 }; 127 128 /// void* in address space 0 129 union { 130 llvm::PointerType *VoidPtrTy; 131 llvm::PointerType *Int8PtrTy; 132 }; 133 134 /// void** in address space 0 135 union { 136 llvm::PointerType *VoidPtrPtrTy; 137 llvm::PointerType *Int8PtrPtrTy; 138 }; 139 140 /// The width of a pointer into the generic address space. 141 unsigned char PointerWidthInBits; 142 143 /// The size and alignment of a pointer into the generic address 144 /// space. 145 union { 146 unsigned char PointerAlignInBytes; 147 unsigned char PointerSizeInBytes; 148 unsigned char SizeSizeInBytes; // sizeof(size_t) 149 }; 150 151 llvm::CallingConv::ID RuntimeCC; 152 llvm::CallingConv::ID getRuntimeCC() const { 153 return RuntimeCC; 154 } 155 }; 156 157 struct RREntrypoints { 158 RREntrypoints() { memset(this, 0, sizeof(*this)); } 159 /// void objc_autoreleasePoolPop(void*); 160 llvm::Constant *objc_autoreleasePoolPop; 161 162 /// void *objc_autoreleasePoolPush(void); 163 llvm::Constant *objc_autoreleasePoolPush; 164 }; 165 166 struct ARCEntrypoints { 167 ARCEntrypoints() { memset(this, 0, sizeof(*this)); } 168 169 /// id objc_autorelease(id); 170 llvm::Constant *objc_autorelease; 171 172 /// id objc_autoreleaseReturnValue(id); 173 llvm::Constant *objc_autoreleaseReturnValue; 174 175 /// void objc_copyWeak(id *dest, id *src); 176 llvm::Constant *objc_copyWeak; 177 178 /// void objc_destroyWeak(id*); 179 llvm::Constant *objc_destroyWeak; 180 181 /// id objc_initWeak(id*, id); 182 llvm::Constant *objc_initWeak; 183 184 /// id objc_loadWeak(id*); 185 llvm::Constant *objc_loadWeak; 186 187 /// id objc_loadWeakRetained(id*); 188 llvm::Constant *objc_loadWeakRetained; 189 190 /// void objc_moveWeak(id *dest, id *src); 191 llvm::Constant *objc_moveWeak; 192 193 /// id objc_retain(id); 194 llvm::Constant *objc_retain; 195 196 /// id objc_retainAutorelease(id); 197 llvm::Constant *objc_retainAutorelease; 198 199 /// id objc_retainAutoreleaseReturnValue(id); 200 llvm::Constant *objc_retainAutoreleaseReturnValue; 201 202 /// id objc_retainAutoreleasedReturnValue(id); 203 llvm::Constant *objc_retainAutoreleasedReturnValue; 204 205 /// id objc_retainBlock(id); 206 llvm::Constant *objc_retainBlock; 207 208 /// void objc_release(id); 209 llvm::Constant *objc_release; 210 211 /// id objc_storeStrong(id*, id); 212 llvm::Constant *objc_storeStrong; 213 214 /// id objc_storeWeak(id*, id); 215 llvm::Constant *objc_storeWeak; 216 217 /// A void(void) inline asm to use to mark that the return value of 218 /// a call will be immediately retain. 219 llvm::InlineAsm *retainAutoreleasedReturnValueMarker; 220 221 /// void clang.arc.use(...); 222 llvm::Constant *clang_arc_use; 223 }; 224 225 /// CodeGenModule - This class organizes the cross-function state that is used 226 /// while generating LLVM code. 227 class CodeGenModule : public CodeGenTypeCache { 228 CodeGenModule(const CodeGenModule &) LLVM_DELETED_FUNCTION; 229 void operator=(const CodeGenModule &) LLVM_DELETED_FUNCTION; 230 231 typedef std::vector<std::pair<llvm::Constant*, int> > CtorList; 232 233 ASTContext &Context; 234 const LangOptions &LangOpts; 235 const CodeGenOptions &CodeGenOpts; 236 llvm::Module &TheModule; 237 DiagnosticsEngine &Diags; 238 const llvm::DataLayout &TheDataLayout; 239 const TargetInfo &Target; 240 llvm::OwningPtr<CGCXXABI> ABI; 241 llvm::LLVMContext &VMContext; 242 243 CodeGenTBAA *TBAA; 244 245 mutable const TargetCodeGenInfo *TheTargetCodeGenInfo; 246 247 // This should not be moved earlier, since its initialization depends on some 248 // of the previous reference members being already initialized and also checks 249 // if TheTargetCodeGenInfo is NULL 250 CodeGenTypes Types; 251 252 /// VTables - Holds information about C++ vtables. 253 CodeGenVTables VTables; 254 255 CGObjCRuntime* ObjCRuntime; 256 CGOpenCLRuntime* OpenCLRuntime; 257 CGCUDARuntime* CUDARuntime; 258 CGDebugInfo* DebugInfo; 259 ARCEntrypoints *ARCData; 260 llvm::MDNode *NoObjCARCExceptionsMetadata; 261 RREntrypoints *RRData; 262 PGOProfileData *PGOData; 263 264 // WeakRefReferences - A set of references that have only been seen via 265 // a weakref so far. This is used to remove the weak of the reference if we 266 // ever see a direct reference or a definition. 267 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences; 268 269 /// DeferredDecls - This contains all the decls which have definitions but 270 /// which are deferred for emission and therefore should only be output if 271 /// they are actually used. If a decl is in this, then it is known to have 272 /// not been referenced yet. 273 llvm::StringMap<GlobalDecl> DeferredDecls; 274 275 /// DeferredDeclsToEmit - This is a list of deferred decls which we have seen 276 /// that *are* actually referenced. These get code generated when the module 277 /// is done. 278 struct DeferredGlobal { 279 DeferredGlobal(llvm::GlobalValue *GV, GlobalDecl GD) : GV(GV), GD(GD) {} 280 llvm::AssertingVH<llvm::GlobalValue> GV; 281 GlobalDecl GD; 282 }; 283 std::vector<DeferredGlobal> DeferredDeclsToEmit; 284 void addDeferredDeclToEmit(llvm::GlobalValue *GV, GlobalDecl GD) { 285 DeferredDeclsToEmit.push_back(DeferredGlobal(GV, GD)); 286 } 287 288 /// List of alias we have emitted. Used to make sure that what they point to 289 /// is defined once we get to the end of the of the translation unit. 290 std::vector<GlobalDecl> Aliases; 291 292 typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy; 293 ReplacementsTy Replacements; 294 295 /// DeferredVTables - A queue of (optional) vtables to consider emitting. 296 std::vector<const CXXRecordDecl*> DeferredVTables; 297 298 /// LLVMUsed - List of global values which are required to be 299 /// present in the object file; bitcast to i8*. This is used for 300 /// forcing visibility of symbols which may otherwise be optimized 301 /// out. 302 std::vector<llvm::WeakVH> LLVMUsed; 303 304 /// GlobalCtors - Store the list of global constructors and their respective 305 /// priorities to be emitted when the translation unit is complete. 306 CtorList GlobalCtors; 307 308 /// GlobalDtors - Store the list of global destructors and their respective 309 /// priorities to be emitted when the translation unit is complete. 310 CtorList GlobalDtors; 311 312 /// MangledDeclNames - A map of canonical GlobalDecls to their mangled names. 313 llvm::DenseMap<GlobalDecl, StringRef> MangledDeclNames; 314 llvm::BumpPtrAllocator MangledNamesAllocator; 315 316 /// Global annotations. 317 std::vector<llvm::Constant*> Annotations; 318 319 /// Map used to get unique annotation strings. 320 llvm::StringMap<llvm::Constant*> AnnotationStrings; 321 322 llvm::StringMap<llvm::Constant*> CFConstantStringMap; 323 llvm::StringMap<llvm::GlobalVariable*> ConstantStringMap; 324 llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap; 325 llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap; 326 llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap; 327 328 llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap; 329 llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap; 330 331 /// Map used to get unique type descriptor constants for sanitizers. 332 llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap; 333 334 /// Map used to track internal linkage functions declared within 335 /// extern "C" regions. 336 typedef llvm::MapVector<IdentifierInfo *, 337 llvm::GlobalValue *> StaticExternCMap; 338 StaticExternCMap StaticExternCValues; 339 340 /// \brief thread_local variables defined or used in this TU. 341 std::vector<std::pair<const VarDecl *, llvm::GlobalVariable *> > 342 CXXThreadLocals; 343 344 /// \brief thread_local variables with initializers that need to run 345 /// before any thread_local variable in this TU is odr-used. 346 std::vector<llvm::Constant*> CXXThreadLocalInits; 347 348 /// CXXGlobalInits - Global variables with initializers that need to run 349 /// before main. 350 std::vector<llvm::Constant*> CXXGlobalInits; 351 352 /// When a C++ decl with an initializer is deferred, null is 353 /// appended to CXXGlobalInits, and the index of that null is placed 354 /// here so that the initializer will be performed in the correct 355 /// order. 356 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition; 357 358 typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData; 359 360 struct GlobalInitPriorityCmp { 361 bool operator()(const GlobalInitData &LHS, 362 const GlobalInitData &RHS) const { 363 return LHS.first.priority < RHS.first.priority; 364 } 365 }; 366 367 /// - Global variables with initializers whose order of initialization 368 /// is set by init_priority attribute. 369 SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits; 370 371 /// CXXGlobalDtors - Global destructor functions and arguments that need to 372 /// run on termination. 373 std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors; 374 375 /// \brief The complete set of modules that has been imported. 376 llvm::SetVector<clang::Module *> ImportedModules; 377 378 /// \brief A vector of metadata strings. 379 SmallVector<llvm::Value *, 16> LinkerOptionsMetadata; 380 381 /// @name Cache for Objective-C runtime types 382 /// @{ 383 384 /// CFConstantStringClassRef - Cached reference to the class for constant 385 /// strings. This value has type int * but is actually an Obj-C class pointer. 386 llvm::WeakVH CFConstantStringClassRef; 387 388 /// ConstantStringClassRef - Cached reference to the class for constant 389 /// strings. This value has type int * but is actually an Obj-C class pointer. 390 llvm::WeakVH ConstantStringClassRef; 391 392 /// \brief The LLVM type corresponding to NSConstantString. 393 llvm::StructType *NSConstantStringType; 394 395 /// \brief The type used to describe the state of a fast enumeration in 396 /// Objective-C's for..in loop. 397 QualType ObjCFastEnumerationStateType; 398 399 /// @} 400 401 /// Lazily create the Objective-C runtime 402 void createObjCRuntime(); 403 404 void createOpenCLRuntime(); 405 void createCUDARuntime(); 406 407 bool isTriviallyRecursive(const FunctionDecl *F); 408 bool shouldEmitFunction(GlobalDecl GD); 409 410 /// @name Cache for Blocks Runtime Globals 411 /// @{ 412 413 llvm::Constant *NSConcreteGlobalBlock; 414 llvm::Constant *NSConcreteStackBlock; 415 416 llvm::Constant *BlockObjectAssign; 417 llvm::Constant *BlockObjectDispose; 418 419 llvm::Type *BlockDescriptorType; 420 llvm::Type *GenericBlockLiteralType; 421 422 struct { 423 int GlobalUniqueCount; 424 } Block; 425 426 /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>) 427 llvm::Constant *LifetimeStartFn; 428 429 /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>) 430 llvm::Constant *LifetimeEndFn; 431 432 GlobalDecl initializedGlobalDecl; 433 434 llvm::OwningPtr<llvm::SpecialCaseList> SanitizerBlacklist; 435 436 const SanitizerOptions &SanOpts; 437 438 /// @} 439 public: 440 CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts, 441 llvm::Module &M, const llvm::DataLayout &TD, 442 DiagnosticsEngine &Diags); 443 444 ~CodeGenModule(); 445 446 void clear(); 447 448 /// Release - Finalize LLVM code generation. 449 void Release(); 450 451 /// getObjCRuntime() - Return a reference to the configured 452 /// Objective-C runtime. 453 CGObjCRuntime &getObjCRuntime() { 454 if (!ObjCRuntime) createObjCRuntime(); 455 return *ObjCRuntime; 456 } 457 458 /// hasObjCRuntime() - Return true iff an Objective-C runtime has 459 /// been configured. 460 bool hasObjCRuntime() { return !!ObjCRuntime; } 461 462 /// getOpenCLRuntime() - Return a reference to the configured OpenCL runtime. 463 CGOpenCLRuntime &getOpenCLRuntime() { 464 assert(OpenCLRuntime != 0); 465 return *OpenCLRuntime; 466 } 467 468 /// getCUDARuntime() - Return a reference to the configured CUDA runtime. 469 CGCUDARuntime &getCUDARuntime() { 470 assert(CUDARuntime != 0); 471 return *CUDARuntime; 472 } 473 474 ARCEntrypoints &getARCEntrypoints() const { 475 assert(getLangOpts().ObjCAutoRefCount && ARCData != 0); 476 return *ARCData; 477 } 478 479 RREntrypoints &getRREntrypoints() const { 480 assert(RRData != 0); 481 return *RRData; 482 } 483 484 PGOProfileData *getPGOData() const { 485 return PGOData; 486 } 487 488 llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) { 489 return StaticLocalDeclMap[D]; 490 } 491 void setStaticLocalDeclAddress(const VarDecl *D, 492 llvm::Constant *C) { 493 StaticLocalDeclMap[D] = C; 494 } 495 496 llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) { 497 return StaticLocalDeclGuardMap[D]; 498 } 499 void setStaticLocalDeclGuardAddress(const VarDecl *D, 500 llvm::GlobalVariable *C) { 501 StaticLocalDeclGuardMap[D] = C; 502 } 503 504 llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) { 505 return AtomicSetterHelperFnMap[Ty]; 506 } 507 void setAtomicSetterHelperFnMap(QualType Ty, 508 llvm::Constant *Fn) { 509 AtomicSetterHelperFnMap[Ty] = Fn; 510 } 511 512 llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) { 513 return AtomicGetterHelperFnMap[Ty]; 514 } 515 void setAtomicGetterHelperFnMap(QualType Ty, 516 llvm::Constant *Fn) { 517 AtomicGetterHelperFnMap[Ty] = Fn; 518 } 519 520 llvm::Constant *getTypeDescriptor(QualType Ty) { 521 return TypeDescriptorMap[Ty]; 522 } 523 void setTypeDescriptor(QualType Ty, llvm::Constant *C) { 524 TypeDescriptorMap[Ty] = C; 525 } 526 527 CGDebugInfo *getModuleDebugInfo() { return DebugInfo; } 528 529 llvm::MDNode *getNoObjCARCExceptionsMetadata() { 530 if (!NoObjCARCExceptionsMetadata) 531 NoObjCARCExceptionsMetadata = 532 llvm::MDNode::get(getLLVMContext(), 533 SmallVector<llvm::Value*,1>()); 534 return NoObjCARCExceptionsMetadata; 535 } 536 537 ASTContext &getContext() const { return Context; } 538 const LangOptions &getLangOpts() const { return LangOpts; } 539 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } 540 llvm::Module &getModule() const { return TheModule; } 541 DiagnosticsEngine &getDiags() const { return Diags; } 542 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; } 543 const TargetInfo &getTarget() const { return Target; } 544 CGCXXABI &getCXXABI() const { return *ABI; } 545 llvm::LLVMContext &getLLVMContext() { return VMContext; } 546 547 bool shouldUseTBAA() const { return TBAA != 0; } 548 549 const TargetCodeGenInfo &getTargetCodeGenInfo(); 550 551 CodeGenTypes &getTypes() { return Types; } 552 553 CodeGenVTables &getVTables() { return VTables; } 554 555 ItaniumVTableContext &getItaniumVTableContext() { 556 return VTables.getItaniumVTableContext(); 557 } 558 559 MicrosoftVTableContext &getMicrosoftVTableContext() { 560 return VTables.getMicrosoftVTableContext(); 561 } 562 563 llvm::MDNode *getTBAAInfo(QualType QTy); 564 llvm::MDNode *getTBAAInfoForVTablePtr(); 565 llvm::MDNode *getTBAAStructInfo(QualType QTy); 566 /// Return the MDNode in the type DAG for the given struct type. 567 llvm::MDNode *getTBAAStructTypeInfo(QualType QTy); 568 /// Return the path-aware tag for given base type, access node and offset. 569 llvm::MDNode *getTBAAStructTagInfo(QualType BaseTy, llvm::MDNode *AccessN, 570 uint64_t O); 571 572 bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor); 573 574 bool isPaddedAtomicType(QualType type); 575 bool isPaddedAtomicType(const AtomicType *type); 576 577 /// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag 578 /// is the same as the type. For struct-path aware TBAA, the tag 579 /// is different from the type: base type, access type and offset. 580 /// When ConvertTypeToTag is true, we create a tag based on the scalar type. 581 void DecorateInstruction(llvm::Instruction *Inst, 582 llvm::MDNode *TBAAInfo, 583 bool ConvertTypeToTag = true); 584 585 /// getSize - Emit the given number of characters as a value of type size_t. 586 llvm::ConstantInt *getSize(CharUnits numChars); 587 588 /// setGlobalVisibility - Set the visibility for the given LLVM 589 /// GlobalValue. 590 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const; 591 592 /// setTLSMode - Set the TLS mode for the given LLVM GlobalVariable 593 /// for the thread-local variable declaration D. 594 void setTLSMode(llvm::GlobalVariable *GV, const VarDecl &D) const; 595 596 /// TypeVisibilityKind - The kind of global variable that is passed to 597 /// setTypeVisibility 598 enum TypeVisibilityKind { 599 TVK_ForVTT, 600 TVK_ForVTable, 601 TVK_ForConstructionVTable, 602 TVK_ForRTTI, 603 TVK_ForRTTIName 604 }; 605 606 /// setTypeVisibility - Set the visibility for the given global 607 /// value which holds information about a type. 608 void setTypeVisibility(llvm::GlobalValue *GV, const CXXRecordDecl *D, 609 TypeVisibilityKind TVK) const; 610 611 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) { 612 switch (V) { 613 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility; 614 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility; 615 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility; 616 } 617 llvm_unreachable("unknown visibility!"); 618 } 619 620 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) { 621 if (isa<CXXConstructorDecl>(GD.getDecl())) 622 return GetAddrOfCXXConstructor(cast<CXXConstructorDecl>(GD.getDecl()), 623 GD.getCtorType()); 624 else if (isa<CXXDestructorDecl>(GD.getDecl())) 625 return GetAddrOfCXXDestructor(cast<CXXDestructorDecl>(GD.getDecl()), 626 GD.getDtorType()); 627 else if (isa<FunctionDecl>(GD.getDecl())) 628 return GetAddrOfFunction(GD); 629 else 630 return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl())); 631 } 632 633 /// CreateOrReplaceCXXRuntimeVariable - Will return a global variable of the 634 /// given type. If a variable with a different type already exists then a new 635 /// variable with the right type will be created and all uses of the old 636 /// variable will be replaced with a bitcast to the new variable. 637 llvm::GlobalVariable * 638 CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, 639 llvm::GlobalValue::LinkageTypes Linkage); 640 641 /// GetGlobalVarAddressSpace - Return the address space of the underlying 642 /// global variable for D, as determined by its declaration. Normally this 643 /// is the same as the address space of D's type, but in CUDA, address spaces 644 /// are associated with declarations, not types. 645 unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace); 646 647 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 648 /// given global variable. If Ty is non-null and if the global doesn't exist, 649 /// then it will be greated with the specified type instead of whatever the 650 /// normal requested type would be. 651 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, 652 llvm::Type *Ty = 0); 653 654 655 /// GetAddrOfFunction - Return the address of the given function. If Ty is 656 /// non-null, then this function will use the specified type if it has to 657 /// create it. 658 llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = 0, 659 bool ForVTable = false, 660 bool DontDefer = false); 661 662 /// GetAddrOfRTTIDescriptor - Get the address of the RTTI descriptor 663 /// for the given type. 664 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false); 665 666 /// GetAddrOfUuidDescriptor - Get the address of a uuid descriptor . 667 llvm::Constant *GetAddrOfUuidDescriptor(const CXXUuidofExpr* E); 668 669 /// GetAddrOfThunk - Get the address of the thunk for the given global decl. 670 llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk); 671 672 /// GetWeakRefReference - Get a reference to the target of VD. 673 llvm::Constant *GetWeakRefReference(const ValueDecl *VD); 674 675 /// GetNonVirtualBaseClassOffset - Returns the offset from a derived class to 676 /// a class. Returns null if the offset is 0. 677 llvm::Constant * 678 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, 679 CastExpr::path_const_iterator PathBegin, 680 CastExpr::path_const_iterator PathEnd); 681 682 /// A pair of helper functions for a __block variable. 683 class ByrefHelpers : public llvm::FoldingSetNode { 684 public: 685 llvm::Constant *CopyHelper; 686 llvm::Constant *DisposeHelper; 687 688 /// The alignment of the field. This is important because 689 /// different offsets to the field within the byref struct need to 690 /// have different helper functions. 691 CharUnits Alignment; 692 693 ByrefHelpers(CharUnits alignment) : Alignment(alignment) {} 694 virtual ~ByrefHelpers(); 695 696 void Profile(llvm::FoldingSetNodeID &id) const { 697 id.AddInteger(Alignment.getQuantity()); 698 profileImpl(id); 699 } 700 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0; 701 702 virtual bool needsCopy() const { return true; } 703 virtual void emitCopy(CodeGenFunction &CGF, 704 llvm::Value *dest, llvm::Value *src) = 0; 705 706 virtual bool needsDispose() const { return true; } 707 virtual void emitDispose(CodeGenFunction &CGF, llvm::Value *field) = 0; 708 }; 709 710 llvm::FoldingSet<ByrefHelpers> ByrefHelpersCache; 711 712 /// getUniqueBlockCount - Fetches the global unique block count. 713 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; } 714 715 /// getBlockDescriptorType - Fetches the type of a generic block 716 /// descriptor. 717 llvm::Type *getBlockDescriptorType(); 718 719 /// getGenericBlockLiteralType - The type of a generic block literal. 720 llvm::Type *getGenericBlockLiteralType(); 721 722 /// GetAddrOfGlobalBlock - Gets the address of a block which 723 /// requires no captures. 724 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *); 725 726 /// GetAddrOfConstantCFString - Return a pointer to a constant CFString object 727 /// for the given string. 728 llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal); 729 730 /// GetAddrOfConstantString - Return a pointer to a constant NSString object 731 /// for the given string. Or a user defined String object as defined via 732 /// -fconstant-string-class=class_name option. 733 llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal); 734 735 /// GetConstantArrayFromStringLiteral - Return a constant array for the given 736 /// string. 737 llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E); 738 739 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a constant array 740 /// for the given string literal. 741 llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S); 742 743 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 744 /// array for the given ObjCEncodeExpr node. 745 llvm::Constant *GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *); 746 747 /// GetAddrOfConstantString - Returns a pointer to a character array 748 /// containing the literal. This contents are exactly that of the given 749 /// string, i.e. it will not be null terminated automatically; see 750 /// GetAddrOfConstantCString. Note that whether the result is actually a 751 /// pointer to an LLVM constant depends on Feature.WriteableStrings. 752 /// 753 /// The result has pointer to array type. 754 /// 755 /// \param GlobalName If provided, the name to use for the global 756 /// (if one is created). 757 llvm::Constant *GetAddrOfConstantString(StringRef Str, 758 const char *GlobalName=0, 759 unsigned Alignment=0); 760 761 /// GetAddrOfConstantCString - Returns a pointer to a character array 762 /// containing the literal and a terminating '\0' character. The result has 763 /// pointer to array type. 764 /// 765 /// \param GlobalName If provided, the name to use for the global (if one is 766 /// created). 767 llvm::Constant *GetAddrOfConstantCString(const std::string &str, 768 const char *GlobalName=0, 769 unsigned Alignment=0); 770 771 /// GetAddrOfConstantCompoundLiteral - Returns a pointer to a constant global 772 /// variable for the given file-scope compound literal expression. 773 llvm::Constant *GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E); 774 775 /// \brief Returns a pointer to a global variable representing a temporary 776 /// with static or thread storage duration. 777 llvm::Constant *GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E, 778 const Expr *Inner); 779 780 /// \brief Retrieve the record type that describes the state of an 781 /// Objective-C fast enumeration loop (for..in). 782 QualType getObjCFastEnumerationStateType(); 783 784 /// GetAddrOfCXXConstructor - Return the address of the constructor of the 785 /// given type. 786 llvm::GlobalValue *GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor, 787 CXXCtorType ctorType, 788 const CGFunctionInfo *fnInfo = 0, 789 bool DontDefer = false); 790 791 /// GetAddrOfCXXDestructor - Return the address of the constructor of the 792 /// given type. 793 llvm::GlobalValue *GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor, 794 CXXDtorType dtorType, 795 const CGFunctionInfo *fnInfo = 0, 796 llvm::FunctionType *fnType = 0, 797 bool DontDefer = false); 798 799 /// getBuiltinLibFunction - Given a builtin id for a function like 800 /// "__builtin_fabsf", return a Function* for "fabsf". 801 llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD, 802 unsigned BuiltinID); 803 804 llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None); 805 806 /// EmitTopLevelDecl - Emit code for a single top level declaration. 807 void EmitTopLevelDecl(Decl *D); 808 809 /// HandleCXXStaticMemberVarInstantiation - Tell the consumer that this 810 // variable has been instantiated. 811 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD); 812 813 /// \brief If the declaration has internal linkage but is inside an 814 /// extern "C" linkage specification, prepare to emit an alias for it 815 /// to the expected name. 816 template<typename SomeDecl> 817 void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV); 818 819 /// AddUsedGlobal - Add a global which should be forced to be 820 /// present in the object file; these are emitted to the llvm.used 821 /// metadata global. 822 void AddUsedGlobal(llvm::GlobalValue *GV); 823 824 /// AddCXXDtorEntry - Add a destructor and object to add to the C++ global 825 /// destructor function. 826 void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) { 827 CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object)); 828 } 829 830 /// CreateRuntimeFunction - Create a new runtime function with the specified 831 /// type and name. 832 llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty, 833 StringRef Name, 834 llvm::AttributeSet ExtraAttrs = 835 llvm::AttributeSet()); 836 /// CreateRuntimeVariable - Create a new runtime global variable with the 837 /// specified type and name. 838 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty, 839 StringRef Name); 840 841 ///@name Custom Blocks Runtime Interfaces 842 ///@{ 843 844 llvm::Constant *getNSConcreteGlobalBlock(); 845 llvm::Constant *getNSConcreteStackBlock(); 846 llvm::Constant *getBlockObjectAssign(); 847 llvm::Constant *getBlockObjectDispose(); 848 849 ///@} 850 851 llvm::Constant *getLLVMLifetimeStartFn(); 852 llvm::Constant *getLLVMLifetimeEndFn(); 853 854 // UpdateCompleteType - Make sure that this type is translated. 855 void UpdateCompletedType(const TagDecl *TD); 856 857 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e); 858 859 /// EmitConstantInit - Try to emit the initializer for the given declaration 860 /// as a constant; returns 0 if the expression cannot be emitted as a 861 /// constant. 862 llvm::Constant *EmitConstantInit(const VarDecl &D, CodeGenFunction *CGF = 0); 863 864 /// EmitConstantExpr - Try to emit the given expression as a 865 /// constant; returns 0 if the expression cannot be emitted as a 866 /// constant. 867 llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType, 868 CodeGenFunction *CGF = 0); 869 870 /// EmitConstantValue - Emit the given constant value as a constant, in the 871 /// type's scalar representation. 872 llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType, 873 CodeGenFunction *CGF = 0); 874 875 /// EmitConstantValueForMemory - Emit the given constant value as a constant, 876 /// in the type's memory representation. 877 llvm::Constant *EmitConstantValueForMemory(const APValue &Value, 878 QualType DestType, 879 CodeGenFunction *CGF = 0); 880 881 /// EmitNullConstant - Return the result of value-initializing the given 882 /// type, i.e. a null expression of the given type. This is usually, 883 /// but not always, an LLVM null constant. 884 llvm::Constant *EmitNullConstant(QualType T); 885 886 /// EmitNullConstantForBase - Return a null constant appropriate for 887 /// zero-initializing a base class with the given type. This is usually, 888 /// but not always, an LLVM null constant. 889 llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record); 890 891 /// Error - Emit a general error that something can't be done. 892 void Error(SourceLocation loc, StringRef error); 893 894 /// ErrorUnsupported - Print out an error that codegen doesn't support the 895 /// specified stmt yet. 896 void ErrorUnsupported(const Stmt *S, const char *Type); 897 898 /// ErrorUnsupported - Print out an error that codegen doesn't support the 899 /// specified decl yet. 900 void ErrorUnsupported(const Decl *D, const char *Type); 901 902 /// SetInternalFunctionAttributes - Set the attributes on the LLVM 903 /// function for the given decl and function info. This applies 904 /// attributes necessary for handling the ABI as well as user 905 /// specified attributes like section. 906 void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F, 907 const CGFunctionInfo &FI); 908 909 /// SetLLVMFunctionAttributes - Set the LLVM function attributes 910 /// (sext, zext, etc). 911 void SetLLVMFunctionAttributes(const Decl *D, 912 const CGFunctionInfo &Info, 913 llvm::Function *F); 914 915 /// SetLLVMFunctionAttributesForDefinition - Set the LLVM function attributes 916 /// which only apply to a function definintion. 917 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F); 918 919 /// ReturnTypeUsesSRet - Return true iff the given type uses 'sret' when used 920 /// as a return type. 921 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI); 922 923 /// ReturnTypeUsesFPRet - Return true iff the given type uses 'fpret' when 924 /// used as a return type. 925 bool ReturnTypeUsesFPRet(QualType ResultType); 926 927 /// ReturnTypeUsesFP2Ret - Return true iff the given type uses 'fp2ret' when 928 /// used as a return type. 929 bool ReturnTypeUsesFP2Ret(QualType ResultType); 930 931 /// ConstructAttributeList - Get the LLVM attributes and calling convention to 932 /// use for a particular function type. 933 /// 934 /// \param Info - The function type information. 935 /// \param TargetDecl - The decl these attributes are being constructed 936 /// for. If supplied the attributes applied to this decl may contribute to the 937 /// function attributes and calling convention. 938 /// \param PAL [out] - On return, the attribute list to use. 939 /// \param CallingConv [out] - On return, the LLVM calling convention to use. 940 void ConstructAttributeList(const CGFunctionInfo &Info, 941 const Decl *TargetDecl, 942 AttributeListType &PAL, 943 unsigned &CallingConv, 944 bool AttrOnCallSite); 945 946 StringRef getMangledName(GlobalDecl GD); 947 void getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer, 948 const BlockDecl *BD); 949 950 void EmitTentativeDefinition(const VarDecl *D); 951 952 void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired); 953 954 /// EmitFundamentalRTTIDescriptors - Emit the RTTI descriptors for the 955 /// builtin types. 956 void EmitFundamentalRTTIDescriptors(); 957 958 /// \brief Appends Opts to the "Linker Options" metadata value. 959 void AppendLinkerOptions(StringRef Opts); 960 961 /// \brief Appends a detect mismatch command to the linker options. 962 void AddDetectMismatch(StringRef Name, StringRef Value); 963 964 /// \brief Appends a dependent lib to the "Linker Options" metadata value. 965 void AddDependentLib(StringRef Lib); 966 967 llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD); 968 969 void setFunctionLinkage(GlobalDecl GD, llvm::GlobalValue *V) { 970 V->setLinkage(getFunctionLinkage(GD)); 971 } 972 973 /// getVTableLinkage - Return the appropriate linkage for the vtable, VTT, 974 /// and type information of the given class. 975 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD); 976 977 /// GetTargetTypeStoreSize - Return the store size, in character units, of 978 /// the given LLVM type. 979 CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const; 980 981 /// GetLLVMLinkageVarDefinition - Returns LLVM linkage for a global 982 /// variable. 983 llvm::GlobalValue::LinkageTypes 984 GetLLVMLinkageVarDefinition(const VarDecl *D, bool isConstant); 985 986 /// Emit all the global annotations. 987 void EmitGlobalAnnotations(); 988 989 /// Emit an annotation string. 990 llvm::Constant *EmitAnnotationString(StringRef Str); 991 992 /// Emit the annotation's translation unit. 993 llvm::Constant *EmitAnnotationUnit(SourceLocation Loc); 994 995 /// Emit the annotation line number. 996 llvm::Constant *EmitAnnotationLineNo(SourceLocation L); 997 998 /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 999 /// annotation information for a given GlobalValue. The annotation struct is 1000 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 1001 /// GlobalValue being annotated. The second field is the constant string 1002 /// created from the AnnotateAttr's annotation. The third field is a constant 1003 /// string containing the name of the translation unit. The fourth field is 1004 /// the line number in the file of the annotated value declaration. 1005 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV, 1006 const AnnotateAttr *AA, 1007 SourceLocation L); 1008 1009 /// Add global annotations that are set on D, for the global GV. Those 1010 /// annotations are emitted during finalization of the LLVM code. 1011 void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV); 1012 1013 const llvm::SpecialCaseList &getSanitizerBlacklist() const { 1014 return *SanitizerBlacklist; 1015 } 1016 1017 const SanitizerOptions &getSanOpts() const { return SanOpts; } 1018 1019 void addDeferredVTable(const CXXRecordDecl *RD) { 1020 DeferredVTables.push_back(RD); 1021 } 1022 1023 /// EmitGlobal - Emit code for a singal global function or var decl. Forward 1024 /// declarations are emitted lazily. 1025 void EmitGlobal(GlobalDecl D); 1026 1027 private: 1028 llvm::GlobalValue *GetGlobalValue(StringRef Ref); 1029 1030 llvm::Constant * 1031 GetOrCreateLLVMFunction(StringRef MangledName, llvm::Type *Ty, GlobalDecl D, 1032 bool ForVTable, bool DontDefer = false, 1033 llvm::AttributeSet ExtraAttrs = llvm::AttributeSet()); 1034 1035 llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName, 1036 llvm::PointerType *PTy, 1037 const VarDecl *D, 1038 bool UnnamedAddr = false); 1039 1040 /// SetCommonAttributes - Set attributes which are common to any 1041 /// form of a global definition (alias, Objective-C method, 1042 /// function, global variable). 1043 /// 1044 /// NOTE: This should only be called for definitions. 1045 void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV); 1046 1047 /// SetFunctionDefinitionAttributes - Set attributes for a global definition. 1048 void SetFunctionDefinitionAttributes(const FunctionDecl *D, 1049 llvm::GlobalValue *GV); 1050 1051 /// SetFunctionAttributes - Set function attributes for a function 1052 /// declaration. 1053 void SetFunctionAttributes(GlobalDecl GD, 1054 llvm::Function *F, 1055 bool IsIncompleteFunction); 1056 1057 void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = 0); 1058 1059 void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV); 1060 void EmitGlobalVarDefinition(const VarDecl *D); 1061 void EmitAliasDefinition(GlobalDecl GD); 1062 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D); 1063 void EmitObjCIvarInitializations(ObjCImplementationDecl *D); 1064 1065 // C++ related functions. 1066 1067 bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target, 1068 bool InEveryTU); 1069 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D); 1070 1071 void EmitNamespace(const NamespaceDecl *D); 1072 void EmitLinkageSpec(const LinkageSpecDecl *D); 1073 void CompleteDIClassType(const CXXMethodDecl* D); 1074 1075 /// EmitCXXConstructor - Emit a single constructor with the given type from 1076 /// a C++ constructor Decl. 1077 void EmitCXXConstructor(const CXXConstructorDecl *D, CXXCtorType Type); 1078 1079 /// EmitCXXDestructor - Emit a single destructor with the given type from 1080 /// a C++ destructor Decl. 1081 void EmitCXXDestructor(const CXXDestructorDecl *D, CXXDtorType Type); 1082 1083 /// \brief Emit the function that initializes C++ thread_local variables. 1084 void EmitCXXThreadLocalInitFunc(); 1085 1086 /// EmitCXXGlobalInitFunc - Emit the function that initializes C++ globals. 1087 void EmitCXXGlobalInitFunc(); 1088 1089 /// EmitCXXGlobalDtorFunc - Emit the function that destroys C++ globals. 1090 void EmitCXXGlobalDtorFunc(); 1091 1092 /// EmitCXXGlobalVarDeclInitFunc - Emit the function that initializes the 1093 /// specified global (if PerformInit is true) and registers its destructor. 1094 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, 1095 llvm::GlobalVariable *Addr, 1096 bool PerformInit); 1097 1098 // FIXME: Hardcoding priority here is gross. 1099 void AddGlobalCtor(llvm::Function *Ctor, int Priority=65535); 1100 void AddGlobalDtor(llvm::Function *Dtor, int Priority=65535); 1101 1102 /// EmitCtorList - Generates a global array of functions and priorities using 1103 /// the given list and name. This array will have appending linkage and is 1104 /// suitable for use as a LLVM constructor or destructor array. 1105 void EmitCtorList(const CtorList &Fns, const char *GlobalName); 1106 1107 /// EmitFundamentalRTTIDescriptor - Emit the RTTI descriptors for the 1108 /// given type. 1109 void EmitFundamentalRTTIDescriptor(QualType Type); 1110 1111 /// EmitDeferred - Emit any needed decls for which code generation 1112 /// was deferred. 1113 void EmitDeferred(); 1114 1115 /// Call replaceAllUsesWith on all pairs in Replacements. 1116 void applyReplacements(); 1117 1118 void checkAliases(); 1119 1120 /// EmitDeferredVTables - Emit any vtables which we deferred and 1121 /// still have a use for. 1122 void EmitDeferredVTables(); 1123 1124 /// EmitLLVMUsed - Emit the llvm.used metadata used to force 1125 /// references to global which may otherwise be optimized out. 1126 void EmitLLVMUsed(); 1127 1128 /// \brief Emit the link options introduced by imported modules. 1129 void EmitModuleLinkOptions(); 1130 1131 /// \brief Emit aliases for internal-linkage declarations inside "C" language 1132 /// linkage specifications, giving them the "expected" name where possible. 1133 void EmitStaticExternCAliases(); 1134 1135 void EmitDeclMetadata(); 1136 1137 /// \brief Emit the Clang version as llvm.ident metadata. 1138 void EmitVersionIdentMetadata(); 1139 1140 /// EmitCoverageFile - Emit the llvm.gcov metadata used to tell LLVM where 1141 /// to emit the .gcno and .gcda files in a way that persists in .bc files. 1142 void EmitCoverageFile(); 1143 1144 /// Emits the initializer for a uuidof string. 1145 llvm::Constant *EmitUuidofInitializer(StringRef uuidstr, QualType IIDType); 1146 1147 /// MayDeferGeneration - Determine if the given decl can be emitted 1148 /// lazily; this is only relevant for definitions. The given decl 1149 /// must be either a function or var decl. 1150 bool MayDeferGeneration(const ValueDecl *D); 1151 1152 /// SimplifyPersonality - Check whether we can use a "simpler", more 1153 /// core exceptions personality function. 1154 void SimplifyPersonality(); 1155 }; 1156 } // end namespace CodeGen 1157 } // end namespace clang 1158 1159 #endif 1160