CodeGenModule.h revision 67b00520c8f5b48fad722b790d87fea6be764efe
1bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
10ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump// This is the internal per-translation-unit state used for llvm translation.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14ef52a2fb2ace36c92f88c6e125bd7defa17dafa5Chris Lattner#ifndef CLANG_CODEGEN_CODEGENMODULE_H
15ef52a2fb2ace36c92f88c6e125bd7defa17dafa5Chris Lattner#define CLANG_CODEGEN_CODEGENMODULE_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenTypes.h"
184f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman#include "clang/AST/Attr.h"
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/DenseMap.h"
20c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson#include "llvm/ADT/StringMap.h"
216ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor#include "llvm/ADT/StringSet.h"
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2396bd13ae1ab89f5a04b3ad3ec8ec1864cbba3482Mike Stump#include "CGBlocks.h"
24b768807c49a1c7085def099b848631856af766faDaniel Dunbar#include "CGCall.h"
25b768807c49a1c7085def099b848631856af766faDaniel Dunbar
26b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson#include <list>
27b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace llvm {
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class Module;
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class Constant;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class Function;
328bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  class GlobalValue;
337a4718e813e5e99d478567a482217c7eef8572c5Devang Patel  class TargetData;
34ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman  class FunctionType;
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class ASTContext;
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class FunctionDecl;
4090db88249ac92e6ed515065048a4ead4467e6639Daniel Dunbar  class IdentifierInfo;
41391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  class ObjCMethodDecl;
4220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  class ObjCImplementationDecl;
4320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  class ObjCCategoryImplDecl;
4420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  class ObjCProtocolDecl;
45eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  class ObjCEncodeExpr;
46d5cab5435371b8cc74a9e05ebd40b5995ebad149Anders Carlsson  class BlockExpr;
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class Decl;
482824723d6d181d2dfa56e62caabd68b0b18f0b9dOliver Hunt  class Expr;
492c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner  class Stmt;
501e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  class StringLiteral;
511a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman  class NamedDecl;
52bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  class ValueDecl;
532b9d2ca4ce53fffbe8a77c7db2fe4df5ccfb9fd4Chris Lattner  class VarDecl;
5443af76e845c95be160c088ec11ba3c43e2527fa8Sebastian Redl  class LangOptions;
55fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner  class Diagnostic;
568bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  class AnnotateAttr;
57ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace CodeGen {
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
60813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio  class CodeGenFunction;
61e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta  class CGDebugInfo;
62af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  class CGObjCRuntime;
63ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump
64ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump/// CodeGenModule - This class organizes the cross-function state that is used
65ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump/// while generating LLVM code.
662a998148a6823c44d67da347c95eb2ea21f6b986Mike Stumpclass CodeGenModule : public BlockModule {
678a219ceda2b5afd447e7199b9c53079bead31b89Anders Carlsson  CodeGenModule(const CodeGenModule&);  // DO NOT IMPLEMENT
688a219ceda2b5afd447e7199b9c53079bead31b89Anders Carlsson  void operator=(const CodeGenModule&); // DO NOT IMPLEMENT
698a219ceda2b5afd447e7199b9c53079bead31b89Anders Carlsson
706bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  typedef std::vector< std::pair<llvm::Constant*, int> > CtorList;
716bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ASTContext &Context;
7345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  const LangOptions &Features;
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Module &TheModule;
757a4718e813e5e99d478567a482217c7eef8572c5Devang Patel  const llvm::TargetData &TheTargetData;
76fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner  Diagnostic &Diags;
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CodeGenTypes Types;
78815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek  CGObjCRuntime* Runtime;
79815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek  CGDebugInfo* DebugInfo;
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Function *MemCpyFn;
820c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman  llvm::Function *MemMoveFn;
8341ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio  llvm::Function *MemSetFn;
849986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar
85b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  /// RuntimeGlobal - List of runtime globals whose names must be
86b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  /// protected from introducing conflicts. These globals should be
87b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  /// created unnamed, we will name them and patch up conflicts when
88b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  /// we release the module.
8999b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner  std::vector<std::pair<llvm::GlobalValue*, std::string> > RuntimeGlobals;
90f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar
916ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  /// GlobalDeclMap - Mapping of decl names (represented as unique
926ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  /// character pointers from either the identifier table or the set
936ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  /// of mangled names) to global variables we have already
946ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  /// emitted. Note that the entries in this map are the actual
956ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  /// globals and therefore may not be of the same type as the decl,
966ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  /// they should be bitcasted on retrieval. Also note that the
97e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner  /// globals are keyed on their source mangled name, not the global name
9867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  /// (which may change with attributes such as asm-labels).  The key
996ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  /// to this map should be generated using getMangledName().
10067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  ///
10167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  /// Note that this map always lines up exactly with the contents of the LLVM
10267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  /// IR symbol table, but this is quicker to query since it is doing uniqued
10367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  /// pointer lookups instead of full string lookups.
1046ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  llvm::DenseMap<const char*, llvm::GlobalValue*> GlobalDeclMap;
1056ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor
1066ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  /// \brief Contains the strings used for mangled names.
1076ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  ///
1086ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  /// FIXME: Eventually, this should map from the semantic/canonical
1096ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  /// declaration for each global entity to its mangled name (if it
1106ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  /// has one).
1116ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  llvm::StringSet<> MangledNames;
112bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
113ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// Aliases - List of aliases in module. These cannot be emitted until all the
114ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// code has been seen, as they reference things by name instead of directly
115ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// and may reference forward.
1165e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar  std::vector<const ValueDecl*> Aliases;
117219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar
11867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  /// DeferredDecls - This contains all the decls which have definitions but
11967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  /// which are deferred for emission and therefore should only be output if
12067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  /// they are actually used.  If a decl is in this, then it is known to have
12167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  /// not been referenced yet.  The key to this map is a uniqued mangled name.
12267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  llvm::DenseMap<const char*, const ValueDecl*> DeferredDecls;
12367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
12467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  /// DeferredDeclsToEmit - This is a list of deferred decls which we have seen
12567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  /// that *are* actually referenced.  These get code generated when the module
12667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  /// is done.
12767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  std::vector<const ValueDecl*> DeferredDeclsToEmit;
12867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
1290269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  /// LLVMUsed - List of global values which are required to be
1300269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  /// present in the object file; bitcast to i8*. This is used for
1310269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  /// forcing visibility of symbols which may otherwise be optimized
1320269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  /// out.
1330269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  std::vector<llvm::Constant*> LLVMUsed;
134ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump
135ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// GlobalCtors - Store the list of global constructors and their respective
136ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// priorities to be emitted when the translation unit is complete.
1376bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  CtorList GlobalCtors;
1386bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
139ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// GlobalDtors - Store the list of global destructors and their respective
140ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// priorities to be emitted when the translation unit is complete.
1416bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  CtorList GlobalDtors;
1426bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
143532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  std::vector<llvm::Constant*> Annotations;
144ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump
145c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  llvm::StringMap<llvm::Constant*> CFConstantStringMap;
14645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  llvm::StringMap<llvm::Constant*> ConstantStringMap;
1473e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar
148ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// CFConstantStringClassRef - Cached reference to the class for constant
149ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// strings. This value has type int * but is actually an Obj-C class pointer.
150c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  llvm::Constant *CFConstantStringClassRef;
151ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump
152e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner  /// BuiltinFunctions - This is the cached set of Function*'s that have been
153e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner  /// created for each builtin, indexed by the Builtin ID.  This is null if the
154e21c4b829c9e69c20f4baab4916e05cdb786d9aeChris Lattner  /// Function* has not yet been created.
155c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump  std::vector<llvm::Value *> BuiltinFunctions;
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
157ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
158e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta                const llvm::TargetData &TD, Diagnostic &Diags,
159f77ac86f4eca528a04b817d7ad7f045a47d52712Daniel Dunbar                bool GenerateDebugInfo);
160815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek
1612b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner  ~CodeGenModule();
162ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump
163815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek  /// Release - Finalize LLVM code generation.
164815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek  void Release();
165208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar
166208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  /// getObjCRuntime() - Return a reference to the configured
167208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  /// Objective-C runtime.
168ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  CGObjCRuntime &getObjCRuntime() {
169208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar    assert(Runtime && "No Objective-C runtime has been configured.");
170ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump    return *Runtime;
171208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  }
172ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump
173208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  /// hasObjCRuntime() - Return true iff an Objective-C runtime has
174208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  /// been configured.
175208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  bool hasObjCRuntime() { return !!Runtime; }
176208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar
177e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta  CGDebugInfo *getDebugInfo() { return DebugInfo; }
1785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ASTContext &getContext() const { return Context; }
17945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  const LangOptions &getLangOptions() const { return Features; }
1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Module &getModule() const { return TheModule; }
1815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CodeGenTypes &getTypes() { return Types; }
182fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner  Diagnostic &getDiags() const { return Diags; }
1838f925280abe3795ae0fb47f2ba90faea62e26af4Chris Lattner  const llvm::TargetData &getTargetData() const { return TheTargetData; }
184bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
185ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
186570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  /// given global variable.  If Ty is non-null and if the global doesn't exist,
187570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  /// then it will be greated with the specified type instead of whatever the
188570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  /// normal requested type would be.
189570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
190570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner                                     const llvm::Type *Ty = 0);
191bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
19234809507232bc4c3c4840c7d092c7440219fddafChris Lattner  /// GetAddrOfFunction - Return the address of the given function.  If Ty is
19334809507232bc4c3c4840c7d092c7440219fddafChris Lattner  /// non-null, then this function will use the specified type if it has to
19434809507232bc4c3c4840c7d092c7440219fddafChris Lattner  /// create it.
1950558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  llvm::Constant *GetAddrOfFunction(const FunctionDecl *D,
19634809507232bc4c3c4840c7d092c7440219fddafChris Lattner                                    const llvm::Type *Ty = 0);
1976143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
198ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// GetStringForStringLiteral - Return the appropriate bytes for a string
199ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// literal, properly padded to match the literal type. If only the address of
200ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// a constant is needed consider using GetAddrOfConstantStringLiteral.
2016143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  std::string GetStringForStringLiteral(const StringLiteral *E);
2026143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
203ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// GetAddrOfConstantCFString - Return a pointer to a constant CFString object
204ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// for the given string.
205c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
206a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner
207ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// GetAddrOfConstantStringFromLiteral - Return a pointer to a constant array
208ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// for the given string literal.
2096143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
2101e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
211eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
212eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  /// array for the given ObjCEncodeExpr node.
213eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  llvm::Constant *GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
214eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner
2156143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  /// GetAddrOfConstantString - Returns a pointer to a character array
216ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// containing the literal. This contents are exactly that of the given
217ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// string, i.e. it will not be null terminated automatically; see
218ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// GetAddrOfConstantCString. Note that whether the result is actually a
219ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// pointer to an LLVM constant depends on Feature.WriteableStrings.
2206143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  ///
2216143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  /// The result has pointer to array type.
2225fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar  ///
2235fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar  /// \param GlobalName If provided, the name to use for the global
2245fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar  /// (if one is created).
2255fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar  llvm::Constant *GetAddrOfConstantString(const std::string& str,
2265fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                          const char *GlobalName=0);
2276143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
228ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// GetAddrOfConstantCString - Returns a pointer to a character array
229ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// containing the literal and a terminating '\0' character. The result has
230ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// pointer to array type.
2315fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar  ///
232ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// \param GlobalName If provided, the name to use for the global (if one is
233ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// created).
2345fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar  llvm::Constant *GetAddrOfConstantCString(const std::string &str,
2355fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar                                           const char *GlobalName=0);
236ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump
23741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  /// getBuiltinLibFunction - Given a builtin id for a function like
23841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  /// "__builtin_fabsf", return a Function* for "fabsf".
239c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump  llvm::Value *getBuiltinLibFunction(unsigned BuiltinID);
2406143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Function *getMemCpyFn();
2420c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman  llvm::Function *getMemMoveFn();
24341ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio  llvm::Function *getMemSetFn();
244ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
2457acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                               unsigned NumTys = 0);
246bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
24741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  /// EmitTopLevelDecl - Emit code for a single top level declaration.
24841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  void EmitTopLevelDecl(Decl *D);
249bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
2500269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  /// AddUsedGlobal - Add a global which should be forced to be
2510269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  /// present in the object file; these are emitted to the llvm.used
2520269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  /// metadata global.
2530269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  void AddUsedGlobal(llvm::GlobalValue *GV);
2540269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
255bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
256bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
257ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// CreateRuntimeFunction - Create a new runtime function whose name must be
258ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// protected from collisions.
259ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  llvm::Function *CreateRuntimeFunction(const llvm::FunctionType *Ty,
260f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar                                        const std::string &Name);
261b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  /// CreateRuntimeVariable - Create a new runtime global variable
262b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  /// whose name must be protected from collisions.
263b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  llvm::GlobalVariable *CreateRuntimeVariable(const llvm::Type *Ty,
264b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar                                              const std::string &Name);
265f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar
266c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner  void UpdateCompletedType(const TagDecl *D);
267d60f2fbf69df08e952377b498c036f269395024dDaniel Dunbar
268d60f2fbf69df08e952377b498c036f269395024dDaniel Dunbar  /// EmitConstantExpr - Try to emit the given expression as a
269d60f2fbf69df08e952377b498c036f269395024dDaniel Dunbar  /// constant; returns 0 if the expression cannot be emitted as a
270d60f2fbf69df08e952377b498c036f269395024dDaniel Dunbar  /// constant.
271813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio  llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
272d60f2fbf69df08e952377b498c036f269395024dDaniel Dunbar
2738bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
2748bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                   const AnnotateAttr *AA, unsigned LineNo);
275ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump
276488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  /// ErrorUnsupported - Print out an error that codegen doesn't support the
27790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  /// specified stmt yet.
278ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// \param OmitOnError - If true, then this error should only be emitted if no
279ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// other errors have been reported.
280ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  void ErrorUnsupported(const Stmt *S, const char *Type,
28190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                        bool OmitOnError=false);
282ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump
283488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  /// ErrorUnsupported - Print out an error that codegen doesn't support the
284c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  /// specified decl yet.
285ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// \param OmitOnError - If true, then this error should only be emitted if no
286ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// other errors have been reported.
28790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  void ErrorUnsupported(const Decl *D, const char *Type,
28890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                        bool OmitOnError=false);
2894f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman
290f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar  void SetMethodAttributes(const ObjCMethodDecl *MD,
291f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar                           llvm::Function *F);
292f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
293761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  void SetFunctionAttributes(const Decl *D,
294ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump                             const CGFunctionInfo &Info,
295b768807c49a1c7085def099b848631856af766faDaniel Dunbar                             llvm::Function *F);
296b768807c49a1c7085def099b848631856af766faDaniel Dunbar
297ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// ReturnTypeUsesSret - Return true iff the given type uses 'sret' when used
298ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// as a return type.
29988b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  bool ReturnTypeUsesSret(const CGFunctionInfo &FI);
300b768807c49a1c7085def099b848631856af766faDaniel Dunbar
30188b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  void ConstructAttributeList(const CGFunctionInfo &Info,
30288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                              const Decl *TargetDecl,
303761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel                              AttributeListType &PAL);
304b768807c49a1c7085def099b848631856af766faDaniel Dunbar
3056ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  const char *getMangledName(const NamedDecl *ND);
3065f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor
3075f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor
3089cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattnerprivate:
3095c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  /// SetGlobalValueAttributes - Set attributes for a global decl.
3105c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar  void SetGlobalValueAttributes(const Decl *D,
3115c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar                                bool IsInternal,
3125c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar                                bool IsInline,
3135c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar                                llvm::GlobalValue *GV,
3145c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar                                bool ForDefinition);
3155c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar
316ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// SetFunctionAttributesForDefinition - Set function attributes specific to a
317ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// function definition.
318219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  /// \param D - The ObjCMethodDecl or FunctionDecl defining \arg F.
319219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  void SetFunctionAttributesForDefinition(const Decl *D,
320219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar                                          llvm::Function *F);
321f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
322ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman  void SetFunctionAttributes(const FunctionDecl *FD,
323f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar                             llvm::Function *F);
324d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes
325ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// EmitGlobal - Emit code for a singal global function or var decl. Forward
326ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// declarations are emitted lazily.
32741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  void EmitGlobal(const ValueDecl *D);
32841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
329bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  void EmitGlobalDefinition(const ValueDecl *D);
330d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar
331bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  void EmitGlobalFunctionDefinition(const FunctionDecl *D);
332bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  void EmitGlobalVarDefinition(const VarDecl *D);
333af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
334ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump
3356bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // FIXME: Hardcoding priority here is gross.
3366bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  void AddGlobalCtor(llvm::Function * Ctor, int Priority=65535);
3376bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  void AddGlobalDtor(llvm::Function * Dtor, int Priority=65535);
3386bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
339ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// EmitCtorList - Generates a global array of functions and priorities using
340ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// the given list and name. This array will have appending linkage and is
341ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump  /// suitable for use as a LLVM constructor or destructor array.
3426bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  void EmitCtorList(const CtorList &Fns, const char *GlobalName);
343bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
344219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  void EmitAliases(void);
345bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  void EmitAnnotations(void);
3460269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
34742745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar  /// EmitDeferred - Emit any needed decls for which code generation
34842745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar  /// was deferred.
3490269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  void EmitDeferred(void);
3500269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
35142745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar  /// EmitLLVMUsed - Emit the llvm.used metadata used to force
35242745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar  /// references to global which may otherwise be optimized out.
3530269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  void EmitLLVMUsed(void);
354f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar
355b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar  void BindRuntimeGlobals();
35673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar
35742745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar  /// MayDeferGeneration - Determine if the given decl can be emitted
35842745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar  /// lazily; this is only relevant for definitions. The given decl
35942745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar  /// must be either a function or var decl.
36073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar  bool MayDeferGeneration(const ValueDecl *D);
3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace CodeGen
3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
366