1 //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the internal per-translation-unit state used for llvm translation.
12 //===----------------------------------------------------------------------===//
14 #ifndef CLANG_CODEGEN_CODEGENMODULE_H
15 #define CLANG_CODEGEN_CODEGENMODULE_H
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/SmallPtrSet.h"
30 #include "llvm/Support/ValueHandle.h"
44 class TargetCodeGenInfo;
49 class ObjCImplementationDecl;
50 class ObjCCategoryImplDecl;
51 class ObjCProtocolDecl;
65 class DiagnosticsEngine;
67 class CXXDestructorDecl;
73 class CodeGenFunction;
78 class CGOpenCLRuntime;
80 class BlockFieldFlags;
81 class FunctionArgList;
83 struct OrderGlobalInits {
84 unsigned int priority;
85 unsigned int lex_order;
86 OrderGlobalInits(unsigned int p, unsigned int l)
87 : priority(p), lex_order(l) {}
89 bool operator==(const OrderGlobalInits &RHS) const {
90 return priority == RHS.priority &&
91 lex_order == RHS.lex_order;
94 bool operator<(const OrderGlobalInits &RHS) const {
95 if (priority < RHS.priority)
98 return priority == RHS.priority && lex_order < RHS.lex_order;
102 struct CodeGenTypeCache {
106 /// i8, i16, i32, and i64
107 llvm::IntegerType *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty;
109 llvm::Type *FloatTy, *DoubleTy;
112 llvm::IntegerType *IntTy;
114 /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size.
116 llvm::IntegerType *IntPtrTy;
117 llvm::IntegerType *SizeTy;
118 llvm::IntegerType *PtrDiffTy;
121 /// void* in address space 0
123 llvm::PointerType *VoidPtrTy;
124 llvm::PointerType *Int8PtrTy;
127 /// void** in address space 0
129 llvm::PointerType *VoidPtrPtrTy;
130 llvm::PointerType *Int8PtrPtrTy;
133 /// The width of a pointer into the generic address space.
134 unsigned char PointerWidthInBits;
136 /// The size and alignment of a pointer into the generic address
139 unsigned char PointerAlignInBytes;
140 unsigned char PointerSizeInBytes;
141 unsigned char SizeSizeInBytes; // sizeof(size_t)
145 struct RREntrypoints {
146 RREntrypoints() { memset(this, 0, sizeof(*this)); }
147 /// void objc_autoreleasePoolPop(void*);
148 llvm::Constant *objc_autoreleasePoolPop;
150 /// void *objc_autoreleasePoolPush(void);
151 llvm::Constant *objc_autoreleasePoolPush;
154 struct ARCEntrypoints {
155 ARCEntrypoints() { memset(this, 0, sizeof(*this)); }
157 /// id objc_autorelease(id);
158 llvm::Constant *objc_autorelease;
160 /// id objc_autoreleaseReturnValue(id);
161 llvm::Constant *objc_autoreleaseReturnValue;
163 /// void objc_copyWeak(id *dest, id *src);
164 llvm::Constant *objc_copyWeak;
166 /// void objc_destroyWeak(id*);
167 llvm::Constant *objc_destroyWeak;
169 /// id objc_initWeak(id*, id);
170 llvm::Constant *objc_initWeak;
172 /// id objc_loadWeak(id*);
173 llvm::Constant *objc_loadWeak;
175 /// id objc_loadWeakRetained(id*);
176 llvm::Constant *objc_loadWeakRetained;
178 /// void objc_moveWeak(id *dest, id *src);
179 llvm::Constant *objc_moveWeak;
181 /// id objc_retain(id);
182 llvm::Constant *objc_retain;
184 /// id objc_retainAutorelease(id);
185 llvm::Constant *objc_retainAutorelease;
187 /// id objc_retainAutoreleaseReturnValue(id);
188 llvm::Constant *objc_retainAutoreleaseReturnValue;
190 /// id objc_retainAutoreleasedReturnValue(id);
191 llvm::Constant *objc_retainAutoreleasedReturnValue;
193 /// id objc_retainBlock(id);
194 llvm::Constant *objc_retainBlock;
196 /// void objc_release(id);
197 llvm::Constant *objc_release;
199 /// id objc_storeStrong(id*, id);
200 llvm::Constant *objc_storeStrong;
202 /// id objc_storeWeak(id*, id);
203 llvm::Constant *objc_storeWeak;
205 /// A void(void) inline asm to use to mark that the return value of
206 /// a call will be immediately retain.
207 llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
210 /// CodeGenModule - This class organizes the cross-function state that is used
211 /// while generating LLVM code.
212 class CodeGenModule : public CodeGenTypeCache {
213 CodeGenModule(const CodeGenModule &) LLVM_DELETED_FUNCTION;
214 void operator=(const CodeGenModule &) LLVM_DELETED_FUNCTION;
216 typedef std::vector<std::pair<llvm::Constant*, int> > CtorList;
219 const LangOptions &LangOpts;
220 const CodeGenOptions &CodeGenOpts;
221 llvm::Module &TheModule;
222 const llvm::DataLayout &TheDataLayout;
223 mutable const TargetCodeGenInfo *TheTargetCodeGenInfo;
224 DiagnosticsEngine &Diags;
229 /// VTables - Holds information about C++ vtables.
230 CodeGenVTables VTables;
231 friend class CodeGenVTables;
233 CGObjCRuntime* ObjCRuntime;
234 CGOpenCLRuntime* OpenCLRuntime;
235 CGCUDARuntime* CUDARuntime;
236 CGDebugInfo* DebugInfo;
237 ARCEntrypoints *ARCData;
238 llvm::MDNode *NoObjCARCExceptionsMetadata;
239 RREntrypoints *RRData;
241 // WeakRefReferences - A set of references that have only been seen via
242 // a weakref so far. This is used to remove the weak of the reference if we ever
243 // see a direct reference or a definition.
244 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
246 /// DeferredDecls - This contains all the decls which have definitions but
247 /// which are deferred for emission and therefore should only be output if
248 /// they are actually used. If a decl is in this, then it is known to have
249 /// not been referenced yet.
250 llvm::StringMap<GlobalDecl> DeferredDecls;
252 /// DeferredDeclsToEmit - This is a list of deferred decls which we have seen
253 /// that *are* actually referenced. These get code generated when the module
255 std::vector<GlobalDecl> DeferredDeclsToEmit;
257 /// LLVMUsed - List of global values which are required to be
258 /// present in the object file; bitcast to i8*. This is used for
259 /// forcing visibility of symbols which may otherwise be optimized
261 std::vector<llvm::WeakVH> LLVMUsed;
263 /// GlobalCtors - Store the list of global constructors and their respective
264 /// priorities to be emitted when the translation unit is complete.
265 CtorList GlobalCtors;
267 /// GlobalDtors - Store the list of global destructors and their respective
268 /// priorities to be emitted when the translation unit is complete.
269 CtorList GlobalDtors;
271 /// MangledDeclNames - A map of canonical GlobalDecls to their mangled names.
272 llvm::DenseMap<GlobalDecl, StringRef> MangledDeclNames;
273 llvm::BumpPtrAllocator MangledNamesAllocator;
275 /// Global annotations.
276 std::vector<llvm::Constant*> Annotations;
278 /// Map used to get unique annotation strings.
279 llvm::StringMap<llvm::Constant*> AnnotationStrings;
281 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
282 llvm::StringMap<llvm::GlobalVariable*> ConstantStringMap;
283 llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
284 llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
286 llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
287 llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
289 /// CXXGlobalInits - Global variables with initializers that need to run
291 std::vector<llvm::Constant*> CXXGlobalInits;
293 /// When a C++ decl with an initializer is deferred, null is
294 /// appended to CXXGlobalInits, and the index of that null is placed
295 /// here so that the initializer will be performed in the correct
297 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
299 typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData;
301 struct GlobalInitPriorityCmp {
302 bool operator()(const GlobalInitData &LHS,
303 const GlobalInitData &RHS) const {
304 return LHS.first.priority < RHS.first.priority;
308 /// - Global variables with initializers whose order of initialization
309 /// is set by init_priority attribute.
310 SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
312 /// CXXGlobalDtors - Global destructor functions and arguments that need to
313 /// run on termination.
314 std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
316 /// @name Cache for Objective-C runtime types
319 /// CFConstantStringClassRef - Cached reference to the class for constant
320 /// strings. This value has type int * but is actually an Obj-C class pointer.
321 llvm::Constant *CFConstantStringClassRef;
323 /// ConstantStringClassRef - Cached reference to the class for constant
324 /// strings. This value has type int * but is actually an Obj-C class pointer.
325 llvm::Constant *ConstantStringClassRef;
327 /// \brief The LLVM type corresponding to NSConstantString.
328 llvm::StructType *NSConstantStringType;
330 /// \brief The type used to describe the state of a fast enumeration in
331 /// Objective-C's for..in loop.
332 QualType ObjCFastEnumerationStateType;
336 /// Lazily create the Objective-C runtime
337 void createObjCRuntime();
339 void createOpenCLRuntime();
340 void createCUDARuntime();
342 bool isTriviallyRecursive(const FunctionDecl *F);
343 bool shouldEmitFunction(const FunctionDecl *F);
344 llvm::LLVMContext &VMContext;
346 /// @name Cache for Blocks Runtime Globals
349 llvm::Constant *NSConcreteGlobalBlock;
350 llvm::Constant *NSConcreteStackBlock;
352 llvm::Constant *BlockObjectAssign;
353 llvm::Constant *BlockObjectDispose;
355 llvm::Type *BlockDescriptorType;
356 llvm::Type *GenericBlockLiteralType;
359 int GlobalUniqueCount;
362 GlobalDecl initializedGlobalDecl;
366 CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,
367 llvm::Module &M, const llvm::DataLayout &TD,
368 DiagnosticsEngine &Diags);
372 /// Release - Finalize LLVM code generation.
375 /// getObjCRuntime() - Return a reference to the configured
376 /// Objective-C runtime.
377 CGObjCRuntime &getObjCRuntime() {
378 if (!ObjCRuntime) createObjCRuntime();
382 /// hasObjCRuntime() - Return true iff an Objective-C runtime has
384 bool hasObjCRuntime() { return !!ObjCRuntime; }
386 /// getOpenCLRuntime() - Return a reference to the configured OpenCL runtime.
387 CGOpenCLRuntime &getOpenCLRuntime() {
388 assert(OpenCLRuntime != 0);
389 return *OpenCLRuntime;
392 /// getCUDARuntime() - Return a reference to the configured CUDA runtime.
393 CGCUDARuntime &getCUDARuntime() {
394 assert(CUDARuntime != 0);
398 /// getCXXABI() - Return a reference to the configured C++ ABI.
399 CGCXXABI &getCXXABI() { return ABI; }
401 ARCEntrypoints &getARCEntrypoints() const {
402 assert(getLangOpts().ObjCAutoRefCount && ARCData != 0);
406 RREntrypoints &getRREntrypoints() const {
411 llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
412 return StaticLocalDeclMap[D];
414 void setStaticLocalDeclAddress(const VarDecl *D,
416 StaticLocalDeclMap[D] = C;
419 llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
420 return StaticLocalDeclGuardMap[D];
422 void setStaticLocalDeclGuardAddress(const VarDecl *D,
423 llvm::GlobalVariable *C) {
424 StaticLocalDeclGuardMap[D] = C;
427 llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
428 return AtomicSetterHelperFnMap[Ty];
430 void setAtomicSetterHelperFnMap(QualType Ty,
431 llvm::Constant *Fn) {
432 AtomicSetterHelperFnMap[Ty] = Fn;
435 llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
436 return AtomicGetterHelperFnMap[Ty];
438 void setAtomicGetterHelperFnMap(QualType Ty,
439 llvm::Constant *Fn) {
440 AtomicGetterHelperFnMap[Ty] = Fn;
443 CGDebugInfo *getModuleDebugInfo() { return DebugInfo; }
445 llvm::MDNode *getNoObjCARCExceptionsMetadata() {
446 if (!NoObjCARCExceptionsMetadata)
447 NoObjCARCExceptionsMetadata =
448 llvm::MDNode::get(getLLVMContext(),
449 SmallVector<llvm::Value*,1>());
450 return NoObjCARCExceptionsMetadata;
453 ASTContext &getContext() const { return Context; }
454 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
455 const LangOptions &getLangOpts() const { return LangOpts; }
456 llvm::Module &getModule() const { return TheModule; }
457 CodeGenTypes &getTypes() { return Types; }
458 CodeGenVTables &getVTables() { return VTables; }
459 VTableContext &getVTableContext() { return VTables.getVTableContext(); }
460 DiagnosticsEngine &getDiags() const { return Diags; }
461 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
462 const TargetInfo &getTarget() const { return Context.getTargetInfo(); }
463 llvm::LLVMContext &getLLVMContext() { return VMContext; }
464 const TargetCodeGenInfo &getTargetCodeGenInfo();
465 bool isTargetDarwin() const;
467 bool shouldUseTBAA() const { return TBAA != 0; }
469 llvm::MDNode *getTBAAInfo(QualType QTy);
470 llvm::MDNode *getTBAAInfoForVTablePtr();
471 llvm::MDNode *getTBAAStructInfo(QualType QTy);
473 bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
475 static void DecorateInstruction(llvm::Instruction *Inst,
476 llvm::MDNode *TBAAInfo);
478 /// getSize - Emit the given number of characters as a value of type size_t.
479 llvm::ConstantInt *getSize(CharUnits numChars);
481 /// setGlobalVisibility - Set the visibility for the given LLVM
483 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
485 /// setTLSMode - Set the TLS mode for the given LLVM GlobalVariable
486 /// for the thread-local variable declaration D.
487 void setTLSMode(llvm::GlobalVariable *GV, const VarDecl &D) const;
489 /// TypeVisibilityKind - The kind of global variable that is passed to
490 /// setTypeVisibility
491 enum TypeVisibilityKind {
494 TVK_ForConstructionVTable,
499 /// setTypeVisibility - Set the visibility for the given global
500 /// value which holds information about a type.
501 void setTypeVisibility(llvm::GlobalValue *GV, const CXXRecordDecl *D,
502 TypeVisibilityKind TVK) const;
504 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
506 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
507 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
508 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
510 llvm_unreachable("unknown visibility!");
513 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) {
514 if (isa<CXXConstructorDecl>(GD.getDecl()))
515 return GetAddrOfCXXConstructor(cast<CXXConstructorDecl>(GD.getDecl()),
517 else if (isa<CXXDestructorDecl>(GD.getDecl()))
518 return GetAddrOfCXXDestructor(cast<CXXDestructorDecl>(GD.getDecl()),
520 else if (isa<FunctionDecl>(GD.getDecl()))
521 return GetAddrOfFunction(GD);
523 return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl()));
526 /// CreateOrReplaceCXXRuntimeVariable - Will return a global variable of the given
527 /// type. If a variable with a different type already exists then a new
528 /// variable with the right type will be created and all uses of the old
529 /// variable will be replaced with a bitcast to the new variable.
530 llvm::GlobalVariable *
531 CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
532 llvm::GlobalValue::LinkageTypes Linkage);
534 /// GetGlobalVarAddressSpace - Return the address space of the underlying
535 /// global variable for D, as determined by its declaration. Normally this
536 /// is the same as the address space of D's type, but in CUDA, address spaces
537 /// are associated with declarations, not types.
538 unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace);
540 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
541 /// given global variable. If Ty is non-null and if the global doesn't exist,
542 /// then it will be greated with the specified type instead of whatever the
543 /// normal requested type would be.
544 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
548 /// GetAddrOfFunction - Return the address of the given function. If Ty is
549 /// non-null, then this function will use the specified type if it has to
551 llvm::Constant *GetAddrOfFunction(GlobalDecl GD,
553 bool ForVTable = false);
555 /// GetAddrOfRTTIDescriptor - Get the address of the RTTI descriptor
556 /// for the given type.
557 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
559 /// GetAddrOfUuidDescriptor - Get the address of a uuid descriptor .
560 llvm::Constant *GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
562 /// GetAddrOfThunk - Get the address of the thunk for the given global decl.
563 llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
565 /// GetWeakRefReference - Get a reference to the target of VD.
566 llvm::Constant *GetWeakRefReference(const ValueDecl *VD);
568 /// GetNonVirtualBaseClassOffset - Returns the offset from a derived class to
569 /// a class. Returns null if the offset is 0.
571 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
572 CastExpr::path_const_iterator PathBegin,
573 CastExpr::path_const_iterator PathEnd);
575 /// A pair of helper functions for a __block variable.
576 class ByrefHelpers : public llvm::FoldingSetNode {
578 llvm::Constant *CopyHelper;
579 llvm::Constant *DisposeHelper;
581 /// The alignment of the field. This is important because
582 /// different offsets to the field within the byref struct need to
583 /// have different helper functions.
586 ByrefHelpers(CharUnits alignment) : Alignment(alignment) {}
587 virtual ~ByrefHelpers();
589 void Profile(llvm::FoldingSetNodeID &id) const {
590 id.AddInteger(Alignment.getQuantity());
593 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
595 virtual bool needsCopy() const { return true; }
596 virtual void emitCopy(CodeGenFunction &CGF,
597 llvm::Value *dest, llvm::Value *src) = 0;
599 virtual bool needsDispose() const { return true; }
600 virtual void emitDispose(CodeGenFunction &CGF, llvm::Value *field) = 0;
603 llvm::FoldingSet<ByrefHelpers> ByrefHelpersCache;
605 /// getUniqueBlockCount - Fetches the global unique block count.
606 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
608 /// getBlockDescriptorType - Fetches the type of a generic block
610 llvm::Type *getBlockDescriptorType();
612 /// getGenericBlockLiteralType - The type of a generic block literal.
613 llvm::Type *getGenericBlockLiteralType();
615 /// GetAddrOfGlobalBlock - Gets the address of a block which
616 /// requires no captures.
617 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
619 /// GetAddrOfConstantCFString - Return a pointer to a constant CFString object
620 /// for the given string.
621 llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal);
623 /// GetAddrOfConstantString - Return a pointer to a constant NSString object
624 /// for the given string. Or a user defined String object as defined via
625 /// -fconstant-string-class=class_name option.
626 llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal);
628 /// GetConstantArrayFromStringLiteral - Return a constant array for the given
630 llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
632 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a constant array
633 /// for the given string literal.
634 llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
636 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
637 /// array for the given ObjCEncodeExpr node.
638 llvm::Constant *GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
640 /// GetAddrOfConstantString - Returns a pointer to a character array
641 /// containing the literal. This contents are exactly that of the given
642 /// string, i.e. it will not be null terminated automatically; see
643 /// GetAddrOfConstantCString. Note that whether the result is actually a
644 /// pointer to an LLVM constant depends on Feature.WriteableStrings.
646 /// The result has pointer to array type.
648 /// \param GlobalName If provided, the name to use for the global
649 /// (if one is created).
650 llvm::Constant *GetAddrOfConstantString(StringRef Str,
651 const char *GlobalName=0,
652 unsigned Alignment=1);
654 /// GetAddrOfConstantCString - Returns a pointer to a character array
655 /// containing the literal and a terminating '\0' character. The result has
656 /// pointer to array type.
658 /// \param GlobalName If provided, the name to use for the global (if one is
660 llvm::Constant *GetAddrOfConstantCString(const std::string &str,
661 const char *GlobalName=0,
662 unsigned Alignment=1);
664 /// GetAddrOfConstantCompoundLiteral - Returns a pointer to a constant global
665 /// variable for the given file-scope compound literal expression.
666 llvm::Constant *GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
668 /// \brief Retrieve the record type that describes the state of an
669 /// Objective-C fast enumeration loop (for..in).
670 QualType getObjCFastEnumerationStateType();
672 /// GetAddrOfCXXConstructor - Return the address of the constructor of the
674 llvm::GlobalValue *GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor,
675 CXXCtorType ctorType,
676 const CGFunctionInfo *fnInfo = 0);
678 /// GetAddrOfCXXDestructor - Return the address of the constructor of the
680 llvm::GlobalValue *GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
681 CXXDtorType dtorType,
682 const CGFunctionInfo *fnInfo = 0);
684 /// getBuiltinLibFunction - Given a builtin id for a function like
685 /// "__builtin_fabsf", return a Function* for "fabsf".
686 llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
689 llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys =
690 ArrayRef<llvm::Type*>());
692 /// EmitTopLevelDecl - Emit code for a single top level declaration.
693 void EmitTopLevelDecl(Decl *D);
695 /// HandleCXXStaticMemberVarInstantiation - Tell the consumer that this
696 // variable has been instantiated.
697 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
699 /// AddUsedGlobal - Add a global which should be forced to be
700 /// present in the object file; these are emitted to the llvm.used
702 void AddUsedGlobal(llvm::GlobalValue *GV);
704 /// AddCXXDtorEntry - Add a destructor and object to add to the C++ global
705 /// destructor function.
706 void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) {
707 CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object));
710 /// CreateRuntimeFunction - Create a new runtime function with the specified
712 llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty,
714 llvm::Attributes ExtraAttrs =
716 /// CreateRuntimeVariable - Create a new runtime global variable with the
717 /// specified type and name.
718 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
721 ///@name Custom Blocks Runtime Interfaces
724 llvm::Constant *getNSConcreteGlobalBlock();
725 llvm::Constant *getNSConcreteStackBlock();
726 llvm::Constant *getBlockObjectAssign();
727 llvm::Constant *getBlockObjectDispose();
731 // UpdateCompleteType - Make sure that this type is translated.
732 void UpdateCompletedType(const TagDecl *TD);
734 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
736 /// EmitConstantInit - Try to emit the initializer for the given declaration
737 /// as a constant; returns 0 if the expression cannot be emitted as a
739 llvm::Constant *EmitConstantInit(const VarDecl &D, CodeGenFunction *CGF = 0);
741 /// EmitConstantExpr - Try to emit the given expression as a
742 /// constant; returns 0 if the expression cannot be emitted as a
744 llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType,
745 CodeGenFunction *CGF = 0);
747 /// EmitConstantValue - Emit the given constant value as a constant, in the
748 /// type's scalar representation.
749 llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType,
750 CodeGenFunction *CGF = 0);
752 /// EmitConstantValueForMemory - Emit the given constant value as a constant,
753 /// in the type's memory representation.
754 llvm::Constant *EmitConstantValueForMemory(const APValue &Value,
756 CodeGenFunction *CGF = 0);
758 /// EmitNullConstant - Return the result of value-initializing the given
759 /// type, i.e. a null expression of the given type. This is usually,
760 /// but not always, an LLVM null constant.
761 llvm::Constant *EmitNullConstant(QualType T);
763 /// EmitNullConstantForBase - Return a null constant appropriate for
764 /// zero-initializing a base class with the given type. This is usually,
765 /// but not always, an LLVM null constant.
766 llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
768 /// Error - Emit a general error that something can't be done.
769 void Error(SourceLocation loc, StringRef error);
771 /// ErrorUnsupported - Print out an error that codegen doesn't support the
772 /// specified stmt yet.
773 /// \param OmitOnError - If true, then this error should only be emitted if no
774 /// other errors have been reported.
775 void ErrorUnsupported(const Stmt *S, const char *Type,
776 bool OmitOnError=false);
778 /// ErrorUnsupported - Print out an error that codegen doesn't support the
779 /// specified decl yet.
780 /// \param OmitOnError - If true, then this error should only be emitted if no
781 /// other errors have been reported.
782 void ErrorUnsupported(const Decl *D, const char *Type,
783 bool OmitOnError=false);
785 /// SetInternalFunctionAttributes - Set the attributes on the LLVM
786 /// function for the given decl and function info. This applies
787 /// attributes necessary for handling the ABI as well as user
788 /// specified attributes like section.
789 void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F,
790 const CGFunctionInfo &FI);
792 /// SetLLVMFunctionAttributes - Set the LLVM function attributes
793 /// (sext, zext, etc).
794 void SetLLVMFunctionAttributes(const Decl *D,
795 const CGFunctionInfo &Info,
798 /// SetLLVMFunctionAttributesForDefinition - Set the LLVM function attributes
799 /// which only apply to a function definintion.
800 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
802 /// ReturnTypeUsesSRet - Return true iff the given type uses 'sret' when used
803 /// as a return type.
804 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
806 /// ReturnTypeUsesFPRet - Return true iff the given type uses 'fpret' when
807 /// used as a return type.
808 bool ReturnTypeUsesFPRet(QualType ResultType);
810 /// ReturnTypeUsesFP2Ret - Return true iff the given type uses 'fp2ret' when
811 /// used as a return type.
812 bool ReturnTypeUsesFP2Ret(QualType ResultType);
814 /// ConstructAttributeList - Get the LLVM attributes and calling convention to
815 /// use for a particular function type.
817 /// \param Info - The function type information.
818 /// \param TargetDecl - The decl these attributes are being constructed
819 /// for. If supplied the attributes applied to this decl may contribute to the
820 /// function attributes and calling convention.
821 /// \param PAL [out] - On return, the attribute list to use.
822 /// \param CallingConv [out] - On return, the LLVM calling convention to use.
823 void ConstructAttributeList(const CGFunctionInfo &Info,
824 const Decl *TargetDecl,
825 AttributeListType &PAL,
826 unsigned &CallingConv);
828 StringRef getMangledName(GlobalDecl GD);
829 void getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer,
830 const BlockDecl *BD);
832 void EmitTentativeDefinition(const VarDecl *D);
834 void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired);
836 llvm::GlobalVariable::LinkageTypes
837 getFunctionLinkage(const FunctionDecl *FD);
839 void setFunctionLinkage(const FunctionDecl *FD, llvm::GlobalValue *V) {
840 V->setLinkage(getFunctionLinkage(FD));
843 /// getVTableLinkage - Return the appropriate linkage for the vtable, VTT,
844 /// and type information of the given class.
845 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
847 /// GetTargetTypeStoreSize - Return the store size, in character units, of
848 /// the given LLVM type.
849 CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
851 /// GetLLVMLinkageVarDefinition - Returns LLVM linkage for a global
853 llvm::GlobalValue::LinkageTypes
854 GetLLVMLinkageVarDefinition(const VarDecl *D,
855 llvm::GlobalVariable *GV);
857 std::vector<const CXXRecordDecl*> DeferredVTables;
859 /// Emit all the global annotations.
860 void EmitGlobalAnnotations();
862 /// Emit an annotation string.
863 llvm::Constant *EmitAnnotationString(llvm::StringRef Str);
865 /// Emit the annotation's translation unit.
866 llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
868 /// Emit the annotation line number.
869 llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
871 /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the
872 /// annotation information for a given GlobalValue. The annotation struct is
873 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
874 /// GlobalValue being annotated. The second field is the constant string
875 /// created from the AnnotateAttr's annotation. The third field is a constant
876 /// string containing the name of the translation unit. The fourth field is
877 /// the line number in the file of the annotated value declaration.
878 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
879 const AnnotateAttr *AA,
882 /// Add global annotations that are set on D, for the global GV. Those
883 /// annotations are emitted during finalization of the LLVM code.
884 void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
887 llvm::GlobalValue *GetGlobalValue(StringRef Ref);
889 llvm::Constant *GetOrCreateLLVMFunction(StringRef MangledName,
893 llvm::Attributes ExtraAttrs =
895 llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
896 llvm::PointerType *PTy,
898 bool UnnamedAddr = false);
900 /// SetCommonAttributes - Set attributes which are common to any
901 /// form of a global definition (alias, Objective-C method,
902 /// function, global variable).
904 /// NOTE: This should only be called for definitions.
905 void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV);
907 /// SetFunctionDefinitionAttributes - Set attributes for a global definition.
908 void SetFunctionDefinitionAttributes(const FunctionDecl *D,
909 llvm::GlobalValue *GV);
911 /// SetFunctionAttributes - Set function attributes for a function
913 void SetFunctionAttributes(GlobalDecl GD,
915 bool IsIncompleteFunction);
917 /// EmitGlobal - Emit code for a singal global function or var decl. Forward
918 /// declarations are emitted lazily.
919 void EmitGlobal(GlobalDecl D);
921 void EmitGlobalDefinition(GlobalDecl D);
923 void EmitGlobalFunctionDefinition(GlobalDecl GD);
924 void EmitGlobalVarDefinition(const VarDecl *D);
925 llvm::Constant *MaybeEmitGlobalStdInitializerListInitializer(const VarDecl *D,
927 void EmitAliasDefinition(GlobalDecl GD);
928 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
929 void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
931 // C++ related functions.
933 bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target);
934 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
936 void EmitNamespace(const NamespaceDecl *D);
937 void EmitLinkageSpec(const LinkageSpecDecl *D);
939 /// EmitCXXConstructors - Emit constructors (base, complete) from a
940 /// C++ constructor Decl.
941 void EmitCXXConstructors(const CXXConstructorDecl *D);
943 /// EmitCXXConstructor - Emit a single constructor with the given type from
944 /// a C++ constructor Decl.
945 void EmitCXXConstructor(const CXXConstructorDecl *D, CXXCtorType Type);
947 /// EmitCXXDestructors - Emit destructors (base, complete) from a
948 /// C++ destructor Decl.
949 void EmitCXXDestructors(const CXXDestructorDecl *D);
951 /// EmitCXXDestructor - Emit a single destructor with the given type from
952 /// a C++ destructor Decl.
953 void EmitCXXDestructor(const CXXDestructorDecl *D, CXXDtorType Type);
955 /// EmitCXXGlobalInitFunc - Emit the function that initializes C++ globals.
956 void EmitCXXGlobalInitFunc();
958 /// EmitCXXGlobalDtorFunc - Emit the function that destroys C++ globals.
959 void EmitCXXGlobalDtorFunc();
961 /// EmitCXXGlobalVarDeclInitFunc - Emit the function that initializes the
962 /// specified global (if PerformInit is true) and registers its destructor.
963 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
964 llvm::GlobalVariable *Addr,
967 // FIXME: Hardcoding priority here is gross.
968 void AddGlobalCtor(llvm::Function *Ctor, int Priority=65535);
969 void AddGlobalDtor(llvm::Function *Dtor, int Priority=65535);
971 /// EmitCtorList - Generates a global array of functions and priorities using
972 /// the given list and name. This array will have appending linkage and is
973 /// suitable for use as a LLVM constructor or destructor array.
974 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
976 /// EmitFundamentalRTTIDescriptor - Emit the RTTI descriptors for the
978 void EmitFundamentalRTTIDescriptor(QualType Type);
980 /// EmitFundamentalRTTIDescriptors - Emit the RTTI descriptors for the
982 void EmitFundamentalRTTIDescriptors();
984 /// EmitDeferred - Emit any needed decls for which code generation
986 void EmitDeferred(void);
988 /// EmitLLVMUsed - Emit the llvm.used metadata used to force
989 /// references to global which may otherwise be optimized out.
990 void EmitLLVMUsed(void);
992 void EmitDeclMetadata();
994 /// EmitCoverageFile - Emit the llvm.gcov metadata used to tell LLVM where
995 /// to emit the .gcno and .gcda files in a way that persists in .bc files.
996 void EmitCoverageFile();
998 /// Emits the initializer for a uuidof string.
999 llvm::Constant *EmitUuidofInitializer(StringRef uuidstr, QualType IIDType);
1001 /// MayDeferGeneration - Determine if the given decl can be emitted
1002 /// lazily; this is only relevant for definitions. The given decl
1003 /// must be either a function or var decl.
1004 bool MayDeferGeneration(const ValueDecl *D);
1006 /// SimplifyPersonality - Check whether we can use a "simpler", more
1007 /// core exceptions personality function.
1008 void SimplifyPersonality();
1010 } // end namespace CodeGen
1011 } // end namespace clang