Coverage Report

Created: 2023-12-11 06:17

/proc/self/cwd/src/ir/data_decl.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_IR_DATA_DECL_H_
17
#define SRC_IR_DATA_DECL_H_
18
19
#include "absl/container/flat_hash_map.h"
20
#include "src/ir/types/type.h"
21
22
namespace raksha::ir {
23
24
// A data declaration, which corresponds to an input or output of a block.
25
class DataDecl {
26
 public:
27
  DataDecl(absl::string_view name, types::Type type)
28
0
      : name_(name), type_(std::move(type)) {}
29
30
0
  absl::string_view name() const { return name_; }
31
0
  const types::Type& type() const { return type_; }
32
33
 private:
34
  std::string name_;
35
  types::Type type_;
36
};
37
38
class DataDeclCollection {
39
 public:
40
  // Adds a declaration to the collection. Fails if there is already a
41
  // declaration with the given name.
42
0
  const DataDecl& AddDecl(absl::string_view name, types::Type type) {
43
0
    auto insertion_result = decls_.insert(
44
0
        {std::string(name), std::make_unique<DataDecl>(name, std::move(type))});
45
0
    CHECK(insertion_result.second)
46
0
        << "Adding a duplicate declaration for `" << name << "`.";
47
0
    return *(insertion_result.first)->second.get();
48
0
  }
49
50
0
  const DataDecl* FindDecl(absl::string_view name) const {
51
0
    auto find_result = decls_.find(name);
52
0
    return (find_result == decls_.end()) ? nullptr : find_result->second.get();
53
0
  }
54
55
 private:
56
  absl::flat_hash_map<std::string, std::unique_ptr<DataDecl>> decls_;
57
};
58
59
}  // namespace raksha::ir
60
61
#endif  // SRC_IR_DATA_DECL_H_