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 {
35class BaseSubobject;
36class CodeGenOptions;
37class CoverageSourceInfo;
38class Decl;
39class DiagnosticsEngine;
40class GlobalDecl;
41class HeaderSearchOptions;
42class LangOptions;
43class PreprocessorOptions;
44class CompilerInstance;
45
46namespace CodeGen {
47 class CodeGenModule;
48 class CGDebugInfo;
49}
50
51/// The primary public interface to the Clang code generator.
52class CodeGenerator : public ASTConsumer {
53 virtual void anchor();
54
55protected:
56 /// Use CreateLLVMCodeGen() below to create an instance of this class.
57 CodeGenerator() = default;
58
59 /// True if we've finished generating IR. This prevents us from generating
60 /// additional LLVM IR after emitting output in HandleTranslationUnit. This
61 /// can happen when Clang plugins trigger additional AST deserialization.
62 bool IRGenFinished = false;
63
64public:
65 /// Return an opaque reference to the CodeGenModule object, which can
66 /// be used in various secondary APIs. It is valid as long as the
67 /// CodeGenerator exists.
69
70 /// Return the module that this code generator is building into.
71 ///
72 /// This may return null after HandleTranslationUnit is called;
73 /// this signifies that there was an error generating code. A
74 /// diagnostic will have been generated in this case, and the module
75 /// will be deleted.
76 ///
77 /// It will also return null if the module is released.
78 llvm::Module *GetModule();
79
80 /// Release ownership of the module to the caller.
81 ///
82 /// It is illegal to call methods other than GetModule on the
83 /// CodeGenerator after releasing its module.
84 std::unique_ptr<llvm::Module> ReleaseModule();
85
86 /// Return debug info code generator.
88
89 /// Given a mangled name, return a declaration which mangles that way
90 /// which has been added to this code generator via a Handle method.
91 ///
92 /// This may return null if there was no matching declaration.
93 const Decl *GetDeclForMangledName(llvm::StringRef MangledName);
94
95 /// Given a global declaration, return a mangled name for this declaration
96 /// which has been added to this code generator via a Handle method.
97 llvm::StringRef GetMangledName(GlobalDecl GD);
98
99 /// Return the LLVM address of the given global entity.
100 ///
101 /// \param isForDefinition If true, the caller intends to define the
102 /// entity; the object returned will be an llvm::GlobalValue of
103 /// some sort. If false, the caller just intends to use the entity;
104 /// the object returned may be any sort of constant value, and the
105 /// code generator will schedule the entity for emission if a
106 /// definition has been registered with this code generator.
107 llvm::Constant *GetAddrOfGlobal(GlobalDecl decl, bool isForDefinition);
108
109 /// Return the LLVM address of the vtable for the given base subobject.
110 ///
111 /// \param base The base subobject that owns the vptr to be initialized.
112 /// \param decl The derived type being initialized, that contains `base`.
113 llvm::Constant *GetAddrOfVTable(BaseSubobject base,
114 const CXXRecordDecl *decl);
115
116 /// Create a new \c llvm::Module after calling HandleTranslationUnit. This
117 /// enable codegen in interactive processing environments.
118 llvm::Module* StartModule(llvm::StringRef ModuleName, llvm::LLVMContext &C);
119};
120
121/// CreateLLVMCodeGen - Create a CodeGenerator instance.
122///
123/// Remember to call Initialize() if you plan to use this directly.
124std::unique_ptr<CodeGenerator>
125CreateLLVMCodeGen(const CompilerInstance &CI, llvm::StringRef ModuleName,
126 llvm::LLVMContext &C,
127 CoverageSourceInfo *CoverageInfo = nullptr);
128
129std::unique_ptr<CodeGenerator>
130CreateLLVMCodeGen(DiagnosticsEngine &Diags, llvm::StringRef ModuleName,
132 const HeaderSearchOptions &HeaderSearchOpts,
133 const PreprocessorOptions &PreprocessorOpts,
134 const CodeGenOptions &CGO, llvm::LLVMContext &C,
135 CoverageSourceInfo *CoverageInfo = nullptr);
136
137namespace CodeGen {
138/// Demangle the artificial function name (\param FuncName) used to encode trap
139/// reasons used in debug info for traps (e.g. __builtin_verbose_trap). See
140/// `CGDebugInfo::CreateTrapFailureMessageFor`.
141///
142/// \param FuncName - The function name to demangle.
143///
144/// \return A std::optional. If demangling succeeds the optional will contain
145/// a pair of StringRefs where the first field is the trap category and the
146/// second is the trap message. These can both be empty. If demangling fails the
147/// optional will not contain a value. Note the returned StringRefs if non-empty
148/// point into the underlying storage for \param FuncName and thus have the same
149/// lifetime.
150std::optional<std::pair<StringRef, StringRef>>
151DemangleTrapReasonInDebugInfo(StringRef FuncName);
152} // namespace CodeGen
153
154} // end namespace clang
155
156#endif
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
constexpr llvm::StringRef ClangTrapPrefix
ASTConsumer()=default
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
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...
llvm::Constant * GetAddrOfVTable(BaseSubobject base, const CXXRecordDecl *decl)
Return the LLVM address of the vtable for the given base subobject.
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:233
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