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