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