blob: d69b87f12fa6ee78a4421c5380424709baa4bb4a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This is the internal per-translation-unit state used for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattneref52a2f2008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENMODULE_H
15#define CLANG_CODEGEN_CODEGENMODULE_H
Reid Spencer5f016e22007-07-11 17:01:13 +000016
17#include "CodeGenTypes.h"
Chris Lattner2b94fe32008-03-01 08:45:05 +000018#include "CGObjCRuntime.h"
Dan Gohman4f8d1232008-05-22 00:50:06 +000019#include "clang/AST/Attr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/DenseMap.h"
Anders Carlssonc9e20912007-08-21 00:21:21 +000021#include "llvm/ADT/StringMap.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022
23namespace llvm {
24 class Module;
25 class Constant;
26 class Function;
Nate Begeman8bd4afe2008-04-19 04:17:09 +000027 class GlobalValue;
Devang Patel7a4718e2007-10-31 20:01:01 +000028 class TargetData;
Eli Friedmanff4a2d92008-06-01 15:54:49 +000029 class FunctionType;
Reid Spencer5f016e22007-07-11 17:01:13 +000030}
31
32namespace clang {
33 class ASTContext;
34 class FunctionDecl;
Chris Lattner391d77a2008-03-30 23:03:07 +000035 class ObjCMethodDecl;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000036 class ObjCImplementationDecl;
37 class ObjCCategoryImplDecl;
38 class ObjCProtocolDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000039 class Decl;
Oliver Hunt28247232007-12-02 00:11:25 +000040 class Expr;
Chris Lattner2c8569d2007-12-02 07:19:18 +000041 class Stmt;
Nate Begeman1a1d92a2008-04-20 20:38:08 +000042 class NamedDecl;
Chris Lattner2b9d2ca2007-12-18 08:16:44 +000043 class VarDecl;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000044 struct LangOptions;
Chris Lattnerfb97b032007-12-02 01:40:18 +000045 class Diagnostic;
Nate Begeman8bd4afe2008-04-19 04:17:09 +000046 class AnnotateAttr;
Reid Spencer5f016e22007-07-11 17:01:13 +000047
48namespace CodeGen {
49
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000050 class CodeGenFunction;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000051 class CGDebugInfo;
52
Reid Spencer5f016e22007-07-11 17:01:13 +000053/// CodeGenModule - This class organizes the cross-module state that is used
54/// while generating LLVM code.
55class CodeGenModule {
56 ASTContext &Context;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000057 const LangOptions &Features;
Reid Spencer5f016e22007-07-11 17:01:13 +000058 llvm::Module &TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000059 const llvm::TargetData &TheTargetData;
Chris Lattnerfb97b032007-12-02 01:40:18 +000060 Diagnostic &Diags;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 CodeGenTypes Types;
Chris Lattner2b94fe32008-03-01 08:45:05 +000062 CGObjCRuntime *Runtime;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000063 CGDebugInfo *DebugInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +000064
65 llvm::Function *MemCpyFn;
Eli Friedman0c995092008-05-26 12:59:39 +000066 llvm::Function *MemMoveFn;
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +000067 llvm::Function *MemSetFn;
Reid Spencer5f016e22007-07-11 17:01:13 +000068 llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap;
Nate Begeman1a1d92a2008-04-20 20:38:08 +000069 std::vector<const NamedDecl*> StaticDecls;
Nate Begeman4c13b7a2008-04-20 06:29:50 +000070
Chris Lattner6d397602008-03-14 17:18:18 +000071 std::vector<llvm::Constant*> GlobalCtors;
Nate Begeman532485c2008-04-18 23:43:57 +000072 std::vector<llvm::Constant*> Annotations;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000073
Anders Carlssonc9e20912007-08-21 00:21:21 +000074 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000075 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Anders Carlssonc9e20912007-08-21 00:21:21 +000076 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerbef20ac2007-08-31 04:31:45 +000077
78 std::vector<llvm::Function *> BuiltinFunctions;
Reid Spencer5f016e22007-07-11 17:01:13 +000079public:
Chris Lattner45e8cbd2007-11-28 05:34:05 +000080 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000081 const llvm::TargetData &TD, Diagnostic &Diags,
82 bool GenerateDebugInfo);
Chris Lattner2b94fe32008-03-01 08:45:05 +000083 ~CodeGenModule();
Reid Spencer5f016e22007-07-11 17:01:13 +000084
Chris Lattner2b94fe32008-03-01 08:45:05 +000085 CGObjCRuntime *getObjCRuntime() { return Runtime; }
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000086 CGDebugInfo *getDebugInfo() { return DebugInfo; }
Reid Spencer5f016e22007-07-11 17:01:13 +000087 ASTContext &getContext() const { return Context; }
Chris Lattner45e8cbd2007-11-28 05:34:05 +000088 const LangOptions &getLangOptions() const { return Features; }
Reid Spencer5f016e22007-07-11 17:01:13 +000089 llvm::Module &getModule() const { return TheModule; }
90 CodeGenTypes &getTypes() { return Types; }
Chris Lattnerfb97b032007-12-02 01:40:18 +000091 Diagnostic &getDiags() const { return Diags; }
Chris Lattner8f925282008-01-03 06:36:51 +000092 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Reid Spencer5f016e22007-07-11 17:01:13 +000093
Chris Lattner9cd4fe42007-12-02 07:09:19 +000094 llvm::Constant *GetAddrOfFunctionDecl(const FunctionDecl *D,
95 bool isDefinition);
Chris Lattner2b9d2ca2007-12-18 08:16:44 +000096 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, bool isDefinition);
Chris Lattnerbef20ac2007-08-31 04:31:45 +000097
Chris Lattner58c3f9e2007-12-02 06:27:33 +000098
Chris Lattnerbef20ac2007-08-31 04:31:45 +000099 /// getBuiltinLibFunction - Given a builtin id for a function like
100 /// "__builtin_fabsf", return a Function* for "fabsf".
101 ///
102 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Anders Carlssonc9e20912007-08-21 00:21:21 +0000103 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000104
105 /// GetAddrOfConstantString -- returns a pointer to the character
106 /// array containing the literal. The result is pointer to array type.
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000107 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 llvm::Function *getMemCpyFn();
Eli Friedman0c995092008-05-26 12:59:39 +0000109 llvm::Function *getMemMoveFn();
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +0000110 llvm::Function *getMemSetFn();
Chris Lattner7acda7c2007-12-18 00:25:38 +0000111 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
112 unsigned NumTys = 0);
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000113
Chris Lattner6d397602008-03-14 17:18:18 +0000114 void AddGlobalCtor(llvm::Function * Ctor);
115 void EmitGlobalCtors(void);
Nate Begeman532485c2008-04-18 23:43:57 +0000116 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
117 void EmitAnnotations(void);
Nate Begeman4c13b7a2008-04-20 06:29:50 +0000118 void EmitStatics(void);
Chris Lattner6d397602008-03-14 17:18:18 +0000119
Chris Lattner391d77a2008-03-30 23:03:07 +0000120 void EmitObjCMethod(const ObjCMethodDecl *OMD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000121 void EmitObjCCategoryImpl(const ObjCCategoryImplDecl *OCD);
122 void EmitObjCClassImplementation(const ObjCImplementationDecl *OID);
123 void EmitObjCProtocolImplementation(const ObjCProtocolDecl *PD);
Chris Lattner88a69ad2007-07-13 05:13:43 +0000124 void EmitFunction(const FunctionDecl *FD);
Steve Naroff248a7532008-04-15 22:42:06 +0000125 void EmitGlobalVar(const VarDecl *D);
Nate Begeman4c13b7a2008-04-20 06:29:50 +0000126 void EmitGlobalVarInit(const VarDecl *D);
Steve Naroff248a7532008-04-15 22:42:06 +0000127 void EmitGlobalVarDeclarator(const VarDecl *D);
Chris Lattnerc5b88062008-02-06 05:08:19 +0000128 void UpdateCompletedType(const TagDecl *D);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000129 llvm::Constant *EmitGlobalInit(const Expr *E);
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000130 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000131 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
132 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000133
Chris Lattner2c8569d2007-12-02 07:19:18 +0000134 /// WarnUnsupported - Print out a warning that codegen doesn't support the
135 /// specified stmt yet.
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000136
Chris Lattner2c8569d2007-12-02 07:19:18 +0000137 void WarnUnsupported(const Stmt *S, const char *Type);
138
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000139 /// WarnUnsupported - Print out a warning that codegen doesn't support the
140 /// specified decl yet.
141 void WarnUnsupported(const Decl *D, const char *Type);
142
Dan Gohman4f8d1232008-05-22 00:50:06 +0000143 /// setVisibility - Set the visibility for the given LLVM GlobalValue
144 /// according to the given clang AST visibility value.
145 static void setVisibility(llvm::GlobalValue *GV,
146 VisibilityAttr::VisibilityTypes);
147
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000148private:
149 /// ReplaceMapValuesWith - This is a really slow and bad function that
150 /// searches for any entries in GlobalDeclMap that point to OldVal, changing
151 /// them to point to NewVal. This is badbadbad, FIXME!
152 void ReplaceMapValuesWith(llvm::Constant *OldVal, llvm::Constant *NewVal);
Eli Friedmanff4a2d92008-06-01 15:54:49 +0000153
154 void SetFunctionAttributes(const FunctionDecl *FD,
155 llvm::Function *F,
156 const llvm::FunctionType *FTy);
Nuno Lopesd4cbda62008-06-08 15:45:52 +0000157
158 void SetGlobalValueAttributes(const FunctionDecl *FD,
159 llvm::GlobalValue *GV);
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000160
Reid Spencer5f016e22007-07-11 17:01:13 +0000161};
162} // end namespace CodeGen
163} // end namespace clang
164
165#endif