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