Remove const from GetNode return type

This is to make sure that compiler won't crash when creating data
types using the result from GetNode function from node_id_map.

Change-Id: I96fad13d3494de4808e29d6952e5e88e697f8516
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/61381
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Vasyl Teliman <vasniktel@gmail.com>
Reviewed-by: Paul Thomson <paulthomson@google.com>
Commit-Queue: Paul Thomson <paulthomson@google.com>
This commit is contained in:
Shiyu Liu 2021-08-11 16:43:45 +00:00 committed by Tint LUCI CQ
parent 4ebede411e
commit 36747d7046
2 changed files with 12 additions and 8 deletions

View File

@ -29,22 +29,26 @@ NodeIdMap::NodeIdMap(const tint::Program& program) : NodeIdMap() {
}
NodeIdMap::IdType NodeIdMap::GetId(const ast::Node* node) const {
auto it = node_to_id_.find(node);
// Since node is immutable by default, const_cast won't
// modify the node structure.
auto it = node_to_id_.find(const_cast<ast::Node*>(node));
return it == node_to_id_.end() ? 0 : it->second;
}
const ast::Node* NodeIdMap::GetNode(IdType id) const {
ast::Node* NodeIdMap::GetNode(IdType id) const {
auto it = id_to_node_.find(id);
return it == id_to_node_.end() ? nullptr : it->second;
}
void NodeIdMap::Add(const ast::Node* node, IdType id) {
assert(!node_to_id_.count(node) && "The node already exists in the map");
auto* casted_node = const_cast<ast::Node*>(node);
assert(!node_to_id_.count(casted_node) &&
"The node already exists in the map");
assert(IdIsFreshAndValid(id) && "Id already exists in the map or Id is zero");
assert(node && "`node` can't be a nullptr");
node_to_id_[node] = id;
id_to_node_[id] = node;
node_to_id_[casted_node] = id;
id_to_node_[id] = casted_node;
if (id >= fresh_id_) {
fresh_id_ = id + 1;

View File

@ -54,7 +54,7 @@ class NodeIdMap {
/// @param id - any value is accepted.
/// @return a pointer to some node if `id` exists in this map.
/// @return `nullptr` otherwise.
const ast::Node* GetNode(IdType id) const;
ast::Node* GetNode(IdType id) const;
/// @brief Returns an id of the given `node`.
/// @param node - can be a `nullptr`.
@ -85,8 +85,8 @@ class NodeIdMap {
private:
IdType fresh_id_ = 1;
std::unordered_map<const ast::Node*, IdType> node_to_id_;
std::unordered_map<IdType, const ast::Node*> id_to_node_;
std::unordered_map<ast::Node*, IdType> node_to_id_;
std::unordered_map<IdType, ast::Node*> id_to_node_;
};
} // namespace ast_fuzzer