clang 23.0.0git
ModuleBuilder.h
Go to the documentation of this file.
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
17#include "clang/Basic/LLVM.h"
18#include "llvm/ADT/StringRef.h"
19
20namespace 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.
32inline constexpr llvm::StringRef ClangTrapPrefix = "__clang_trap_msg";
33
34namespace 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 class CompilerInstance;
44
45namespace CodeGen {
46 class CodeGenModule;
47 class CGDebugInfo;
48}
49
50/// The primary public interface to the Clang code generator.
51class CodeGenerator : public ASTConsumer {
52 virtual void anchor();
53
54protected:
55 /// Use CreateLLVMCodeGen() below to create an instance of this class.
56 CodeGenerator() = default;
57
58 /// True if we've finished generating IR. This prevents us from generating
59 /// additional LLVM IR after emitting output in HandleTranslationUnit. This
60 /// can happen when Clang plugins trigger additional AST deserialization.
61 bool IRGenFinished = false;
62
63public:
64 /// Return an opaque reference to the CodeGenModule object, which can
65 /// be used in various secondary APIs. It is valid as long as the
66 /// CodeGenerator exists.
68
69 /// Return the module that this code generator is building into.
70 ///
71 /// This may return null after HandleTranslationUnit is called;
72 /// this signifies that there was an error generating code. A
73 /// diagnostic will have been generated in this case, and the module
74 /// will be deleted.
75 ///
76 /// It will also return null if the module is released.
77 llvm::Module *GetModule();
78
79 /// Release ownership of the module to the caller.
80 ///
81 /// It is illegal to call methods other than GetModule on the
82 /// CodeGenerator after releasing its module.
83 std::unique_ptr<llvm::Module> ReleaseModule();
84
85 /// Return debug info code generator.
87
88 /// Given a mangled name, return a declaration which mangles that way
89 /// which has been added to this code generator via a Handle method.
90 ///
91 /// This may return null if there was no matching declaration.
92 const Decl *GetDeclForMangledName(llvm::StringRef MangledName);
93
94 /// Given a global declaration, return a mangled name for this declaration
95 /// which has been added to this code generator via a Handle method.
96 llvm::StringRef GetMangledName(GlobalDecl GD);
97
98 /// Return the LLVM address of the given global entity.
99 ///
100 /// \param isForDefinition If true, the caller intends to define the
101 /// entity; the object returned will be an llvm::GlobalValue of
102 /// some sort. If false, the caller just intends to use the entity;
103 /// the object returned may be any sort of constant value, and the
104 /// code generator will schedule the entity for emission if a
105 /// definition has been registered with this code generator.
106 llvm::Constant *GetAddrOfGlobal(GlobalDecl decl, bool isForDefinition);
107
108 /// Create a new \c llvm::Module after calling HandleTranslationUnit. This
109 /// enable codegen in interactive processing environments.
110 llvm::Module* StartModule(llvm::StringRef ModuleName, llvm::LLVMContext &C);
111};
112
113/// CreateLLVMCodeGen - Create a CodeGenerator instance.
114///
115/// Remember to call Initialize() if you plan to use this directly.
116std::unique_ptr<CodeGenerator>
117CreateLLVMCodeGen(const CompilerInstance &CI, llvm::StringRef ModuleName,
118 llvm::LLVMContext &C,
119 CoverageSourceInfo *CoverageInfo = nullptr);
120
121std::unique_ptr<CodeGenerator>
122CreateLLVMCodeGen(DiagnosticsEngine &Diags, llvm::StringRef ModuleName,
124 const HeaderSearchOptions &HeaderSearchOpts,
125 const PreprocessorOptions &PreprocessorOpts,
126 const CodeGenOptions &CGO, llvm::LLVMContext &C,
127 CoverageSourceInfo *CoverageInfo = nullptr);
128
129namespace CodeGen {
130/// Demangle the artificial function name (\param FuncName) used to encode trap
131/// reasons used in debug info for traps (e.g. __builtin_verbose_trap). See
132/// `CGDebugInfo::CreateTrapFailureMessageFor`.
133///
134/// \param FuncName - The function name to demangle.
135///
136/// \return A std::optional. If demangling succeeds the optional will contain
137/// a pair of StringRefs where the first field is the trap category and the
138/// second is the trap message. These can both be empty. If demangling fails the
139/// optional will not contain a value. Note the returned StringRefs if non-empty
140/// point into the underlying storage for \param FuncName and thus have the same
141/// lifetime.
142std::optional<std::pair<StringRef, StringRef>>
143DemangleTrapReasonInDebugInfo(StringRef FuncName);
144} // namespace CodeGen
145
146} // end namespace clang
147
148#endif
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
constexpr llvm::StringRef ClangTrapPrefix
ASTConsumer()=default
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition CGDebugInfo.h:59
This class organizes the cross-function state that is used while generating LLVM code.
const Decl * GetDeclForMangledName(llvm::StringRef MangledName)
Given a mangled name, return a declaration which mangles that way which has been added to this code g...
llvm::Module * StartModule(llvm::StringRef ModuleName, llvm::LLVMContext &C)
Create a new llvm::Module after calling HandleTranslationUnit.
CodeGenerator()=default
Use CreateLLVMCodeGen() below to create an instance of this class.
std::unique_ptr< llvm::Module > ReleaseModule()
Release ownership of the module to the caller.
llvm::Constant * GetAddrOfGlobal(GlobalDecl decl, bool isForDefinition)
Return the LLVM address of the given global entity.
llvm::StringRef GetMangledName(GlobalDecl GD)
Given a global declaration, return a mangled name for this declaration which has been added to this c...
CodeGen::CGDebugInfo * getCGDebugInfo()
Return debug info code generator.
bool IRGenFinished
True if we've finished generating IR.
CodeGen::CodeGenModule & CGM()
Return an opaque reference to the CodeGenModule object, which can be used in various secondary APIs.
llvm::Module * GetModule()
Return the module that this code generator is building into.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Stores additional source code information like skipped ranges which is required by the coverage mappi...
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:232
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
std::optional< std::pair< StringRef, StringRef > > DemangleTrapReasonInDebugInfo(StringRef FuncName)
Demangle the artificial function name (.
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
The JSON file list parser is used to communicate input to InstallAPI.
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition Linkage.h:54
std::unique_ptr< CodeGenerator > CreateLLVMCodeGen(const CompilerInstance &CI, llvm::StringRef ModuleName, llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo=nullptr)
CreateLLVMCodeGen - Create a CodeGenerator instance.
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30