blob: d69b87f12fa6ee78a4421c5380424709baa4bb4a [file] [log] [blame]
Chris Lattnerbed31442007-05-28 01:07:47 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen --------------===//
Chris Lattnerf97fe382007-05-24 06:29:05 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerf97fe382007-05-24 06:29:05 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This is the internal per-translation-unit state used for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerc18bfbb2008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENMODULE_H
15#define CLANG_CODEGEN_CODEGENMODULE_H
Chris Lattnerf97fe382007-05-24 06:29:05 +000016
Chris Lattner2ccb73b2007-06-16 00:16:26 +000017#include "CodeGenTypes.h"
Chris Lattnera087ff92008-03-01 08:45:05 +000018#include "CGObjCRuntime.h"
Dan Gohman75d69da2008-05-22 00:50:06 +000019#include "clang/AST/Attr.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000020#include "llvm/ADT/DenseMap.h"
Anders Carlssonb04ea612007-08-21 00:21:21 +000021#include "llvm/ADT/StringMap.h"
Chris Lattner2ccb73b2007-06-16 00:16:26 +000022
Chris Lattnerf97fe382007-05-24 06:29:05 +000023namespace llvm {
24 class Module;
Chris Lattner2052bc82007-06-16 00:12:05 +000025 class Constant;
Chris Lattner09153c02007-06-22 18:48:09 +000026 class Function;
Nate Begemanfaae0812008-04-19 04:17:09 +000027 class GlobalValue;
Devang Patel75ef2f02007-10-31 20:01:01 +000028 class TargetData;
Eli Friedmanf6b091f2008-06-01 15:54:49 +000029 class FunctionType;
Chris Lattner23b7eb62007-06-15 23:05:46 +000030}
31
Chris Lattnerf97fe382007-05-24 06:29:05 +000032namespace clang {
33 class ASTContext;
34 class FunctionDecl;
Chris Lattner4bd55962008-03-30 23:03:07 +000035 class ObjCMethodDecl;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000036 class ObjCImplementationDecl;
37 class ObjCCategoryImplDecl;
38 class ObjCProtocolDecl;
Chris Lattner2052bc82007-06-16 00:12:05 +000039 class Decl;
Oliver Huntaefc8fd2007-12-02 00:11:25 +000040 class Expr;
Chris Lattnerd45aa2a2007-12-02 07:19:18 +000041 class Stmt;
Nate Begeman1c906202008-04-20 20:38:08 +000042 class NamedDecl;
Chris Lattner37bd2ec2007-12-18 08:16:44 +000043 class VarDecl;
Chris Lattnerfb300092007-11-28 05:34:05 +000044 struct LangOptions;
Chris Lattnerc8dbe1e2007-12-02 01:40:18 +000045 class Diagnostic;
Nate Begemanfaae0812008-04-19 04:17:09 +000046 class AnnotateAttr;
Chris Lattnerf97fe382007-05-24 06:29:05 +000047
48namespace CodeGen {
49
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +000050 class CodeGenFunction;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000051 class CGDebugInfo;
52
Chris Lattnerbed31442007-05-28 01:07:47 +000053/// CodeGenModule - This class organizes the cross-module state that is used
54/// while generating LLVM code.
55class CodeGenModule {
Chris Lattnerf97fe382007-05-24 06:29:05 +000056 ASTContext &Context;
Chris Lattnerfb300092007-11-28 05:34:05 +000057 const LangOptions &Features;
Chris Lattner23b7eb62007-06-15 23:05:46 +000058 llvm::Module &TheModule;
Devang Patel75ef2f02007-10-31 20:01:01 +000059 const llvm::TargetData &TheTargetData;
Chris Lattnerc8dbe1e2007-12-02 01:40:18 +000060 Diagnostic &Diags;
Chris Lattner2ccb73b2007-06-16 00:16:26 +000061 CodeGenTypes Types;
Chris Lattnera087ff92008-03-01 08:45:05 +000062 CGObjCRuntime *Runtime;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000063 CGDebugInfo *DebugInfo;
Chris Lattner2ccb73b2007-06-16 00:16:26 +000064
Chris Lattner09153c02007-06-22 18:48:09 +000065 llvm::Function *MemCpyFn;
Eli Friedmandf649f32008-05-26 12:59:39 +000066 llvm::Function *MemMoveFn;
Lauro Ramos Venancio91fdb9e2008-02-19 22:01:01 +000067 llvm::Function *MemSetFn;
Chris Lattnerb6984c42007-06-20 04:44:43 +000068 llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap;
Nate Begeman1c906202008-04-20 20:38:08 +000069 std::vector<const NamedDecl*> StaticDecls;
Nate Begeman8e8d4982008-04-20 06:29:50 +000070
Chris Lattner5ff2d5d2008-03-14 17:18:18 +000071 std::vector<llvm::Constant*> GlobalCtors;
Nate Begeman7fab5782008-04-18 23:43:57 +000072 std::vector<llvm::Constant*> Annotations;
Chris Lattnerfb300092007-11-28 05:34:05 +000073
Anders Carlssonb04ea612007-08-21 00:21:21 +000074 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattnerfb300092007-11-28 05:34:05 +000075 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Anders Carlssonb04ea612007-08-21 00:21:21 +000076 llvm::Constant *CFConstantStringClassRef;
Chris Lattner1eec6602007-08-31 04:31:45 +000077
78 std::vector<llvm::Function *> BuiltinFunctions;
Chris Lattnerf97fe382007-05-24 06:29:05 +000079public:
Chris Lattnerfb300092007-11-28 05:34:05 +000080 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000081 const llvm::TargetData &TD, Diagnostic &Diags,
82 bool GenerateDebugInfo);
Chris Lattnera087ff92008-03-01 08:45:05 +000083 ~CodeGenModule();
Chris Lattnerf97fe382007-05-24 06:29:05 +000084
Chris Lattnera087ff92008-03-01 08:45:05 +000085 CGObjCRuntime *getObjCRuntime() { return Runtime; }
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000086 CGDebugInfo *getDebugInfo() { return DebugInfo; }
Chris Lattnerd1af2d22007-05-29 23:17:50 +000087 ASTContext &getContext() const { return Context; }
Chris Lattnerfb300092007-11-28 05:34:05 +000088 const LangOptions &getLangOptions() const { return Features; }
Chris Lattner23b7eb62007-06-15 23:05:46 +000089 llvm::Module &getModule() const { return TheModule; }
Chris Lattner2ccb73b2007-06-16 00:16:26 +000090 CodeGenTypes &getTypes() { return Types; }
Chris Lattnerc8dbe1e2007-12-02 01:40:18 +000091 Diagnostic &getDiags() const { return Diags; }
Chris Lattner20455f22008-01-03 06:36:51 +000092 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Chris Lattnerd1af2d22007-05-29 23:17:50 +000093
Chris Lattner5bcdf242007-12-02 07:09:19 +000094 llvm::Constant *GetAddrOfFunctionDecl(const FunctionDecl *D,
95 bool isDefinition);
Chris Lattner37bd2ec2007-12-18 08:16:44 +000096 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, bool isDefinition);
Chris Lattner1eec6602007-08-31 04:31:45 +000097
Chris Lattner41af8182007-12-02 06:27:33 +000098
Chris Lattner1eec6602007-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 Carlssonb04ea612007-08-21 00:21:21 +0000103 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattner36fc8792008-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 Lattnerfb300092007-11-28 05:34:05 +0000107 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Chris Lattner09153c02007-06-22 18:48:09 +0000108 llvm::Function *getMemCpyFn();
Eli Friedmandf649f32008-05-26 12:59:39 +0000109 llvm::Function *getMemMoveFn();
Lauro Ramos Venancio91fdb9e2008-02-19 22:01:01 +0000110 llvm::Function *getMemSetFn();
Chris Lattnerb8be97e2007-12-18 00:25:38 +0000111 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
112 unsigned NumTys = 0);
Chris Lattner1eec6602007-08-31 04:31:45 +0000113
Chris Lattner5ff2d5d2008-03-14 17:18:18 +0000114 void AddGlobalCtor(llvm::Function * Ctor);
115 void EmitGlobalCtors(void);
Nate Begeman7fab5782008-04-18 23:43:57 +0000116 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
117 void EmitAnnotations(void);
Nate Begeman8e8d4982008-04-20 06:29:50 +0000118 void EmitStatics(void);
Chris Lattner5ff2d5d2008-03-14 17:18:18 +0000119
Chris Lattner4bd55962008-03-30 23:03:07 +0000120 void EmitObjCMethod(const ObjCMethodDecl *OMD);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000121 void EmitObjCCategoryImpl(const ObjCCategoryImplDecl *OCD);
122 void EmitObjCClassImplementation(const ObjCImplementationDecl *OID);
123 void EmitObjCProtocolImplementation(const ObjCProtocolDecl *PD);
Chris Lattnerd14bfa92007-07-13 05:13:43 +0000124 void EmitFunction(const FunctionDecl *FD);
Steve Naroff08899ff2008-04-15 22:42:06 +0000125 void EmitGlobalVar(const VarDecl *D);
Nate Begeman8e8d4982008-04-20 06:29:50 +0000126 void EmitGlobalVarInit(const VarDecl *D);
Steve Naroff08899ff2008-04-15 22:42:06 +0000127 void EmitGlobalVarDeclarator(const VarDecl *D);
Chris Lattner68be6062008-02-06 05:08:19 +0000128 void UpdateCompletedType(const TagDecl *D);
Anders Carlsson610ee712008-01-26 01:36:00 +0000129 llvm::Constant *EmitGlobalInit(const Expr *E);
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +0000130 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begemanfaae0812008-04-19 04:17:09 +0000131 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
132 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlsson610ee712008-01-26 01:36:00 +0000133
Chris Lattnerd45aa2a2007-12-02 07:19:18 +0000134 /// WarnUnsupported - Print out a warning that codegen doesn't support the
135 /// specified stmt yet.
Anders Carlsson610ee712008-01-26 01:36:00 +0000136
Chris Lattnerd45aa2a2007-12-02 07:19:18 +0000137 void WarnUnsupported(const Stmt *S, const char *Type);
138
Chris Lattner38376f12008-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 Gohman75d69da2008-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 Lattner5bcdf242007-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 Friedmanf6b091f2008-06-01 15:54:49 +0000153
154 void SetFunctionAttributes(const FunctionDecl *FD,
155 llvm::Function *F,
156 const llvm::FunctionType *FTy);
Nuno Lopesb6f79532008-06-08 15:45:52 +0000157
158 void SetGlobalValueAttributes(const FunctionDecl *FD,
159 llvm::GlobalValue *GV);
Chris Lattner5bcdf242007-12-02 07:09:19 +0000160
Chris Lattnerf97fe382007-05-24 06:29:05 +0000161};
162} // end namespace CodeGen
163} // end namespace clang
Chris Lattnerf97fe382007-05-24 06:29:05 +0000164
165#endif