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/StringSet.h"
30 #include "llvm/ADT/SmallPtrSet.h"
31 #include "llvm/Support/ValueHandle.h"
45 class TargetCodeGenInfo;
50 class ObjCImplementationDecl;
51 class ObjCCategoryImplDecl;
52 class ObjCProtocolDecl;
67 class CXXDestructorDecl;
73 class CodeGenFunction;
78 class BlockFieldFlags;
79 class FunctionArgList;
81 struct OrderGlobalInits {
82 unsigned int priority;
83 unsigned int lex_order;
84 OrderGlobalInits(unsigned int p, unsigned int l)
85 : priority(p), lex_order(l) {}
87 bool operator==(const OrderGlobalInits &RHS) const {
88 return priority == RHS.priority &&
89 lex_order == RHS.lex_order;
92 bool operator<(const OrderGlobalInits &RHS) const {
93 if (priority < RHS.priority)
96 return priority == RHS.priority && lex_order < RHS.lex_order;
100 struct CodeGenTypeCache {
105 llvm::IntegerType *Int8Ty, *Int32Ty, *Int64Ty;
108 llvm::IntegerType *IntTy;
110 /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size.
112 llvm::IntegerType *IntPtrTy;
113 llvm::IntegerType *SizeTy;
114 llvm::IntegerType *PtrDiffTy;
117 /// void* in address space 0
119 llvm::PointerType *VoidPtrTy;
120 llvm::PointerType *Int8PtrTy;
123 /// void** in address space 0
125 llvm::PointerType *VoidPtrPtrTy;
126 llvm::PointerType *Int8PtrPtrTy;
129 /// The width of a pointer into the generic address space.
130 unsigned char PointerWidthInBits;
132 /// The alignment of a pointer into the generic address space.
133 unsigned char PointerAlignInBytes;
136 struct RREntrypoints {
137 RREntrypoints() { memset(this, 0, sizeof(*this)); }
138 /// void objc_autoreleasePoolPop(void*);
139 llvm::Constant *objc_autoreleasePoolPop;
141 /// void *objc_autoreleasePoolPush(void);
142 llvm::Constant *objc_autoreleasePoolPush;
145 struct ARCEntrypoints {
146 ARCEntrypoints() { memset(this, 0, sizeof(*this)); }
148 /// id objc_autorelease(id);
149 llvm::Constant *objc_autorelease;
151 /// id objc_autoreleaseReturnValue(id);
152 llvm::Constant *objc_autoreleaseReturnValue;
154 /// void objc_copyWeak(id *dest, id *src);
155 llvm::Constant *objc_copyWeak;
157 /// void objc_destroyWeak(id*);
158 llvm::Constant *objc_destroyWeak;
160 /// id objc_initWeak(id*, id);
161 llvm::Constant *objc_initWeak;
163 /// id objc_loadWeak(id*);
164 llvm::Constant *objc_loadWeak;
166 /// id objc_loadWeakRetained(id*);
167 llvm::Constant *objc_loadWeakRetained;
169 /// void objc_moveWeak(id *dest, id *src);
170 llvm::Constant *objc_moveWeak;
172 /// id objc_retain(id);
173 llvm::Constant *objc_retain;
175 /// id objc_retainAutorelease(id);
176 llvm::Constant *objc_retainAutorelease;
178 /// id objc_retainAutoreleaseReturnValue(id);
179 llvm::Constant *objc_retainAutoreleaseReturnValue;
181 /// id objc_retainAutoreleasedReturnValue(id);
182 llvm::Constant *objc_retainAutoreleasedReturnValue;
184 /// id objc_retainBlock(id);
185 llvm::Constant *objc_retainBlock;
187 /// void objc_release(id);
188 llvm::Constant *objc_release;
190 /// id objc_storeStrong(id*, id);
191 llvm::Constant *objc_storeStrong;
193 /// id objc_storeWeak(id*, id);
194 llvm::Constant *objc_storeWeak;
196 /// A void(void) inline asm to use to mark that the return value of
197 /// a call will be immediately retain.
198 llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
201 /// CodeGenModule - This class organizes the cross-function state that is used
202 /// while generating LLVM code.
203 class CodeGenModule : public CodeGenTypeCache {
204 CodeGenModule(const CodeGenModule&); // DO NOT IMPLEMENT
205 void operator=(const CodeGenModule&); // DO NOT IMPLEMENT
207 typedef std::vector<std::pair<llvm::Constant*, int> > CtorList;
210 const LangOptions &Features;
211 const CodeGenOptions &CodeGenOpts;
212 llvm::Module &TheModule;
213 const llvm::TargetData &TheTargetData;
214 mutable const TargetCodeGenInfo *TheTargetCodeGenInfo;
220 /// VTables - Holds information about C++ vtables.
221 CodeGenVTables VTables;
222 friend class CodeGenVTables;
224 CGObjCRuntime* Runtime;
225 CGDebugInfo* DebugInfo;
226 ARCEntrypoints *ARCData;
227 RREntrypoints *RRData;
229 // WeakRefReferences - A set of references that have only been seen via
230 // a weakref so far. This is used to remove the weak of the reference if we ever
231 // see a direct reference or a definition.
232 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
234 /// DeferredDecls - This contains all the decls which have definitions but
235 /// which are deferred for emission and therefore should only be output if
236 /// they are actually used. If a decl is in this, then it is known to have
237 /// not been referenced yet.
238 llvm::StringMap<GlobalDecl> DeferredDecls;
240 /// DeferredDeclsToEmit - This is a list of deferred decls which we have seen
241 /// that *are* actually referenced. These get code generated when the module
243 std::vector<GlobalDecl> DeferredDeclsToEmit;
245 /// LLVMUsed - List of global values which are required to be
246 /// present in the object file; bitcast to i8*. This is used for
247 /// forcing visibility of symbols which may otherwise be optimized
249 std::vector<llvm::WeakVH> LLVMUsed;
251 /// GlobalCtors - Store the list of global constructors and their respective
252 /// priorities to be emitted when the translation unit is complete.
253 CtorList GlobalCtors;
255 /// GlobalDtors - Store the list of global destructors and their respective
256 /// priorities to be emitted when the translation unit is complete.
257 CtorList GlobalDtors;
259 /// MangledDeclNames - A map of canonical GlobalDecls to their mangled names.
260 llvm::DenseMap<GlobalDecl, llvm::StringRef> MangledDeclNames;
261 llvm::BumpPtrAllocator MangledNamesAllocator;
263 std::vector<llvm::Constant*> Annotations;
265 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
266 llvm::StringMap<llvm::Constant*> ConstantStringMap;
267 llvm::DenseMap<const Decl*, llvm::Value*> StaticLocalDeclMap;
269 /// CXXGlobalInits - Global variables with initializers that need to run
271 std::vector<llvm::Constant*> CXXGlobalInits;
273 /// When a C++ decl with an initializer is deferred, null is
274 /// appended to CXXGlobalInits, and the index of that null is placed
275 /// here so that the initializer will be performed in the correct
277 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
279 /// - Global variables with initializers whose order of initialization
280 /// is set by init_priority attribute.
282 llvm::SmallVector<std::pair<OrderGlobalInits, llvm::Function*>, 8>
283 PrioritizedCXXGlobalInits;
285 /// CXXGlobalDtors - Global destructor functions and arguments that need to
286 /// run on termination.
287 std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
289 /// CFConstantStringClassRef - Cached reference to the class for constant
290 /// strings. This value has type int * but is actually an Obj-C class pointer.
291 llvm::Constant *CFConstantStringClassRef;
293 /// ConstantStringClassRef - Cached reference to the class for constant
294 /// strings. This value has type int * but is actually an Obj-C class pointer.
295 llvm::Constant *ConstantStringClassRef;
297 /// Lazily create the Objective-C runtime
298 void createObjCRuntime();
300 llvm::LLVMContext &VMContext;
302 /// @name Cache for Blocks Runtime Globals
305 const VarDecl *NSConcreteGlobalBlockDecl;
306 const VarDecl *NSConcreteStackBlockDecl;
307 llvm::Constant *NSConcreteGlobalBlock;
308 llvm::Constant *NSConcreteStackBlock;
310 const FunctionDecl *BlockObjectAssignDecl;
311 const FunctionDecl *BlockObjectDisposeDecl;
312 llvm::Constant *BlockObjectAssign;
313 llvm::Constant *BlockObjectDispose;
315 llvm::Type *BlockDescriptorType;
316 llvm::Type *GenericBlockLiteralType;
319 int GlobalUniqueCount;
324 CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,
325 llvm::Module &M, const llvm::TargetData &TD, Diagnostic &Diags);
329 /// Release - Finalize LLVM code generation.
332 /// getObjCRuntime() - Return a reference to the configured
333 /// Objective-C runtime.
334 CGObjCRuntime &getObjCRuntime() {
335 if (!Runtime) createObjCRuntime();
339 /// hasObjCRuntime() - Return true iff an Objective-C runtime has
341 bool hasObjCRuntime() { return !!Runtime; }
343 /// getCXXABI() - Return a reference to the configured C++ ABI.
344 CGCXXABI &getCXXABI() { return ABI; }
346 ARCEntrypoints &getARCEntrypoints() const {
347 assert(getLangOptions().ObjCAutoRefCount && ARCData != 0);
351 RREntrypoints &getRREntrypoints() const {
356 llvm::Value *getStaticLocalDeclAddress(const VarDecl *VD) {
357 return StaticLocalDeclMap[VD];
359 void setStaticLocalDeclAddress(const VarDecl *D,
360 llvm::GlobalVariable *GV) {
361 StaticLocalDeclMap[D] = GV;
364 CGDebugInfo *getModuleDebugInfo() { return DebugInfo; }
366 ASTContext &getContext() const { return Context; }
367 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
368 const LangOptions &getLangOptions() const { return Features; }
369 llvm::Module &getModule() const { return TheModule; }
370 CodeGenTypes &getTypes() { return Types; }
371 CodeGenVTables &getVTables() { return VTables; }
372 Diagnostic &getDiags() const { return Diags; }
373 const llvm::TargetData &getTargetData() const { return TheTargetData; }
374 const TargetInfo &getTarget() const { return Context.Target; }
375 llvm::LLVMContext &getLLVMContext() { return VMContext; }
376 const TargetCodeGenInfo &getTargetCodeGenInfo();
377 bool isTargetDarwin() const;
379 bool shouldUseTBAA() const { return TBAA != 0; }
381 llvm::MDNode *getTBAAInfo(QualType QTy);
383 static void DecorateInstruction(llvm::Instruction *Inst,
384 llvm::MDNode *TBAAInfo);
386 /// getSize - Emit the given number of characters as a value of type size_t.
387 llvm::ConstantInt *getSize(CharUnits numChars);
389 /// setGlobalVisibility - Set the visibility for the given LLVM
391 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
393 /// TypeVisibilityKind - The kind of global variable that is passed to
394 /// setTypeVisibility
395 enum TypeVisibilityKind {
398 TVK_ForConstructionVTable,
403 /// setTypeVisibility - Set the visibility for the given global
404 /// value which holds information about a type.
405 void setTypeVisibility(llvm::GlobalValue *GV, const CXXRecordDecl *D,
406 TypeVisibilityKind TVK) const;
408 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
410 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
411 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
412 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
414 llvm_unreachable("unknown visibility!");
415 return llvm::GlobalValue::DefaultVisibility;
418 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) {
419 if (isa<CXXConstructorDecl>(GD.getDecl()))
420 return GetAddrOfCXXConstructor(cast<CXXConstructorDecl>(GD.getDecl()),
422 else if (isa<CXXDestructorDecl>(GD.getDecl()))
423 return GetAddrOfCXXDestructor(cast<CXXDestructorDecl>(GD.getDecl()),
425 else if (isa<FunctionDecl>(GD.getDecl()))
426 return GetAddrOfFunction(GD);
428 return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl()));
431 /// CreateOrReplaceCXXRuntimeVariable - Will return a global variable of the given
432 /// type. If a variable with a different type already exists then a new
433 /// variable with the right type will be created and all uses of the old
434 /// variable will be replaced with a bitcast to the new variable.
435 llvm::GlobalVariable *
436 CreateOrReplaceCXXRuntimeVariable(llvm::StringRef Name, const llvm::Type *Ty,
437 llvm::GlobalValue::LinkageTypes Linkage);
439 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
440 /// given global variable. If Ty is non-null and if the global doesn't exist,
441 /// then it will be greated with the specified type instead of whatever the
442 /// normal requested type would be.
443 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
444 const llvm::Type *Ty = 0);
447 /// GetAddrOfFunction - Return the address of the given function. If Ty is
448 /// non-null, then this function will use the specified type if it has to
450 llvm::Constant *GetAddrOfFunction(GlobalDecl GD,
451 const llvm::Type *Ty = 0,
452 bool ForVTable = false);
454 /// GetAddrOfRTTIDescriptor - Get the address of the RTTI descriptor
455 /// for the given type.
456 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
458 /// GetAddrOfThunk - Get the address of the thunk for the given global decl.
459 llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
461 /// GetWeakRefReference - Get a reference to the target of VD.
462 llvm::Constant *GetWeakRefReference(const ValueDecl *VD);
464 /// GetNonVirtualBaseClassOffset - Returns the offset from a derived class to
465 /// a class. Returns null if the offset is 0.
467 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
468 CastExpr::path_const_iterator PathBegin,
469 CastExpr::path_const_iterator PathEnd);
471 /// A pair of helper functions for a __block variable.
472 class ByrefHelpers : public llvm::FoldingSetNode {
474 llvm::Constant *CopyHelper;
475 llvm::Constant *DisposeHelper;
477 /// The alignment of the field. This is important because
478 /// different offsets to the field within the byref struct need to
479 /// have different helper functions.
482 ByrefHelpers(CharUnits alignment) : Alignment(alignment) {}
483 virtual ~ByrefHelpers();
485 void Profile(llvm::FoldingSetNodeID &id) const {
486 id.AddInteger(Alignment.getQuantity());
489 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
491 virtual bool needsCopy() const { return true; }
492 virtual void emitCopy(CodeGenFunction &CGF,
493 llvm::Value *dest, llvm::Value *src) = 0;
495 virtual bool needsDispose() const { return true; }
496 virtual void emitDispose(CodeGenFunction &CGF, llvm::Value *field) = 0;
499 llvm::FoldingSet<ByrefHelpers> ByrefHelpersCache;
501 /// getUniqueBlockCount - Fetches the global unique block count.
502 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
504 /// getBlockDescriptorType - Fetches the type of a generic block
506 llvm::Type *getBlockDescriptorType();
508 /// getGenericBlockLiteralType - The type of a generic block literal.
509 llvm::Type *getGenericBlockLiteralType();
511 /// GetAddrOfGlobalBlock - Gets the address of a block which
512 /// requires no captures.
513 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
515 /// GetStringForStringLiteral - Return the appropriate bytes for a string
516 /// literal, properly padded to match the literal type. If only the address of
517 /// a constant is needed consider using GetAddrOfConstantStringLiteral.
518 std::string GetStringForStringLiteral(const StringLiteral *E);
520 /// GetAddrOfConstantCFString - Return a pointer to a constant CFString object
521 /// for the given string.
522 llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal);
524 /// GetAddrOfConstantString - Return a pointer to a constant NSString object
525 /// for the given string. Or a user defined String object as defined via
526 /// -fconstant-string-class=class_name option.
527 llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal);
529 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a constant array
530 /// for the given string literal.
531 llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
533 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
534 /// array for the given ObjCEncodeExpr node.
535 llvm::Constant *GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
537 /// GetAddrOfConstantString - Returns a pointer to a character array
538 /// containing the literal. This contents are exactly that of the given
539 /// string, i.e. it will not be null terminated automatically; see
540 /// GetAddrOfConstantCString. Note that whether the result is actually a
541 /// pointer to an LLVM constant depends on Feature.WriteableStrings.
543 /// The result has pointer to array type.
545 /// \param GlobalName If provided, the name to use for the global
546 /// (if one is created).
547 llvm::Constant *GetAddrOfConstantString(llvm::StringRef Str,
548 const char *GlobalName=0);
550 /// GetAddrOfConstantCString - Returns a pointer to a character array
551 /// containing the literal and a terminating '\0' character. The result has
552 /// pointer to array type.
554 /// \param GlobalName If provided, the name to use for the global (if one is
556 llvm::Constant *GetAddrOfConstantCString(const std::string &str,
557 const char *GlobalName=0);
559 /// GetAddrOfCXXConstructor - Return the address of the constructor of the
561 llvm::GlobalValue *GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor,
562 CXXCtorType ctorType,
563 const CGFunctionInfo *fnInfo = 0);
565 /// GetAddrOfCXXDestructor - Return the address of the constructor of the
567 llvm::GlobalValue *GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
568 CXXDtorType dtorType,
569 const CGFunctionInfo *fnInfo = 0);
571 /// getBuiltinLibFunction - Given a builtin id for a function like
572 /// "__builtin_fabsf", return a Function* for "fabsf".
573 llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
576 llvm::Function *getIntrinsic(unsigned IID, llvm::ArrayRef<llvm::Type*> Tys =
577 llvm::ArrayRef<llvm::Type*>());
579 /// EmitTopLevelDecl - Emit code for a single top level declaration.
580 void EmitTopLevelDecl(Decl *D);
582 /// AddUsedGlobal - Add a global which should be forced to be
583 /// present in the object file; these are emitted to the llvm.used
585 void AddUsedGlobal(llvm::GlobalValue *GV);
587 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
589 /// AddCXXDtorEntry - Add a destructor and object to add to the C++ global
590 /// destructor function.
591 void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) {
592 CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object));
595 /// CreateRuntimeFunction - Create a new runtime function with the specified
597 llvm::Constant *CreateRuntimeFunction(const llvm::FunctionType *Ty,
598 llvm::StringRef Name,
599 llvm::Attributes ExtraAttrs =
600 llvm::Attribute::None);
601 /// CreateRuntimeVariable - Create a new runtime global variable with the
602 /// specified type and name.
603 llvm::Constant *CreateRuntimeVariable(const llvm::Type *Ty,
604 llvm::StringRef Name);
606 ///@name Custom Blocks Runtime Interfaces
609 llvm::Constant *getNSConcreteGlobalBlock();
610 llvm::Constant *getNSConcreteStackBlock();
611 llvm::Constant *getBlockObjectAssign();
612 llvm::Constant *getBlockObjectDispose();
616 // UpdateCompleteType - Make sure that this type is translated.
617 void UpdateCompletedType(const TagDecl *TD);
619 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
621 /// EmitConstantExpr - Try to emit the given expression as a
622 /// constant; returns 0 if the expression cannot be emitted as a
624 llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType,
625 CodeGenFunction *CGF = 0);
627 /// EmitNullConstant - Return the result of value-initializing the given
628 /// type, i.e. a null expression of the given type. This is usually,
629 /// but not always, an LLVM null constant.
630 llvm::Constant *EmitNullConstant(QualType T);
632 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
633 const AnnotateAttr *AA, unsigned LineNo);
635 /// Error - Emit a general error that something can't be done.
636 void Error(SourceLocation loc, llvm::StringRef error);
638 /// ErrorUnsupported - Print out an error that codegen doesn't support the
639 /// specified stmt yet.
640 /// \param OmitOnError - If true, then this error should only be emitted if no
641 /// other errors have been reported.
642 void ErrorUnsupported(const Stmt *S, const char *Type,
643 bool OmitOnError=false);
645 /// ErrorUnsupported - Print out an error that codegen doesn't support the
646 /// specified decl yet.
647 /// \param OmitOnError - If true, then this error should only be emitted if no
648 /// other errors have been reported.
649 void ErrorUnsupported(const Decl *D, const char *Type,
650 bool OmitOnError=false);
652 /// SetInternalFunctionAttributes - Set the attributes on the LLVM
653 /// function for the given decl and function info. This applies
654 /// attributes necessary for handling the ABI as well as user
655 /// specified attributes like section.
656 void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F,
657 const CGFunctionInfo &FI);
659 /// SetLLVMFunctionAttributes - Set the LLVM function attributes
660 /// (sext, zext, etc).
661 void SetLLVMFunctionAttributes(const Decl *D,
662 const CGFunctionInfo &Info,
665 /// SetLLVMFunctionAttributesForDefinition - Set the LLVM function attributes
666 /// which only apply to a function definintion.
667 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
669 /// ReturnTypeUsesSRet - Return true iff the given type uses 'sret' when used
670 /// as a return type.
671 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
673 /// ReturnTypeUsesSret - Return true iff the given type uses 'fpret' when used
674 /// as a return type.
675 bool ReturnTypeUsesFPRet(QualType ResultType);
677 /// ConstructAttributeList - Get the LLVM attributes and calling convention to
678 /// use for a particular function type.
680 /// \param Info - The function type information.
681 /// \param TargetDecl - The decl these attributes are being constructed
682 /// for. If supplied the attributes applied to this decl may contribute to the
683 /// function attributes and calling convention.
684 /// \param PAL [out] - On return, the attribute list to use.
685 /// \param CallingConv [out] - On return, the LLVM calling convention to use.
686 void ConstructAttributeList(const CGFunctionInfo &Info,
687 const Decl *TargetDecl,
688 AttributeListType &PAL,
689 unsigned &CallingConv);
691 llvm::StringRef getMangledName(GlobalDecl GD);
692 void getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer,
693 const BlockDecl *BD);
695 void EmitTentativeDefinition(const VarDecl *D);
697 void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired);
699 llvm::GlobalVariable::LinkageTypes
700 getFunctionLinkage(const FunctionDecl *FD);
702 void setFunctionLinkage(const FunctionDecl *FD, llvm::GlobalValue *V) {
703 V->setLinkage(getFunctionLinkage(FD));
706 /// getVTableLinkage - Return the appropriate linkage for the vtable, VTT,
707 /// and type information of the given class.
708 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
710 /// GetTargetTypeStoreSize - Return the store size, in character units, of
711 /// the given LLVM type.
712 CharUnits GetTargetTypeStoreSize(const llvm::Type *Ty) const;
714 /// GetLLVMLinkageVarDefinition - Returns LLVM linkage for a global
716 llvm::GlobalValue::LinkageTypes
717 GetLLVMLinkageVarDefinition(const VarDecl *D,
718 llvm::GlobalVariable *GV);
720 std::vector<const CXXRecordDecl*> DeferredVTables;
723 llvm::GlobalValue *GetGlobalValue(llvm::StringRef Ref);
725 llvm::Constant *GetOrCreateLLVMFunction(llvm::StringRef MangledName,
726 const llvm::Type *Ty,
729 llvm::Attributes ExtraAttrs =
730 llvm::Attribute::None);
731 llvm::Constant *GetOrCreateLLVMGlobal(llvm::StringRef MangledName,
732 const llvm::PointerType *PTy,
734 bool UnnamedAddr = false);
736 /// SetCommonAttributes - Set attributes which are common to any
737 /// form of a global definition (alias, Objective-C method,
738 /// function, global variable).
740 /// NOTE: This should only be called for definitions.
741 void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV);
743 /// SetFunctionDefinitionAttributes - Set attributes for a global definition.
744 void SetFunctionDefinitionAttributes(const FunctionDecl *D,
745 llvm::GlobalValue *GV);
747 /// SetFunctionAttributes - Set function attributes for a function
749 void SetFunctionAttributes(GlobalDecl GD,
751 bool IsIncompleteFunction);
753 /// EmitGlobal - Emit code for a singal global function or var decl. Forward
754 /// declarations are emitted lazily.
755 void EmitGlobal(GlobalDecl D);
757 void EmitGlobalDefinition(GlobalDecl D);
759 void EmitGlobalFunctionDefinition(GlobalDecl GD);
760 void EmitGlobalVarDefinition(const VarDecl *D);
761 void EmitAliasDefinition(GlobalDecl GD);
762 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
763 void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
765 // C++ related functions.
767 bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target);
768 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
770 void EmitNamespace(const NamespaceDecl *D);
771 void EmitLinkageSpec(const LinkageSpecDecl *D);
773 /// EmitCXXConstructors - Emit constructors (base, complete) from a
774 /// C++ constructor Decl.
775 void EmitCXXConstructors(const CXXConstructorDecl *D);
777 /// EmitCXXConstructor - Emit a single constructor with the given type from
778 /// a C++ constructor Decl.
779 void EmitCXXConstructor(const CXXConstructorDecl *D, CXXCtorType Type);
781 /// EmitCXXDestructors - Emit destructors (base, complete) from a
782 /// C++ destructor Decl.
783 void EmitCXXDestructors(const CXXDestructorDecl *D);
785 /// EmitCXXDestructor - Emit a single destructor with the given type from
786 /// a C++ destructor Decl.
787 void EmitCXXDestructor(const CXXDestructorDecl *D, CXXDtorType Type);
789 /// EmitCXXGlobalInitFunc - Emit the function that initializes C++ globals.
790 void EmitCXXGlobalInitFunc();
792 /// EmitCXXGlobalDtorFunc - Emit the function that destroys C++ globals.
793 void EmitCXXGlobalDtorFunc();
795 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
796 llvm::GlobalVariable *Addr);
798 // FIXME: Hardcoding priority here is gross.
799 void AddGlobalCtor(llvm::Function *Ctor, int Priority=65535);
800 void AddGlobalDtor(llvm::Function *Dtor, int Priority=65535);
802 /// EmitCtorList - Generates a global array of functions and priorities using
803 /// the given list and name. This array will have appending linkage and is
804 /// suitable for use as a LLVM constructor or destructor array.
805 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
807 void EmitAnnotations(void);
809 /// EmitFundamentalRTTIDescriptor - Emit the RTTI descriptors for the
811 void EmitFundamentalRTTIDescriptor(QualType Type);
813 /// EmitFundamentalRTTIDescriptors - Emit the RTTI descriptors for the
815 void EmitFundamentalRTTIDescriptors();
817 /// EmitDeferred - Emit any needed decls for which code generation
819 void EmitDeferred(void);
821 /// EmitLLVMUsed - Emit the llvm.used metadata used to force
822 /// references to global which may otherwise be optimized out.
823 void EmitLLVMUsed(void);
825 void EmitDeclMetadata();
827 /// EmitCoverageFile - Emit the llvm.gcov metadata used to tell LLVM where
828 /// to emit the .gcno and .gcda files in a way that persists in .bc files.
829 void EmitCoverageFile();
831 /// MayDeferGeneration - Determine if the given decl can be emitted
832 /// lazily; this is only relevant for definitions. The given decl
833 /// must be either a function or var decl.
834 bool MayDeferGeneration(const ValueDecl *D);
836 /// SimplifyPersonality - Check whether we can use a "simpler", more
837 /// core exceptions personality function.
838 void SimplifyPersonality();
840 } // end namespace CodeGen
841 } // end namespace clang