blob: 3e775260676be103fd2d73181ce950fdb34ff43e [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This is the internal per-translation-unit state used for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2629b882008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENMODULE_H
15#define CLANG_CODEGEN_CODEGENMODULE_H
Chris Lattner4b009652007-07-25 00:24:17 +000016
17#include "CodeGenTypes.h"
Chris Lattnercbfb5512008-03-01 08:45:05 +000018#include "CGObjCRuntime.h"
Dan Gohman4751a3a2008-05-22 00:50:06 +000019#include "clang/AST/Attr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "llvm/ADT/DenseMap.h"
Anders Carlsson36a04872007-08-21 00:21:21 +000021#include "llvm/ADT/StringMap.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022
23namespace llvm {
24 class Module;
25 class Constant;
26 class Function;
Nate Begeman8a704172008-04-19 04:17:09 +000027 class GlobalValue;
Devang Patela8fccb82007-10-31 20:01:01 +000028 class TargetData;
Chris Lattner4b009652007-07-25 00:24:17 +000029}
30
31namespace clang {
32 class ASTContext;
33 class FunctionDecl;
Chris Lattnerb326b172008-03-30 23:03:07 +000034 class ObjCMethodDecl;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000035 class ObjCImplementationDecl;
36 class ObjCCategoryImplDecl;
37 class ObjCProtocolDecl;
Chris Lattner4b009652007-07-25 00:24:17 +000038 class Decl;
Oliver Hunt253e0a72007-12-02 00:11:25 +000039 class Expr;
Chris Lattnercf9c9d02007-12-02 07:19:18 +000040 class Stmt;
Nate Begeman91f52012008-04-20 20:38:08 +000041 class NamedDecl;
Chris Lattnerd2df2b52007-12-18 08:16:44 +000042 class VarDecl;
Chris Lattnerdb6be562007-11-28 05:34:05 +000043 struct LangOptions;
Chris Lattner22595b82007-12-02 01:40:18 +000044 class Diagnostic;
Nate Begeman8a704172008-04-19 04:17:09 +000045 class AnnotateAttr;
Chris Lattner4b009652007-07-25 00:24:17 +000046
47namespace CodeGen {
48
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +000049 class CodeGenFunction;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000050 class CGDebugInfo;
51
Chris Lattner4b009652007-07-25 00:24:17 +000052/// CodeGenModule - This class organizes the cross-module state that is used
53/// while generating LLVM code.
54class CodeGenModule {
55 ASTContext &Context;
Chris Lattnerdb6be562007-11-28 05:34:05 +000056 const LangOptions &Features;
Chris Lattner4b009652007-07-25 00:24:17 +000057 llvm::Module &TheModule;
Devang Patela8fccb82007-10-31 20:01:01 +000058 const llvm::TargetData &TheTargetData;
Chris Lattner22595b82007-12-02 01:40:18 +000059 Diagnostic &Diags;
Chris Lattner4b009652007-07-25 00:24:17 +000060 CodeGenTypes Types;
Chris Lattnercbfb5512008-03-01 08:45:05 +000061 CGObjCRuntime *Runtime;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000062 CGDebugInfo *DebugInfo;
Chris Lattner4b009652007-07-25 00:24:17 +000063
64 llvm::Function *MemCpyFn;
Eli Friedman8f08a252008-05-26 12:59:39 +000065 llvm::Function *MemMoveFn;
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +000066 llvm::Function *MemSetFn;
Chris Lattner4b009652007-07-25 00:24:17 +000067 llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap;
Nate Begeman91f52012008-04-20 20:38:08 +000068 std::vector<const NamedDecl*> StaticDecls;
Nate Begemanad320b62008-04-20 06:29:50 +000069
Chris Lattner753d2592008-03-14 17:18:18 +000070 std::vector<llvm::Constant*> GlobalCtors;
Nate Begeman52da5c72008-04-18 23:43:57 +000071 std::vector<llvm::Constant*> Annotations;
Chris Lattnerdb6be562007-11-28 05:34:05 +000072
Anders Carlsson36a04872007-08-21 00:21:21 +000073 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattnerdb6be562007-11-28 05:34:05 +000074 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Anders Carlsson36a04872007-08-21 00:21:21 +000075 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerab862cc2007-08-31 04:31:45 +000076
77 std::vector<llvm::Function *> BuiltinFunctions;
Chris Lattner4b009652007-07-25 00:24:17 +000078public:
Chris Lattnerdb6be562007-11-28 05:34:05 +000079 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000080 const llvm::TargetData &TD, Diagnostic &Diags,
81 bool GenerateDebugInfo);
Chris Lattnercbfb5512008-03-01 08:45:05 +000082 ~CodeGenModule();
Chris Lattner4b009652007-07-25 00:24:17 +000083
Chris Lattnercbfb5512008-03-01 08:45:05 +000084 CGObjCRuntime *getObjCRuntime() { return Runtime; }
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000085 CGDebugInfo *getDebugInfo() { return DebugInfo; }
Chris Lattner4b009652007-07-25 00:24:17 +000086 ASTContext &getContext() const { return Context; }
Chris Lattnerdb6be562007-11-28 05:34:05 +000087 const LangOptions &getLangOptions() const { return Features; }
Chris Lattner4b009652007-07-25 00:24:17 +000088 llvm::Module &getModule() const { return TheModule; }
89 CodeGenTypes &getTypes() { return Types; }
Chris Lattner22595b82007-12-02 01:40:18 +000090 Diagnostic &getDiags() const { return Diags; }
Chris Lattner17c0cb02008-01-03 06:36:51 +000091 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Chris Lattner4b009652007-07-25 00:24:17 +000092
Chris Lattner1a3c1e22007-12-02 07:09:19 +000093 llvm::Constant *GetAddrOfFunctionDecl(const FunctionDecl *D,
94 bool isDefinition);
Chris Lattnerd2df2b52007-12-18 08:16:44 +000095 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, bool isDefinition);
Chris Lattnerab862cc2007-08-31 04:31:45 +000096
Chris Lattner0e4755d2007-12-02 06:27:33 +000097
Chris Lattnerab862cc2007-08-31 04:31:45 +000098 /// getBuiltinLibFunction - Given a builtin id for a function like
99 /// "__builtin_fabsf", return a Function* for "fabsf".
100 ///
101 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Anders Carlsson36a04872007-08-21 00:21:21 +0000102 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattnera6dcce32008-02-11 00:02:17 +0000103
104 /// GetAddrOfConstantString -- returns a pointer to the character
105 /// array containing the literal. The result is pointer to array type.
Chris Lattnerdb6be562007-11-28 05:34:05 +0000106 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Chris Lattner4b009652007-07-25 00:24:17 +0000107 llvm::Function *getMemCpyFn();
Eli Friedman8f08a252008-05-26 12:59:39 +0000108 llvm::Function *getMemMoveFn();
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +0000109 llvm::Function *getMemSetFn();
Chris Lattner4b23f942007-12-18 00:25:38 +0000110 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
111 unsigned NumTys = 0);
Chris Lattnerab862cc2007-08-31 04:31:45 +0000112
Chris Lattner753d2592008-03-14 17:18:18 +0000113 void AddGlobalCtor(llvm::Function * Ctor);
114 void EmitGlobalCtors(void);
Nate Begeman52da5c72008-04-18 23:43:57 +0000115 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
116 void EmitAnnotations(void);
Nate Begemanad320b62008-04-20 06:29:50 +0000117 void EmitStatics(void);
Chris Lattner753d2592008-03-14 17:18:18 +0000118
Chris Lattnerb326b172008-03-30 23:03:07 +0000119 void EmitObjCMethod(const ObjCMethodDecl *OMD);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000120 void EmitObjCCategoryImpl(const ObjCCategoryImplDecl *OCD);
121 void EmitObjCClassImplementation(const ObjCImplementationDecl *OID);
122 void EmitObjCProtocolImplementation(const ObjCProtocolDecl *PD);
Chris Lattner4b009652007-07-25 00:24:17 +0000123 void EmitFunction(const FunctionDecl *FD);
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000124 void EmitGlobalVar(const VarDecl *D);
Nate Begemanad320b62008-04-20 06:29:50 +0000125 void EmitGlobalVarInit(const VarDecl *D);
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000126 void EmitGlobalVarDeclarator(const VarDecl *D);
Chris Lattner9ec3ca22008-02-06 05:08:19 +0000127 void UpdateCompletedType(const TagDecl *D);
Anders Carlssond76cead2008-01-26 01:36:00 +0000128 llvm::Constant *EmitGlobalInit(const Expr *E);
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +0000129 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begeman8a704172008-04-19 04:17:09 +0000130 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
131 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlssond76cead2008-01-26 01:36:00 +0000132
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000133 /// WarnUnsupported - Print out a warning that codegen doesn't support the
134 /// specified stmt yet.
Anders Carlssond76cead2008-01-26 01:36:00 +0000135
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000136 void WarnUnsupported(const Stmt *S, const char *Type);
137
Chris Lattner806a5f52008-01-12 07:05:38 +0000138 /// WarnUnsupported - Print out a warning that codegen doesn't support the
139 /// specified decl yet.
140 void WarnUnsupported(const Decl *D, const char *Type);
141
Dan Gohman4751a3a2008-05-22 00:50:06 +0000142 /// setVisibility - Set the visibility for the given LLVM GlobalValue
143 /// according to the given clang AST visibility value.
144 static void setVisibility(llvm::GlobalValue *GV,
145 VisibilityAttr::VisibilityTypes);
146
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000147private:
148 /// ReplaceMapValuesWith - This is a really slow and bad function that
149 /// searches for any entries in GlobalDeclMap that point to OldVal, changing
150 /// them to point to NewVal. This is badbadbad, FIXME!
151 void ReplaceMapValuesWith(llvm::Constant *OldVal, llvm::Constant *NewVal);
152
Chris Lattner4b009652007-07-25 00:24:17 +0000153};
154} // end namespace CodeGen
155} // end namespace clang
156
157#endif