xref: /llvm-project/clang/lib/CodeGen/ModuleBuilder.cpp (revision 4bd5596d0827ce8d021c8b67fcd9dca041d654ba)
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/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/Decl.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 "clang/CodeGen/ModuleBuilder.h"
27 #include "llvm/Module.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetMachine.h"
30 
31 namespace {
32   class CodeGenerator : public ASTConsumer {
33     Diagnostic &Diags;
34     const llvm::TargetData *TD;
35     ASTContext *Ctx;
36     const LangOptions &Features;
37   protected:
38     llvm::Module *&M;
39     CodeGen::CodeGenModule *Builder;
40   public:
41     CodeGenerator(Diagnostic &diags, const LangOptions &LO,
42                   llvm::Module *&DestModule)
43     : Diags(diags), Features(LO), M(DestModule) {}
44 
45     ~CodeGenerator() {
46       delete Builder;
47     }
48 
49     virtual void Initialize(ASTContext &Context) {
50       Ctx = &Context;
51 
52       M->setTargetTriple(Ctx->Target.getTargetTriple());
53       M->setDataLayout(Ctx->Target.getTargetDescription());
54       TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
55       Builder = new CodeGen::CodeGenModule(Context, Features, *M, *TD, Diags);
56     }
57 
58     virtual void HandleTopLevelDecl(Decl *D) {
59       // If an error occurred, stop code generation, but continue parsing and
60       // semantic analysis (to ensure all warnings and errors are emitted).
61       if (Diags.hasErrorOccurred())
62         return;
63 
64       if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
65         Builder->EmitFunction(FD);
66       } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
67         Builder->EmitGlobalVarDeclarator(FVD);
68       } else if (isa<ObjCClassDecl>(D) || isa<ObjCCategoryDecl>(D)) {
69         // Forward declaration.  Only used for type checking.
70       } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
71         Builder->EmitObjCMethod(OMD);
72       } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
73         if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
74           Builder->WarnUnsupported(LSD, "linkage spec");
75         // FIXME: implement C++ linkage, C linkage works mostly by C
76         // language reuse already.
77       } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
78         std::string AsmString(AD->getAsmString()->getStrData(),
79                               AD->getAsmString()->getByteLength());
80 
81         const std::string &S = Builder->getModule().getModuleInlineAsm();
82         if (S.empty())
83           Builder->getModule().setModuleInlineAsm(AsmString);
84         else
85           Builder->getModule().setModuleInlineAsm(S + '\n' + AsmString);
86       } else {
87         assert(isa<TypeDecl>(D) && "Unknown top level decl");
88         // TODO: handle debug info?
89       }
90     }
91 
92     /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
93     /// (e.g. struct, union, enum, class) is completed.  This allows the client to
94     /// hack on the type, which can occur at any point in the file (because these
95     /// can be defined in declspecs).
96     virtual void HandleTagDeclDefinition(TagDecl *D) {
97       Builder->UpdateCompletedType(D);
98     }
99 
100   };
101 }
102 
103 ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags,
104                                       const LangOptions &Features,
105                                       llvm::Module *&DestModule) {
106   return new CodeGenerator(Diags, Features, DestModule);
107 }
108 
109