1//===----------------------------------------------------------------------===//
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#ifndef LLVM_ASMPARSER_ASMPARSERCONTEXT_H
10#define LLVM_ASMPARSER_ASMPARSERCONTEXT_H
11
12#include "llvm/ADT/DenseMap.h"
13#include "llvm/ADT/IntervalMap.h"
14#include "llvm/AsmParser/FileLoc.h"
15#include "llvm/IR/Value.h"
16#include <optional>
17
18namespace llvm {
19class BasicBlock;
20
21/// Registry of file location information for LLVM IR constructs.
22///
23/// This class provides access to the file location information
24/// for various LLVM IR constructs. Currently, it supports Function,
25/// BasicBlock and Instruction locations.
26///
27/// When available, it can answer queries about what is at a given
28/// file location, as well as where in a file a given IR construct
29/// is.
30///
31/// This information is optionally emitted by the LLParser while
32/// it reads LLVM textual IR.
33class AsmParserContext {
34 using FMap =
35 IntervalMap<FileLoc, Function *,
36 IntervalMapImpl::NodeSizer<FileLoc, Function *>::LeafSize,
37 IntervalMapHalfOpenInfo<FileLoc>>;
38
39 DenseMap<Function *, FileLocRange> Functions;
40 FMap::Allocator FAllocator;
41 FMap FunctionsInverse = FMap(FAllocator);
42
43 DenseMap<BasicBlock *, FileLocRange> Blocks;
44 using BBMap =
45 IntervalMap<FileLoc, BasicBlock *,
46 IntervalMapImpl::NodeSizer<FileLoc, BasicBlock *>::LeafSize,
47 IntervalMapHalfOpenInfo<FileLoc>>;
48 BBMap::Allocator BBAllocator;
49 BBMap BlocksInverse = BBMap(BBAllocator);
50 DenseMap<Value *, FileLocRange> InstructionsAndArguments;
51 using VMap =
52 IntervalMap<FileLoc, Value *,
53 IntervalMapImpl::NodeSizer<FileLoc, Value *>::LeafSize,
54 IntervalMapHalfOpenInfo<FileLoc>>;
55 VMap::Allocator VAllocator;
56 VMap InstructionsAndArgumentsInverse = VMap(VAllocator);
57
58 VMap ReferencedValues = VMap(VAllocator);
59
60public:
61 LLVM_ABI std::optional<FileLocRange>
62 getFunctionLocation(const Function *) const;
63 LLVM_ABI std::optional<FileLocRange>
64 getBlockLocation(const BasicBlock *) const;
65 LLVM_ABI std::optional<FileLocRange>
66 getInstructionOrArgumentLocation(const Value *) const;
67 /// Get the function at the requested location range.
68 /// If no single function occupies the queried range, or the record is
69 /// missing, a nullptr is returned.
70 LLVM_ABI Function *getFunctionAtLocation(const FileLocRange &) const;
71 /// Get the function at the requested location.
72 /// If no function occupies the queried location, or the record is missing, a
73 /// nullptr is returned.
74 LLVM_ABI Function *getFunctionAtLocation(const FileLoc &) const;
75 /// Get the block at the requested location range.
76 /// If no single block occupies the queried range, or the record is missing, a
77 /// nullptr is returned.
78 LLVM_ABI BasicBlock *getBlockAtLocation(const FileLocRange &) const;
79 /// Get the block at the requested location.
80 /// If no block occupies the queried location, or the record is missing, a
81 /// nullptr is returned.
82 LLVM_ABI BasicBlock *getBlockAtLocation(const FileLoc &) const;
83 /// Get the instruction or function argument at the requested location range.
84 /// If no single instruction occupies the queried range, or the record is
85 /// missing, a nullptr is returned.
86 LLVM_ABI Value *
87 getInstructionOrArgumentAtLocation(const FileLocRange &) const;
88 /// Get the instruction or function argument at the requested location.
89 /// If no instruction occupies the queried location, or the record is missing,
90 /// a nullptr is returned.
91 LLVM_ABI Value *getInstructionOrArgumentAtLocation(const FileLoc &) const;
92 /// Get value referenced at the requested location.
93 /// If no value occupies the queried location, or the record is missing,
94 /// a nullptr is returned.
95 LLVM_ABI Value *getValueReferencedAtLocation(const FileLoc &) const;
96 /// Get value referenced at the requested location range.
97 /// If no value occupies the queried location, or the record is missing,
98 /// a nullptr is returned.
99 LLVM_ABI Value *getValueReferencedAtLocation(const FileLocRange &) const;
100 LLVM_ABI bool addFunctionLocation(Function *, const FileLocRange &);
101 LLVM_ABI bool addBlockLocation(BasicBlock *, const FileLocRange &);
102 LLVM_ABI bool addInstructionOrArgumentLocation(Value *, const FileLocRange &);
103 LLVM_ABI bool addValueReferenceAtLocation(Value *, const FileLocRange &);
104};
105} // namespace llvm
106
107#endif
108