xref: /llvm-project/clang/lib/CodeGen/ModuleBuilder.cpp (revision 221fa94e404174160e4e83cd21cfa439cc37f199)
1 //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This builds an AST and converts it to LLVM Code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/CodeGen/ModuleBuilder.h"
15 #include "CodeGenModule.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/Expr.h"
20 using namespace clang;
21 
22 //===----------------------------------------------------------------------===//
23 // LLVM Emitter
24 
25 #include "clang/Basic/Diagnostic.h"
26 #include "clang/Basic/TargetInfo.h"
27 #include "clang/CodeGen/ModuleBuilder.h"
28 #include "llvm/Module.h"
29 #include "llvm/Target/TargetData.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/ADT/OwningPtr.h"
33 
34 
35 namespace {
36   class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
37     Diagnostic &Diags;
38     llvm::OwningPtr<const llvm::TargetData> TD;
39     ASTContext *Ctx;
40     const LangOptions &Features;
41     bool GenerateDebugInfo;
42   protected:
43     llvm::OwningPtr<llvm::Module> M;
44     llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
45   public:
46     CodeGeneratorImpl(Diagnostic &diags, const LangOptions &LO,
47                       const std::string& ModuleName,
48                       bool DebugInfoFlag)
49     : Diags(diags), Features(LO), GenerateDebugInfo(DebugInfoFlag),
50       M(new llvm::Module(ModuleName)) {}
51 
52     virtual ~CodeGeneratorImpl() {}
53 
54     virtual llvm::Module* ReleaseModule() {
55       return M.take();
56     }
57 
58     virtual void Initialize(ASTContext &Context) {
59       Ctx = &Context;
60 
61       M->setTargetTriple(Ctx->Target.getTargetTriple());
62       M->setDataLayout(Ctx->Target.getTargetDescription());
63       TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
64       Builder.reset(new CodeGen::CodeGenModule(Context, Features, *M, *TD,
65                                                Diags, GenerateDebugInfo,
66                                                false));
67     }
68 
69     virtual void HandleTopLevelDecl(Decl *D) {
70       // If an error occurred, stop code generation, but continue parsing and
71       // semantic analysis (to ensure all warnings and errors are emitted).
72       if (Diags.hasErrorOccurred())
73         return;
74 
75       if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
76         Builder->EmitGlobal(FD);
77       } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
78         Builder->EmitGlobal(VD);
79       } else if (isa<ObjCClassDecl>(D)){
80         //Forward declaration.  Only used for type checking.
81       } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)){
82         // Generate Protocol object.
83         Builder->EmitObjCProtocolImplementation(PD);
84       } else if (isa<ObjCCategoryDecl>(D)){
85         //Only used for typechecking.
86       } else if (ObjCCategoryImplDecl *OCD = dyn_cast<ObjCCategoryImplDecl>(D)){
87         // Generate methods, attach to category structure
88         Builder->EmitObjCCategoryImpl(OCD);
89       } else if (ObjCImplementationDecl * OID =
90           dyn_cast<ObjCImplementationDecl>(D)){
91         // Generate methods, attach to class structure
92         Builder->EmitObjCClassImplementation(OID);
93       } else if (isa<ObjCInterfaceDecl>(D)){
94         // Ignore - generated when the implementation decl is CodeGen'd
95       } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
96         Builder->EmitObjCMethod(OMD);
97       } else if (isa<ObjCClassDecl>(D) || isa<ObjCCategoryDecl>(D)) {
98         // Forward declaration.  Only used for type checking.
99       } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
100         Builder->EmitObjCMethod(OMD);
101       } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
102         if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
103           Builder->WarnUnsupported(LSD, "linkage spec");
104         // FIXME: implement C++ linkage, C linkage works mostly by C
105         // language reuse already.
106       } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
107         std::string AsmString(AD->getAsmString()->getStrData(),
108                               AD->getAsmString()->getByteLength());
109 
110         const std::string &S = Builder->getModule().getModuleInlineAsm();
111         if (S.empty())
112           Builder->getModule().setModuleInlineAsm(AsmString);
113         else
114           Builder->getModule().setModuleInlineAsm(S + '\n' + AsmString);
115       } else {
116         assert(isa<TypeDecl>(D) && "Unknown top level decl");
117         // TODO: handle debug info?
118       }
119 
120       if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
121         SD = SD->getNextDeclarator();
122         if (SD)
123           HandleTopLevelDecl(SD);
124       }
125     }
126 
127     /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
128     /// (e.g. struct, union, enum, class) is completed. This allows the client to
129     /// hack on the type, which can occur at any point in the file (because these
130     /// can be defined in declspecs).
131     virtual void HandleTagDeclDefinition(TagDecl *D) {
132       Builder->UpdateCompletedType(D);
133     }
134 
135     virtual void HandleTranslationUnit(TranslationUnit& TU) {
136       if (Diags.hasErrorOccurred()) {
137         M.reset();
138         return;
139       }
140 
141       if (Builder)
142         Builder->Release();
143     };
144   };
145 }
146 
147 CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
148                                         const LangOptions &Features,
149                                         const std::string& ModuleName,
150                                         bool GenerateDebugInfo) {
151   return new CodeGeneratorImpl(Diags, Features, ModuleName, GenerateDebugInfo);
152 }
153