aboutsummaryrefslogtreecommitdiff
path: root/substitution.h
blob: 67f74b46fa6b78dfc98e905dab17f88132a8e641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -*- mode: C++ -*-
//
// Copyright 2022 Google LLC
//
// Licensed under the Apache License v2.0 with LLVM Exceptions (the
// "License"); you may not use this file except in compliance with the
// License.  You may obtain a copy of the License at
//
//     https://llvm.org/LICENSE.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Giuliano Procida

#ifndef STG_SUBSTITUTION_H_
#define STG_SUBSTITUTION_H_

#include <map>
#include <vector>

#include "graph.h"

namespace stg {

// This is a single-node node id substitution function that updates all node
// references according to a given mapping. The caller is responsible for
// determining the nodes to which substitution should apply (e.g., excluding
// orphaned nodes).
//
// The caller must provide a reference to a callable object which should update
// its Id argument only when needed (i.e., when the new value is different).
//
// The Update helpers may be used to update external node id references.
template <typename Updater>
struct Substitute {
  Substitute(Graph& graph, const Updater& updater)
      : graph(graph), updater(updater) {}

  void Update(Id& id) const {
    updater(id);
  }

  void Update(std::vector<Id>& ids) const {
    for (auto& id : ids) {
      Update(id);
    }
  }

  template <typename Key>
  void Update(std::map<Key, Id>& ids) const {
    for (auto& [key, id] : ids) {
      Update(id);
    }
  }

  void operator()(Id id) {
    return graph.Apply<void>(*this, id);
  }

  void operator()(Special&) {}

  void operator()(PointerReference& x) {
    Update(x.pointee_type_id);
  }

  void operator()(PointerToMember& x) {
    Update(x.containing_type_id);
    Update(x.pointee_type_id);
  }

  void operator()(Typedef& x) {
    Update(x.referred_type_id);
  }

  void operator()(Qualified& x) {
    Update(x.qualified_type_id);
  }

  void operator()(Primitive&) {}

  void operator()(Array& x) {
    Update(x.element_type_id);
  }

  void operator()(BaseClass& x) {
    Update(x.type_id);
  }

  void operator()(Method& x) {
    Update(x.type_id);
  }

  void operator()(Member& x) {
    Update(x.type_id);
  }

  void operator()(StructUnion& x) {
    if (x.definition.has_value()) {
      auto& definition = x.definition.value();
      Update(definition.base_classes);
      Update(definition.methods);
      Update(definition.members);
    }
  }

  void operator()(Enumeration& x) {
    if (x.definition.has_value()) {
      auto& definition = x.definition.value();
      Update(definition.underlying_type_id);
    }
  }

  void operator()(Function& x) {
    Update(x.parameters);
    Update(x.return_type_id);
  }

  void operator()(ElfSymbol& x) {
    if (x.type_id) {
      Update(*x.type_id);
    }
  }

  void operator()(Interface& x) {
    Update(x.symbols);
    Update(x.types);
  }

  Graph& graph;
  const Updater& updater;
};

template <class Updater>
Substitute(Graph&, const Updater& updater) -> Substitute<decltype(updater)>;

}  // namespace stg

#endif  // STG_SUBSTITUTION_H_