{}
tl::expected<NodeId, DuplicateNameError>
-Rib::insert (std::string name, NodeId id)
+Rib::insert (std::string name, NodeId id, bool can_shadow)
{
auto res = values.insert ({name, id});
auto inserted_id = res.first->second;
+ auto existed = !res.second;
- // if we couldn't insert, the element already exists - exit with an error
- if (!res.second)
+ // if we couldn't insert, the element already exists - exit with an error,
+ // unless shadowing is allowed
+ if (existed && !can_shadow)
return tl::make_unexpected (DuplicateNameError (name, inserted_id));
// return the NodeId
ForwardTypeParamBan,
/* Const generic, as in the following example: fn foo<T, const X: T>() {} */
ConstParamType,
- };
+ } kind;
Rib (Kind kind);
Rib (Kind kind, std::string identifier, NodeId id);
*
* @param name The name associated with the AST node
* @param id Its NodeId
+ * @param can_shadow If the newly inserted value can shadow an existing one
*
* @return `DuplicateNameError` if the node is already present in the rib. The
* `DuplicateNameError` class contains the NodeId of the existing
* node. Returns the new NodeId on success.
*/
- tl::expected<NodeId, DuplicateNameError> insert (std::string name, NodeId id);
+ tl::expected<NodeId, DuplicateNameError> insert (std::string name, NodeId id,
+ bool can_shadow = false);
/**
* Access an inserted NodeId.
const std::unordered_map<std::string, NodeId> &get_values () const;
private:
- Kind kind;
std::unordered_map<std::string, NodeId> values;
};