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