@@ -437,6 +437,21 @@ public:
*/
tl::expected<NodeId, DuplicateNameError> insert (Identifier name, NodeId id);
+ /**
+ * Insert a new shadowable definition in the innermost `Rib` in this stack
+ *
+ * @param name The name of the definition
+ * @param id Its NodeId
+ *
+ * @return `DuplicateNameError` if that node was already present in the Rib,
+ * the node's `NodeId` otherwise.
+ *
+ * @aborts if there are no `Rib`s inserted in the current map, this function
+ * aborts the program.
+ */
+ tl::expected<NodeId, DuplicateNameError> insert_shadowable (Identifier name,
+ NodeId id);
+
/**
* Insert a new definition at the root of this stack
*
@@ -119,6 +119,16 @@ ForeverStack<N>::insert (Identifier name, NodeId node)
Rib::Definition::NonShadowable (node));
}
+template <Namespace N>
+tl::expected<NodeId, DuplicateNameError>
+ForeverStack<N>::insert_shadowable (Identifier name, NodeId node)
+{
+ auto &innermost_rib = peek ();
+
+ return insert_inner (innermost_rib, name.as_string (),
+ Rib::Definition::Shadowable (node));
+}
+
template <Namespace N>
tl::expected<NodeId, DuplicateNameError>
ForeverStack<N>::insert_at_root (Identifier name, NodeId node)
@@ -45,6 +45,25 @@ NameResolutionContext::insert (Identifier name, NodeId id, Namespace ns)
}
}
+tl::expected<NodeId, DuplicateNameError>
+NameResolutionContext::insert_shadowable (Identifier name, NodeId id,
+ Namespace ns)
+{
+ switch (ns)
+ {
+ case Namespace::Values:
+ return values.insert_shadowable (name, id);
+ case Namespace::Types:
+ return types.insert_shadowable (name, id);
+ case Namespace::Macros:
+ return macros.insert_shadowable (name, id);
+ case Namespace::Labels:
+ default:
+ // return labels.insert (name, id);
+ rust_unreachable ();
+ }
+}
+
void
NameResolutionContext::map_usage (Usage usage, Definition definition)
{
@@ -171,6 +171,9 @@ public:
tl::expected<NodeId, DuplicateNameError> insert (Identifier name, NodeId id,
Namespace ns);
+ tl::expected<NodeId, DuplicateNameError>
+ insert_shadowable (Identifier name, NodeId id, Namespace ns);
+
/**
* Run a lambda in a "scoped" context, meaning that a new `Rib` will be pushed
* before executing the lambda and then popped. This is useful for all kinds
@@ -36,71 +36,72 @@ void
GlobbingVisitor::visit (AST::Module &module)
{
if (module.get_visibility ().is_public ())
- ctx.insert (module.get_name (), module.get_node_id (), Namespace::Types);
+ ctx.insert_shadowable (module.get_name (), module.get_node_id (),
+ Namespace::Types);
}
void
GlobbingVisitor::visit (AST::MacroRulesDefinition ¯o)
{
if (macro.get_visibility ().is_public ())
- ctx.insert (macro.get_rule_name (), macro.get_node_id (),
- Namespace::Macros);
+ ctx.insert_shadowable (macro.get_rule_name (), macro.get_node_id (),
+ Namespace::Macros);
}
void
GlobbingVisitor::visit (AST::Function &function)
{
if (function.get_visibility ().is_public ())
- ctx.insert (function.get_function_name (), function.get_node_id (),
- Namespace::Values);
+ ctx.insert_shadowable (function.get_function_name (),
+ function.get_node_id (), Namespace::Values);
}
void
GlobbingVisitor::visit (AST::StaticItem &static_item)
{
if (static_item.get_visibility ().is_public ())
- ctx.insert (static_item.get_identifier (), static_item.get_node_id (),
- Namespace::Values);
+ ctx.insert_shadowable (static_item.get_identifier (),
+ static_item.get_node_id (), Namespace::Values);
}
void
GlobbingVisitor::visit (AST::StructStruct &struct_item)
{
if (struct_item.get_visibility ().is_public ())
- ctx.insert (struct_item.get_identifier (), struct_item.get_node_id (),
- Namespace::Values);
+ ctx.insert_shadowable (struct_item.get_identifier (),
+ struct_item.get_node_id (), Namespace::Values);
}
void
GlobbingVisitor::visit (AST::TupleStruct &tuple_struct)
{
if (tuple_struct.get_visibility ().is_public ())
- ctx.insert (tuple_struct.get_identifier (), tuple_struct.get_node_id (),
- Namespace::Values);
+ ctx.insert_shadowable (tuple_struct.get_identifier (),
+ tuple_struct.get_node_id (), Namespace::Values);
}
void
GlobbingVisitor::visit (AST::Enum &enum_item)
{
if (enum_item.get_visibility ().is_public ())
- ctx.insert (enum_item.get_identifier (), enum_item.get_node_id (),
- Namespace::Values);
+ ctx.insert_shadowable (enum_item.get_identifier (),
+ enum_item.get_node_id (), Namespace::Values);
}
void
GlobbingVisitor::visit (AST::Union &union_item)
{
if (union_item.get_visibility ().is_public ())
- ctx.insert (union_item.get_identifier (), union_item.get_node_id (),
- Namespace::Values);
+ ctx.insert_shadowable (union_item.get_identifier (),
+ union_item.get_node_id (), Namespace::Values);
}
void
GlobbingVisitor::visit (AST::ConstantItem &const_item)
{
if (const_item.get_visibility ().is_public ())
- ctx.insert (const_item.get_identifier (), const_item.get_node_id (),
- Namespace::Values);
+ ctx.insert_shadowable (const_item.get_identifier (),
+ const_item.get_node_id (), Namespace::Values);
}
void