| 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 | #include "llvm/AsmParser/AsmParserContext.h" |
| 10 | |
| 11 | namespace llvm { |
| 12 | |
| 13 | std::optional<FileLocRange> |
| 14 | AsmParserContext::getFunctionLocation(const Function *F) const { |
| 15 | if (auto FIt = Functions.find(Val: F); FIt != Functions.end()) |
| 16 | return FIt->second; |
| 17 | return std::nullopt; |
| 18 | } |
| 19 | |
| 20 | std::optional<FileLocRange> |
| 21 | AsmParserContext::getBlockLocation(const BasicBlock *BB) const { |
| 22 | if (auto BBIt = Blocks.find(Val: BB); BBIt != Blocks.end()) |
| 23 | return BBIt->second; |
| 24 | return std::nullopt; |
| 25 | } |
| 26 | |
| 27 | std::optional<FileLocRange> |
| 28 | AsmParserContext::getInstructionOrArgumentLocation(const Value *IA) const { |
| 29 | assert(isa<Instruction>(IA) || isa<Argument>(IA)); |
| 30 | if (auto IIt = InstructionsAndArguments.find(Val: IA); |
| 31 | IIt != InstructionsAndArguments.end()) |
| 32 | return IIt->second; |
| 33 | return std::nullopt; |
| 34 | } |
| 35 | |
| 36 | Function * |
| 37 | AsmParserContext::getFunctionAtLocation(const FileLocRange &Query) const { |
| 38 | auto It = FunctionsInverse.find(x: Query.Start); |
| 39 | if (It.stop() <= Query.End) |
| 40 | return *It; |
| 41 | return nullptr; |
| 42 | } |
| 43 | |
| 44 | Function *AsmParserContext::getFunctionAtLocation(const FileLoc &Query) const { |
| 45 | return FunctionsInverse.lookup(x: Query, NotFound: nullptr); |
| 46 | } |
| 47 | |
| 48 | BasicBlock * |
| 49 | AsmParserContext::getBlockAtLocation(const FileLocRange &Query) const { |
| 50 | auto It = BlocksInverse.find(x: Query.Start); |
| 51 | if (It.stop() <= Query.End) |
| 52 | return *It; |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
| 56 | BasicBlock *AsmParserContext::getBlockAtLocation(const FileLoc &Query) const { |
| 57 | return BlocksInverse.lookup(x: Query, NotFound: nullptr); |
| 58 | } |
| 59 | |
| 60 | Value *AsmParserContext::getInstructionOrArgumentAtLocation( |
| 61 | const FileLocRange &Query) const { |
| 62 | auto It = InstructionsAndArgumentsInverse.find(x: Query.Start); |
| 63 | if (It.stop() <= Query.End) |
| 64 | return *It; |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
| 68 | Value *AsmParserContext::getInstructionOrArgumentAtLocation( |
| 69 | const FileLoc &Query) const { |
| 70 | return InstructionsAndArgumentsInverse.lookup(x: Query, NotFound: nullptr); |
| 71 | } |
| 72 | |
| 73 | Value *AsmParserContext::getValueReferencedAtLocation( |
| 74 | const FileLocRange &Query) const { |
| 75 | auto It = ReferencedValues.find(x: Query.Start); |
| 76 | if (It.stop() <= Query.End) |
| 77 | return *It; |
| 78 | return nullptr; |
| 79 | } |
| 80 | |
| 81 | Value * |
| 82 | AsmParserContext::getValueReferencedAtLocation(const FileLoc &Query) const { |
| 83 | return ReferencedValues.lookup(x: Query, NotFound: nullptr); |
| 84 | } |
| 85 | |
| 86 | bool AsmParserContext::addFunctionLocation(Function *F, |
| 87 | const FileLocRange &Loc) { |
| 88 | bool Inserted = Functions.insert(KV: {F, Loc}).second; |
| 89 | if (Inserted) |
| 90 | FunctionsInverse.insert(a: Loc.Start, b: Loc.End, y: F); |
| 91 | return Inserted; |
| 92 | } |
| 93 | |
| 94 | bool AsmParserContext::addBlockLocation(BasicBlock *BB, |
| 95 | const FileLocRange &Loc) { |
| 96 | bool Inserted = Blocks.insert(KV: {BB, Loc}).second; |
| 97 | if (Inserted) |
| 98 | BlocksInverse.insert(a: Loc.Start, b: Loc.End, y: BB); |
| 99 | return Inserted; |
| 100 | } |
| 101 | |
| 102 | bool AsmParserContext::addInstructionOrArgumentLocation( |
| 103 | Value *IA, const FileLocRange &Loc) { |
| 104 | assert(isa<Instruction>(IA) || isa<Argument>(IA)); |
| 105 | bool Inserted = InstructionsAndArguments.insert(KV: {IA, Loc}).second; |
| 106 | if (Inserted) |
| 107 | InstructionsAndArgumentsInverse.insert(a: Loc.Start, b: Loc.End, y: IA); |
| 108 | return Inserted; |
| 109 | } |
| 110 | |
| 111 | bool AsmParserContext::addValueReferenceAtLocation(Value *V, |
| 112 | const FileLocRange &Loc) { |
| 113 | ReferencedValues.insert(a: Loc.Start, b: Loc.End, y: V); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | } // namespace llvm |
| 118 | |