xref: /freebsd/contrib/llvm-project/llvm/lib/Bitcode/Reader/MetadataLoader.h (revision e1c4c8dd8d2d10b6104f06856a77bd5b4813a801)
1 //===-- Bitcode/Reader/MetadataLoader.h - Load Metadatas -------*- 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 class handles loading Metadatas.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_BITCODE_READER_METADATALOADER_H
14 #define LLVM_LIB_BITCODE_READER_METADATALOADER_H
15 
16 #include "llvm/Support/Error.h"
17 
18 #include <functional>
19 #include <memory>
20 
21 namespace llvm {
22 class BasicBlock;
23 class BitcodeReaderValueList;
24 class BitstreamCursor;
25 class DISubprogram;
26 class Function;
27 class Instruction;
28 class Metadata;
29 class Module;
30 class Type;
31 template <typename T> class ArrayRef;
32 
33 typedef std::function<Type *(unsigned)> GetTypeByIDTy;
34 
35 typedef std::function<unsigned(unsigned, unsigned)> GetContainedTypeIDTy;
36 
37 typedef std::function<void(Metadata **, unsigned, GetTypeByIDTy,
38                            GetContainedTypeIDTy)>
39     MDTypeCallbackTy;
40 
41 struct MetadataLoaderCallbacks {
42   GetTypeByIDTy GetTypeByID;
43   GetContainedTypeIDTy GetContainedTypeID;
44   std::optional<MDTypeCallbackTy> MDType;
45 };
46 
47 /// Helper class that handles loading Metadatas and keeping them available.
48 class MetadataLoader {
49   class MetadataLoaderImpl;
50   std::unique_ptr<MetadataLoaderImpl> Pimpl;
51   Error parseMetadata(bool ModuleLevel);
52 
53 public:
54   ~MetadataLoader();
55   MetadataLoader(BitstreamCursor &Stream, Module &TheModule,
56                  BitcodeReaderValueList &ValueList, bool IsImporting,
57                  MetadataLoaderCallbacks Callbacks);
58   MetadataLoader &operator=(MetadataLoader &&);
59   MetadataLoader(MetadataLoader &&);
60 
61   // Parse a module metadata block
62   Error parseModuleMetadata() { return parseMetadata(true); }
63 
64   // Parse a function metadata block
65   Error parseFunctionMetadata() { return parseMetadata(false); }
66 
67   /// Set the mode to strip TBAA metadata on load.
68   void setStripTBAA(bool StripTBAA = true);
69 
70   /// Return true if the Loader is stripping TBAA metadata.
71   bool isStrippingTBAA();
72 
73   // Return true there are remaining unresolved forward references.
74   bool hasFwdRefs() const;
75 
76   /// Return the given metadata, creating a replaceable forward reference if
77   /// necessary.
78   Metadata *getMetadataFwdRefOrLoad(unsigned Idx);
79 
80   /// Return the DISubprogram metadata for a Function if any, null otherwise.
81   DISubprogram *lookupSubprogramForFunction(Function *F);
82 
83   /// Parse a `METADATA_ATTACHMENT` block for a function.
84   Error parseMetadataAttachment(Function &F,
85                                 ArrayRef<Instruction *> InstructionList);
86 
87   /// Parse a `METADATA_KIND` block for the current module.
88   Error parseMetadataKinds();
89 
90   unsigned size() const;
91   void shrinkTo(unsigned N);
92 
93   /// Perform bitcode upgrades on llvm.dbg.* calls.
94   void upgradeDebugIntrinsics(Function &F);
95 };
96 }
97 
98 #endif // LLVM_LIB_BITCODE_READER_METADATALOADER_H
99