Spicy
scope.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <map>
6 #include <memory>
7 #include <optional>
8 #include <string>
9 #include <unordered_map>
10 #include <utility>
11 #include <vector>
12 
13 #include <hilti/ast/node-ref.h>
14 #include <hilti/base/intrusive-ptr.h>
15 
16 namespace hilti {
17 
18 class Declaration;
19 class ID;
20 
26 public:
27  Scope() = default;
28  ~Scope() = default;
29 
30  void insert(NodeRef&& n);
31  void insert(ID id, NodeRef&& n);
32  void insertNotFound(ID id);
33 
35  bool has(const ID& id) const { return ! _findID(id).empty(); }
36 
38  struct Referee {
40  std::string qualified;
41  bool external{};
42  };
43 
45  std::vector<Referee> lookupAll(const ID& id) const { return _findID(id); }
46 
48  std::optional<Referee> lookup(const ID& id) const {
49  if ( auto ids = _findID(id); ! ids.empty() )
50  return ids.front();
51 
52  return {};
53  }
54 
56  void clear() { _items.clear(); }
57 
59  const auto& items() const { return _items; }
60 
67  void render(std::ostream& out, const std::string& prefix = "") const;
68 
69  Scope(const Scope& other) = delete;
70  Scope(Scope&& other) = delete;
71  Scope& operator=(const Scope& other) = delete;
72  Scope& operator=(Scope&& other) = delete;
73 
74 private:
75  using ItemMap = std::map<std::string, std::vector<NodeRef>>;
76 
77  std::vector<Referee> _findID(const ID& id, bool external = false) const;
78  std::vector<Referee> _findID(const Scope* scope, const ID& id, bool external = false) const;
79 
80  ItemMap _items;
81 };
82 
83 } // namespace hilti
bool has(const ID &id) const
Definition: scope.h:35
Definition: intrusive-ptr.h:28
NodeRef node
Definition: scope.h:39
const auto & items() const
Definition: scope.h:59
std::optional< Referee > lookup(const ID &id) const
Definition: scope.h:48
Definition: scope.h:25
std::string qualified
Definition: scope.h:40
Definition: scope.h:38
void clear()
Definition: scope.h:56
Definition: node-ref.h:44
void render(std::ostream &out, const std::string &prefix="") const
Definition: scope.cc:100
std::vector< Referee > lookupAll(const ID &id) const
Definition: scope.h:45
Definition: id.h:18
bool external
Definition: scope.h:41