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:
parent
4ebede411e
commit
36747d7046
|
@ -29,22 +29,26 @@ NodeIdMap::NodeIdMap(const tint::Program& program) : NodeIdMap() {
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeIdMap::IdType NodeIdMap::GetId(const ast::Node* node) const {
|
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;
|
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);
|
auto it = id_to_node_.find(id);
|
||||||
return it == id_to_node_.end() ? nullptr : it->second;
|
return it == id_to_node_.end() ? nullptr : it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeIdMap::Add(const ast::Node* node, IdType id) {
|
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(IdIsFreshAndValid(id) && "Id already exists in the map or Id is zero");
|
||||||
assert(node && "`node` can't be a nullptr");
|
assert(node && "`node` can't be a nullptr");
|
||||||
|
|
||||||
node_to_id_[node] = id;
|
node_to_id_[casted_node] = id;
|
||||||
id_to_node_[id] = node;
|
id_to_node_[id] = casted_node;
|
||||||
|
|
||||||
if (id >= fresh_id_) {
|
if (id >= fresh_id_) {
|
||||||
fresh_id_ = id + 1;
|
fresh_id_ = id + 1;
|
||||||
|
|
|
@ -54,7 +54,7 @@ class NodeIdMap {
|
||||||
/// @param id - any value is accepted.
|
/// @param id - any value is accepted.
|
||||||
/// @return a pointer to some node if `id` exists in this map.
|
/// @return a pointer to some node if `id` exists in this map.
|
||||||
/// @return `nullptr` otherwise.
|
/// @return `nullptr` otherwise.
|
||||||
const ast::Node* GetNode(IdType id) const;
|
ast::Node* GetNode(IdType id) const;
|
||||||
|
|
||||||
/// @brief Returns an id of the given `node`.
|
/// @brief Returns an id of the given `node`.
|
||||||
/// @param node - can be a `nullptr`.
|
/// @param node - can be a `nullptr`.
|
||||||
|
@ -85,8 +85,8 @@ class NodeIdMap {
|
||||||
private:
|
private:
|
||||||
IdType fresh_id_ = 1;
|
IdType fresh_id_ = 1;
|
||||||
|
|
||||||
std::unordered_map<const ast::Node*, IdType> node_to_id_;
|
std::unordered_map<ast::Node*, IdType> node_to_id_;
|
||||||
std::unordered_map<IdType, const ast::Node*> id_to_node_;
|
std::unordered_map<IdType, ast::Node*> id_to_node_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ast_fuzzer
|
} // namespace ast_fuzzer
|
||||||
|
|
Loading…
Reference in New Issue