1 //===--- CodeGen/ModuleBuilder.h - Build LLVM from ASTs ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ModuleBuilder interface.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_CODEGEN_MODULEBUILDER_H
14 #define LLVM_CLANG_CODEGEN_MODULEBUILDER_H
15 
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/Basic/LLVM.h"
18 
19 namespace llvm {
20   class Constant;
21   class LLVMContext;
22   class Module;
23   class StringRef;
24 
25   namespace vfs {
26   class FileSystem;
27   }
28 }
29 
30 namespace clang {
31   class CodeGenOptions;
32   class CoverageSourceInfo;
33   class Decl;
34   class DiagnosticsEngine;
35   class GlobalDecl;
36   class HeaderSearchOptions;
37   class LangOptions;
38   class PreprocessorOptions;
39 
40 namespace CodeGen {
41   class CodeGenModule;
42   class CGDebugInfo;
43 }
44 
45 /// The primary public interface to the Clang code generator.
46 ///
47 /// This is not really an abstract interface.
48 class CodeGenerator : public ASTConsumer {
49   virtual void anchor();
50 
51 public:
52   /// Return an opaque reference to the CodeGenModule object, which can
53   /// be used in various secondary APIs.  It is valid as long as the
54   /// CodeGenerator exists.
55   CodeGen::CodeGenModule &CGM();
56 
57   /// Return the module that this code generator is building into.
58   ///
59   /// This may return null after HandleTranslationUnit is called;
60   /// this signifies that there was an error generating code.  A
61   /// diagnostic will have been generated in this case, and the module
62   /// will be deleted.
63   ///
64   /// It will also return null if the module is released.
65   llvm::Module *GetModule();
66 
67   /// Release ownership of the module to the caller.
68   ///
69   /// It is illegal to call methods other than GetModule on the
70   /// CodeGenerator after releasing its module.
71   llvm::Module *ReleaseModule();
72 
73   /// Return debug info code generator.
74   CodeGen::CGDebugInfo *getCGDebugInfo();
75 
76   /// Given a mangled name, return a declaration which mangles that way
77   /// which has been added to this code generator via a Handle method.
78   ///
79   /// This may return null if there was no matching declaration.
80   const Decl *GetDeclForMangledName(llvm::StringRef MangledName);
81 
82   /// Given a global declaration, return a mangled name for this declaration
83   /// which has been added to this code generator via a Handle method.
84   llvm::StringRef GetMangledName(GlobalDecl GD);
85 
86   /// Return the LLVM address of the given global entity.
87   ///
88   /// \param isForDefinition If true, the caller intends to define the
89   ///   entity; the object returned will be an llvm::GlobalValue of
90   ///   some sort.  If false, the caller just intends to use the entity;
91   ///   the object returned may be any sort of constant value, and the
92   ///   code generator will schedule the entity for emission if a
93   ///   definition has been registered with this code generator.
94   llvm::Constant *GetAddrOfGlobal(GlobalDecl decl, bool isForDefinition);
95 
96   /// Create a new \c llvm::Module after calling HandleTranslationUnit. This
97   /// enable codegen in interactive processing environments.
98   llvm::Module* StartModule(llvm::StringRef ModuleName, llvm::LLVMContext &C);
99 };
100 
101 /// CreateLLVMCodeGen - Create a CodeGenerator instance.
102 /// It is the responsibility of the caller to call delete on
103 /// the allocated CodeGenerator instance.
104 CodeGenerator *CreateLLVMCodeGen(DiagnosticsEngine &Diags,
105                                  llvm::StringRef ModuleName,
106                                  IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
107                                  const HeaderSearchOptions &HeaderSearchOpts,
108                                  const PreprocessorOptions &PreprocessorOpts,
109                                  const CodeGenOptions &CGO,
110                                  llvm::LLVMContext &C,
111                                  CoverageSourceInfo *CoverageInfo = nullptr);
112 
113 } // end namespace clang
114 
115 #endif
116