blob: 992a3bf3c226eceb73842b83fc8af3c950e17cef [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;
Chris Lattner4b009652007-07-25 00:24:17 +000035 class Decl;
Oliver Hunt253e0a72007-12-02 00:11:25 +000036 class Expr;
Chris Lattnercf9c9d02007-12-02 07:19:18 +000037 class Stmt;
Nate Begeman91f52012008-04-20 20:38:08 +000038 class NamedDecl;
Chris Lattnerd2df2b52007-12-18 08:16:44 +000039 class VarDecl;
Chris Lattnerdb6be562007-11-28 05:34:05 +000040 struct LangOptions;
Chris Lattner22595b82007-12-02 01:40:18 +000041 class Diagnostic;
Nate Begeman8a704172008-04-19 04:17:09 +000042 class AnnotateAttr;
Chris Lattner4b009652007-07-25 00:24:17 +000043
44namespace CodeGen {
45
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +000046 class CodeGenFunction;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000047 class CGDebugInfo;
48
Chris Lattner4b009652007-07-25 00:24:17 +000049/// CodeGenModule - This class organizes the cross-module state that is used
50/// while generating LLVM code.
51class CodeGenModule {
52 ASTContext &Context;
Chris Lattnerdb6be562007-11-28 05:34:05 +000053 const LangOptions &Features;
Chris Lattner4b009652007-07-25 00:24:17 +000054 llvm::Module &TheModule;
Devang Patela8fccb82007-10-31 20:01:01 +000055 const llvm::TargetData &TheTargetData;
Chris Lattner22595b82007-12-02 01:40:18 +000056 Diagnostic &Diags;
Chris Lattner4b009652007-07-25 00:24:17 +000057 CodeGenTypes Types;
Chris Lattnercbfb5512008-03-01 08:45:05 +000058 CGObjCRuntime *Runtime;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000059 CGDebugInfo *DebugInfo;
Chris Lattner4b009652007-07-25 00:24:17 +000060
61 llvm::Function *MemCpyFn;
Eli Friedman8f08a252008-05-26 12:59:39 +000062 llvm::Function *MemMoveFn;
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +000063 llvm::Function *MemSetFn;
Chris Lattner4b009652007-07-25 00:24:17 +000064 llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap;
Nate Begeman91f52012008-04-20 20:38:08 +000065 std::vector<const NamedDecl*> StaticDecls;
Nate Begemanad320b62008-04-20 06:29:50 +000066
Chris Lattner753d2592008-03-14 17:18:18 +000067 std::vector<llvm::Constant*> GlobalCtors;
Nate Begeman52da5c72008-04-18 23:43:57 +000068 std::vector<llvm::Constant*> Annotations;
Chris Lattnerdb6be562007-11-28 05:34:05 +000069
Anders Carlsson36a04872007-08-21 00:21:21 +000070 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattnerdb6be562007-11-28 05:34:05 +000071 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Anders Carlsson36a04872007-08-21 00:21:21 +000072 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerab862cc2007-08-31 04:31:45 +000073
74 std::vector<llvm::Function *> BuiltinFunctions;
Chris Lattner4b009652007-07-25 00:24:17 +000075public:
Chris Lattnerdb6be562007-11-28 05:34:05 +000076 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000077 const llvm::TargetData &TD, Diagnostic &Diags,
78 bool GenerateDebugInfo);
Chris Lattnercbfb5512008-03-01 08:45:05 +000079 ~CodeGenModule();
Chris Lattner4b009652007-07-25 00:24:17 +000080
Chris Lattnercbfb5512008-03-01 08:45:05 +000081 CGObjCRuntime *getObjCRuntime() { return Runtime; }
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000082 CGDebugInfo *getDebugInfo() { return DebugInfo; }
Chris Lattner4b009652007-07-25 00:24:17 +000083 ASTContext &getContext() const { return Context; }
Chris Lattnerdb6be562007-11-28 05:34:05 +000084 const LangOptions &getLangOptions() const { return Features; }
Chris Lattner4b009652007-07-25 00:24:17 +000085 llvm::Module &getModule() const { return TheModule; }
86 CodeGenTypes &getTypes() { return Types; }
Chris Lattner22595b82007-12-02 01:40:18 +000087 Diagnostic &getDiags() const { return Diags; }
Chris Lattner17c0cb02008-01-03 06:36:51 +000088 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Chris Lattner4b009652007-07-25 00:24:17 +000089
Chris Lattner1a3c1e22007-12-02 07:09:19 +000090 llvm::Constant *GetAddrOfFunctionDecl(const FunctionDecl *D,
91 bool isDefinition);
Chris Lattnerd2df2b52007-12-18 08:16:44 +000092 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, bool isDefinition);
Chris Lattnerab862cc2007-08-31 04:31:45 +000093
Chris Lattner0e4755d2007-12-02 06:27:33 +000094
Chris Lattnerab862cc2007-08-31 04:31:45 +000095 /// getBuiltinLibFunction - Given a builtin id for a function like
96 /// "__builtin_fabsf", return a Function* for "fabsf".
97 ///
98 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Anders Carlsson36a04872007-08-21 00:21:21 +000099 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattnera6dcce32008-02-11 00:02:17 +0000100
101 /// GetAddrOfConstantString -- returns a pointer to the character
102 /// array containing the literal. The result is pointer to array type.
Chris Lattnerdb6be562007-11-28 05:34:05 +0000103 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Chris Lattner4b009652007-07-25 00:24:17 +0000104 llvm::Function *getMemCpyFn();
Eli Friedman8f08a252008-05-26 12:59:39 +0000105 llvm::Function *getMemMoveFn();
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +0000106 llvm::Function *getMemSetFn();
Chris Lattner4b23f942007-12-18 00:25:38 +0000107 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
108 unsigned NumTys = 0);
Chris Lattnerab862cc2007-08-31 04:31:45 +0000109
Chris Lattner753d2592008-03-14 17:18:18 +0000110 void AddGlobalCtor(llvm::Function * Ctor);
111 void EmitGlobalCtors(void);
Nate Begeman52da5c72008-04-18 23:43:57 +0000112 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
113 void EmitAnnotations(void);
Nate Begemanad320b62008-04-20 06:29:50 +0000114 void EmitStatics(void);
Chris Lattner753d2592008-03-14 17:18:18 +0000115
Chris Lattnerb326b172008-03-30 23:03:07 +0000116 void EmitObjCMethod(const ObjCMethodDecl *OMD);
Chris Lattner4b009652007-07-25 00:24:17 +0000117 void EmitFunction(const FunctionDecl *FD);
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000118 void EmitGlobalVar(const VarDecl *D);
Nate Begemanad320b62008-04-20 06:29:50 +0000119 void EmitGlobalVarInit(const VarDecl *D);
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000120 void EmitGlobalVarDeclarator(const VarDecl *D);
Chris Lattner9ec3ca22008-02-06 05:08:19 +0000121 void UpdateCompletedType(const TagDecl *D);
Anders Carlssond76cead2008-01-26 01:36:00 +0000122 llvm::Constant *EmitGlobalInit(const Expr *E);
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +0000123 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begeman8a704172008-04-19 04:17:09 +0000124 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
125 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlssond76cead2008-01-26 01:36:00 +0000126
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000127 /// WarnUnsupported - Print out a warning that codegen doesn't support the
128 /// specified stmt yet.
Anders Carlssond76cead2008-01-26 01:36:00 +0000129
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000130 void WarnUnsupported(const Stmt *S, const char *Type);
131
Chris Lattner806a5f52008-01-12 07:05:38 +0000132 /// WarnUnsupported - Print out a warning that codegen doesn't support the
133 /// specified decl yet.
134 void WarnUnsupported(const Decl *D, const char *Type);
135
Dan Gohman4751a3a2008-05-22 00:50:06 +0000136 /// setVisibility - Set the visibility for the given LLVM GlobalValue
137 /// according to the given clang AST visibility value.
138 static void setVisibility(llvm::GlobalValue *GV,
139 VisibilityAttr::VisibilityTypes);
140
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000141private:
142 /// ReplaceMapValuesWith - This is a really slow and bad function that
143 /// searches for any entries in GlobalDeclMap that point to OldVal, changing
144 /// them to point to NewVal. This is badbadbad, FIXME!
145 void ReplaceMapValuesWith(llvm::Constant *OldVal, llvm::Constant *NewVal);
146
Chris Lattner4b009652007-07-25 00:24:17 +0000147};
148} // end namespace CodeGen
149} // end namespace clang
150
151#endif