Coverage Report

Created: 2023-12-11 06:17

/proc/self/cwd/src/ir/selector.h
Line
Count
Source (jump to first uncovered line)
1
//-----------------------------------------------------------------------------
2
// Copyright 2021 Google LLC
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
//     https://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
//----------------------------------------------------------------------------
16
#ifndef SRC_FRONTENDS_ARCS_SELECTOR_H_
17
#define SRC_FRONTENDS_ARCS_SELECTOR_H_
18
19
#include <string>
20
#include <variant>
21
22
#include "src/common/logging/logging.h"
23
#include "src/ir/field_selector.h"
24
25
namespace raksha::ir {
26
27
// An individual selector in an AccessPath descending down the type tree.
28
// This Selector is generic, and may contain any method of descending, such
29
// as field access, array access, tuple element access, etc.
30
//
31
// For now, however, we only have implemented the FieldSelector portion.
32
class Selector {
33
 public:
34
  explicit Selector(FieldSelector field_selector)
35
0
      : specific_selector_(std::move(field_selector)) {}
36
37
0
  std::variant<FieldSelector> variant() const { return specific_selector_; }
38
39
  // Print the string representing a selector's participation in the
40
  // AccessPath. This will include the punctuation indicating the method of
41
  // selection (such as . for string, [ for index, etc) and the string
42
  // contents of the selector itself.
43
0
  std::string ToString() const {
44
0
    return std::visit(
45
0
        [](const auto &specific_selector) {
46
0
          return specific_selector.ToString();
47
0
        },
48
0
        specific_selector_);
49
0
  }
50
51
  // Whether two selectors are equal. Will be true if they are the same type
52
  // of selector and those two types also compare equal.
53
  //
54
  // std::variant has a pre-packaged operator equals that handles the dispatch
55
  // to the specific selector as we would expect.
56
0
  bool operator==(const Selector &other) const {
57
0
    return specific_selector_ == other.specific_selector_;
58
0
  }
59
60
  template <typename H>
61
0
  friend H AbslHashValue(H h, const Selector &selector) {
62
0
    return H::combine(std::move(h), selector.specific_selector_);
63
0
  }
64
65
 private:
66
  // The variant storing the specific type of selector. We will dispatch
67
  // through this to perform all methods on the generic Selector.
68
  std::variant<FieldSelector> specific_selector_;
69
};
70
71
}  // namespace raksha::ir
72
73
#endif  // SRC_FRONTENDS_ARCS_SELECTOR_H_