}
}
-void
-TokenCollector::visit (TraitItemConst &item)
-{
- auto id = item.get_identifier ().as_string ();
- indentation ();
- push (Rust::Token::make (CONST, item.get_locus ()));
- push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
- push (Rust::Token::make (COLON, UNDEF_LOCATION));
- visit (item.get_type ());
- push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
- newline ();
-}
-
void
TokenCollector::visit (TraitItemType &item)
{
void visit (ConstantItem &const_item);
void visit (StaticItem &static_item);
void visit (SelfParam ¶m);
- void visit (TraitItemConst &item);
void visit (TraitItemType &item);
void visit (Trait &trait);
void visit (InherentImpl &impl);
class Union;
class ConstantItem;
class StaticItem;
-class TraitItemConst;
class TraitItemType;
class Trait;
class Impl;
reseat (static_item.get_expr_ptr ());
}
-void
-PointerVisitor::visit (AST::TraitItemConst &item)
-{
- visit_outer_attrs (item);
- reseat (item.get_type_ptr ());
- if (item.has_expr ())
- reseat (item.get_expr_ptr ());
-}
-
void
PointerVisitor::visit (AST::TraitItemType &item)
{
void visit (AST::Union &union_item) override;
void visit (AST::ConstantItem &const_item) override;
void visit (AST::StaticItem &static_item) override;
- void visit (AST::TraitItemConst &item) override;
void visit (AST::TraitItemType &item) override;
void visit (AST::Trait &trait) override;
void visit (AST::InherentImpl &impl) override;
visit (static_item.get_expr ());
}
-void
-DefaultASTVisitor::visit (AST::TraitItemConst &item)
-{
- visit_outer_attrs (item);
- visit (item.get_type ());
- if (item.has_expr ())
- visit (item.get_expr ());
-}
-
void
DefaultASTVisitor::visit (AST::TraitItemType &item)
{
virtual void visit (Union &union_item) = 0;
virtual void visit (ConstantItem &const_item) = 0;
virtual void visit (StaticItem &static_item) = 0;
- virtual void visit (TraitItemConst &item) = 0;
virtual void visit (TraitItemType &item) = 0;
virtual void visit (Trait &trait) = 0;
virtual void visit (InherentImpl &impl) = 0;
virtual void visit (AST::Union &union_item) override;
virtual void visit (AST::ConstantItem &const_item) override;
virtual void visit (AST::StaticItem &static_item) override;
- virtual void visit (AST::TraitItemConst &item) override;
virtual void visit (AST::TraitItemType &item) override;
virtual void visit (AST::Trait &trait) override;
virtual void visit (AST::InherentImpl &impl) override;
}
str += "\n Type: " + type->as_string ();
- // DEBUG: null pointer check
- if (const_expr == nullptr)
- {
- rust_debug ("something really terrible has gone wrong - null "
- "pointer expr in const item.");
- return "NULL_POINTER_MARK";
- }
- str += "\n Expression: " + const_expr->as_string ();
+ if (has_expr ())
+ str += "\n Expression: " + const_expr->as_string ();
return str + "\n";
}
return str;
}
-std::string
-TraitItemConst::as_string () const
-{
- // TODO: rewrite to work with non-linearisable exprs
- std::string str = append_attributes (outer_attrs, OUTER);
-
- str += "\nconst " + name.as_string () + " : " + type->as_string ();
-
- if (has_expression ())
- str += " = " + expr->as_string ();
-
- return str;
-}
-
std::string
TraitItemType::as_string () const
{
vis.visit (*this);
}
-void
-TraitItemConst::accept_vis (ASTVisitor &vis)
-{
- vis.visit (*this);
-}
-
void
TraitItemType::accept_vis (ASTVisitor &vis)
{
void accept_vis (ASTVisitor &vis) override;
- // Invalid if type or expression are null, so base stripping on that.
+ // Invalid if type and expression are null, so base stripping on that.
void mark_for_strip () override
{
type = nullptr;
return type == nullptr && const_expr == nullptr;
}
- bool has_expr () { return const_expr != nullptr; }
+ bool has_expr () const { return const_expr != nullptr; }
// TODO: is this better? Or is a "vis_block" better?
Expr &get_expr ()
}
};
-// Constant item within traits
-class TraitItemConst : public TraitItem
-{
- std::vector<Attribute> outer_attrs;
- Identifier name;
- std::unique_ptr<Type> type;
-
- // bool has_expression;
- std::unique_ptr<Expr> expr;
-
-public:
- // Whether the constant item has an associated expression.
- bool has_expression () const { return expr != nullptr; }
-
- TraitItemConst (Identifier name, std::unique_ptr<Type> type,
- std::unique_ptr<Expr> expr,
- std::vector<Attribute> outer_attrs, location_t locus)
- : TraitItem (locus), outer_attrs (std::move (outer_attrs)),
- name (std::move (name)), type (std::move (type)), expr (std::move (expr))
- {}
-
- // Copy constructor with clones
- TraitItemConst (TraitItemConst const &other)
- : TraitItem (other.locus), outer_attrs (other.outer_attrs),
- name (other.name)
- {
- node_id = other.node_id;
-
- // guard to prevent null dereference
- if (other.expr != nullptr)
- expr = other.expr->clone_expr ();
-
- // guard to prevent null dereference (only for error state)
- if (other.type != nullptr)
- type = other.type->clone_type ();
- }
-
- // Overloaded assignment operator to clone
- TraitItemConst &operator= (TraitItemConst const &other)
- {
- TraitItem::operator= (other);
- outer_attrs = other.outer_attrs;
- name = other.name;
- locus = other.locus;
- node_id = other.node_id;
-
- // guard to prevent null dereference
- if (other.expr != nullptr)
- expr = other.expr->clone_expr ();
- else
- expr = nullptr;
-
- // guard to prevent null dereference (only for error state)
- if (other.type != nullptr)
- type = other.type->clone_type ();
- else
- type = nullptr;
-
- return *this;
- }
-
- // move constructors
- TraitItemConst (TraitItemConst &&other) = default;
- TraitItemConst &operator= (TraitItemConst &&other) = default;
-
- std::string as_string () const override;
-
- location_t get_locus () const override { return locus; }
-
- void accept_vis (ASTVisitor &vis) override;
-
- // Invalid if type is null, so base stripping on that.
- void mark_for_strip () override { type = nullptr; }
- bool is_marked_for_strip () const override { return type == nullptr; }
-
- // TODO: this mutable getter seems really dodgy. Think up better way.
- std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
- const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
-
- bool has_expr () const { return expr != nullptr; }
-
- // TODO: is this better? Or is a "vis_block" better?
- Expr &get_expr ()
- {
- rust_assert (has_expr ());
- return *expr;
- }
-
- std::unique_ptr<Expr> &get_expr_ptr ()
- {
- rust_assert (has_expr ());
- return expr;
- }
-
- // TODO: is this better? Or is a "vis_block" better?
- Type &get_type ()
- {
- rust_assert (type != nullptr);
- return *type;
- }
-
- std::unique_ptr<Type> &get_type_ptr ()
- {
- rust_assert (type != nullptr);
- return type;
- }
-
- Identifier get_identifier () const { return name; }
-
-protected:
- // Clone function implementation as (not pure) virtual method
- TraitItemConst *clone_associated_item_impl () const override
- {
- return new TraitItemConst (*this);
- }
-};
-
// Type items within traits
class TraitItemType : public TraitItem
{
"attributes not allowed");
}
-void
-CfgStrip::visit (AST::TraitItemConst &item)
-{
- // initial test based on outer attrs
- expand_cfg_attrs (item.get_outer_attrs ());
- if (fails_cfg_with_expand (item.get_outer_attrs ()))
- {
- item.mark_for_strip ();
- return;
- }
-
- AST::DefaultASTVisitor::visit (item);
-
- // strip any sub-types
- auto &type = item.get_type ();
-
- if (type.is_marked_for_strip ())
- rust_error_at (type.get_locus (), "cannot strip type in this position");
-
- /* strip any internal sub-expressions - expression itself isn't
- * allowed to have external attributes in this position so can't be
- * stripped */
- if (item.has_expression ())
- {
- auto &expr = item.get_expr ();
- if (expr.is_marked_for_strip ())
- rust_error_at (expr.get_locus (),
- "cannot strip expression in this position - outer "
- "attributes not allowed");
- }
-}
-
void
CfgStrip::visit (AST::TraitItemType &item)
{
void visit (AST::Union &union_item) override;
void visit (AST::ConstantItem &const_item) override;
void visit (AST::StaticItem &static_item) override;
- void visit (AST::TraitItemConst &item) override;
void visit (AST::TraitItemType &item) override;
void visit (AST::Trait &trait) override;
void visit (AST::InherentImpl &impl) override;
virtual void visit (EnumItemDiscriminant &item) override final{};
virtual void visit (ConstantItem &const_item) override final{};
virtual void visit (StaticItem &static_item) override final{};
- virtual void visit (TraitItemConst &item) override final{};
virtual void visit (TraitItemType &item) override final{};
virtual void visit (Trait &trait) override final{};
virtual void visit (InherentImpl &impl) override final{};
maybe_expand_expr (static_item.get_expr_ptr ());
}
-void
-ExpandVisitor::visit (AST::TraitItemConst &const_item)
-{
- maybe_expand_type (const_item.get_type_ptr ());
-
- if (const_item.has_expr ())
- maybe_expand_expr (const_item.get_expr_ptr ());
-}
-
void
ExpandVisitor::visit (AST::Trait &trait)
{
void visit (AST::Union &union_item) override;
void visit (AST::ConstantItem &const_item) override;
void visit (AST::StaticItem &static_item) override;
- void visit (AST::TraitItemConst &item) override;
void visit (AST::Trait &trait) override;
void visit (AST::InherentImpl &impl) override;
void visit (AST::TraitImpl &impl) override;
ASTLoweringBase::visit (AST::StaticItem &)
{}
void
-ASTLoweringBase::visit (AST::TraitItemConst &)
-{}
-void
ASTLoweringBase::visit (AST::TraitItemType &)
{}
void
virtual void visit (AST::Union &union_item) override;
virtual void visit (AST::ConstantItem &const_item) override;
virtual void visit (AST::StaticItem &static_item) override;
- virtual void visit (AST::TraitItemConst &item) override;
virtual void visit (AST::TraitItemType &item) override;
virtual void visit (AST::Trait &trait) override;
virtual void visit (AST::InherentImpl &impl) override;
}
void
-ASTLowerTraitItem::visit (AST::TraitItemConst &constant)
+ASTLowerTraitItem::visit (AST::ConstantItem &constant)
{
HIR::Type *type = ASTLoweringType::translate (constant.get_type ());
- HIR::Expr *expr = constant.has_expression ()
+ HIR::Expr *expr = constant.has_expr ()
? ASTLoweringExpr::translate (constant.get_expr ())
: nullptr;
public:
static HIR::TraitItem *translate (AST::AssociatedItem &item);
void visit (AST::Function &func) override;
- void visit (AST::TraitItemConst &constant) override;
+ void visit (AST::ConstantItem &constant) override;
void visit (AST::TraitItemType &type) override;
private:
// Parses a constant trait item.
template <typename ManagedTokenSource>
-std::unique_ptr<AST::TraitItemConst>
+std::unique_ptr<AST::ConstantItem>
Parser<ManagedTokenSource>::parse_trait_const (AST::AttrVec outer_attrs)
{
location_t locus = lexer.peek_token ()->get_locus ();
return nullptr;
}
- return std::unique_ptr<AST::TraitItemConst> (
- new AST::TraitItemConst (std::move (ident), std::move (type),
- std::move (const_body), std::move (outer_attrs),
- locus));
+ return std::unique_ptr<AST::ConstantItem> (new AST::ConstantItem (
+ std::move (ident), AST::Visibility::create_private (), std::move (type),
+ std::move (const_body), std::move (outer_attrs), locus));
}
/* Parses a struct "impl" item (both inherent impl and trait impl can be
AST::AttrVec outer_attrs);
std::unique_ptr<AST::TraitItemType>
parse_trait_type (AST::AttrVec outer_attrs, AST::Visibility);
- std::unique_ptr<AST::TraitItemConst>
+ std::unique_ptr<AST::ConstantItem>
parse_trait_const (AST::AttrVec outer_attrs);
tl::expected<std::unique_ptr<AST::Param>, ParseSelfError> parse_self_param ();
check_proc_macro_non_function (item.get_outer_attrs ());
}
-void
-AttributeChecker::visit (AST::TraitItemConst &)
-{}
-
void
AttributeChecker::visit (AST::TraitItemType &)
{}
void visit (AST::Union &union_item) override;
void visit (AST::ConstantItem &const_item) override;
void visit (AST::StaticItem &static_item) override;
- void visit (AST::TraitItemConst &item) override;
void visit (AST::TraitItemType &item) override;
void visit (AST::Trait &trait) override;
void visit (AST::InherentImpl &impl) override;