Bash++
Bash++ compiler internal documentation
EntityMap.h
Go to the documentation of this file.
1
6#pragma once
7#include <optional>
8#include "EntityNode.h"
9
10using IntervalTree = lib_interval_tree::interval_tree<IntervalType, EntityHooks>;
11
13 uint32_t line;
14 uint32_t column;
15
16 operator uint64_t() const {
17 // Encode line and column into a single uint64_t
18 // The first 32 bits represent the line number,
19 // and the last 32 bits represent the column number.
20 // This permits perfect sorting of file positions
21 return (static_cast<uint64_t>(line) << 32) | column;
22 }
23
24 FilePosition(uint32_t line, uint32_t column)
25 : line(line), column(column) {}
26};
27
28class EntityMap {
29 private:
31 public:
32
36 void insert(FilePosition start, FilePosition end, const std::shared_ptr<bpp::bpp_entity>& entity) {
37 auto it = tree.insert({start, end});
38 it.node()->payload = entity;
39 }
40
41 std::shared_ptr<bpp::bpp_entity> find(FilePosition point) {
42 // Because of our strictly-nested tree structure,
43 // The innermost active entity at a given point
44 // Is guaranteed to be the entity which overlaps that point and has the highest start position
45 // We can only make this assumption safely because code entities cannot overlap in arbitrary ways
46 // They can only overlap in a way such that one entity is fully contained within another
47 //
48 // Eg, we can only have structures such as [0 [100 [300 350] 500] 1000]
49 // Imagine we're looking for the innermost active entity at point 301
50 // All of the above entities overlap that point, but the innermost active entity is the one starting at 300
51 // Ie, the one with the highest start position
52 //
53 // It is impossible in our case to have any other kind of overlap, such as [0 100] and [50 150]
54 // (Where an inner entity is not *entirely* contained within the outer entity)
55 // If that were possible, this method would not work correctly
56 std::optional<uint64_t> highestStart = std::nullopt;
57 std::shared_ptr<bpp::bpp_entity> innermostEntity;
58 tree.overlap_find_all({point, point}, [&](auto const& it) {
59 // This is also based on the assumption (valid in our case)
60 // That no two entities can start at the same point
61 if (!highestStart.has_value() || it->low() > highestStart) {
62 highestStart = it->low();
63 innermostEntity = it.node()->payload;
64 }
65 return true; // Continue searching
66 });
67 return innermostEntity;
68 }
69
73 std::shared_ptr<bpp::bpp_entity> find(uint32_t line, uint32_t column) {
74 return find(FilePosition(line, column));
75 }
76};
lib_interval_tree::interval_tree< IntervalType, EntityHooks > IntervalTree
Definition EntityMap.h:10
Definition EntityMap.h:28
IntervalTree tree
Definition EntityMap.h:30
void insert(FilePosition start, FilePosition end, const std::shared_ptr< bpp::bpp_entity > &entity)
Add an entity to the entity map.
Definition EntityMap.h:36
std::shared_ptr< bpp::bpp_entity > find(uint32_t line, uint32_t column)
Find the active code entity at a specific line and column.
Definition EntityMap.h:73
std::shared_ptr< bpp::bpp_entity > find(FilePosition point)
Definition EntityMap.h:41
Definition EntityMap.h:12
uint32_t line
Definition EntityMap.h:13
FilePosition(uint32_t line, uint32_t column)
Definition EntityMap.h:24
uint32_t column
Definition EntityMap.h:14