Skip to content

Commit

Permalink
AST/HIR - Draft defaults for value generics
Browse files Browse the repository at this point in the history
  • Loading branch information
thepowersgang committed Dec 3, 2023
1 parent 88ae396 commit 3b6ea38
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/ast/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ MatchGuard MatchGuard::clone() const
return MatchGuard(mv$(inner));
}
}
throw "";
}
NODE(ExprNode_Match, {
os << "match ("<<*m_val<<") {";
Expand Down
13 changes: 9 additions & 4 deletions src/ast/generics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ class ValueParam
Span m_span;
Ident m_name;
TypeRef m_type;
Expr m_default;
public:
ValueParam(Span sp, ::AST::AttributeList attrs, Ident name, TypeRef type)
ValueParam(Span sp, ::AST::AttributeList attrs, Ident name, TypeRef type, Expr val)
:m_attrs( ::std::move(attrs) )
,m_span( ::std::move(sp) )
,m_name( ::std::move(name) )
,m_type( ::std::move(type) )
,m_default( ::std::move(val) )
{
}
ValueParam(ValueParam&&) = default;
Expand All @@ -94,7 +96,8 @@ class ValueParam
m_attrs( x.m_attrs ),
m_span( x.m_span ),
m_name( x.m_name ),
m_type( x.m_type.clone() )
m_type( x.m_type.clone() ),
m_default( x.m_default ? x.m_default.clone() : Expr() )
{
}

Expand All @@ -105,6 +108,8 @@ class ValueParam
const TypeRef& type() const { return m_type; }
TypeRef& type() { return m_type; }

const Expr& default_value() const { return m_default; }

friend ::std::ostream& operator<<(::std::ostream& os, const ValueParam& p);
};
TAGGED_UNION_EX( GenericParam, (), None,
Expand Down Expand Up @@ -220,8 +225,8 @@ class GenericParams
void add_ty_param(TypeParam param) { add_param( ::std::move(param), SIZE_MAX, SIZE_MAX); }
void add_ty_param(TypeParam param, size_t bounds_start, size_t bounds_end) { add_param( ::std::move(param), bounds_start, bounds_end); }

void add_value_param(Span sp, AttributeList attrs, Ident name, TypeRef ty) {
m_params.push_back(ValueParam(mv$(sp), mv$(attrs), mv$(name), mv$(ty)));
void add_value_param(Span sp, AttributeList attrs, Ident name, TypeRef ty, Expr val) {
m_params.push_back(ValueParam(mv$(sp), mv$(attrs), mv$(name), mv$(ty), mv$(val)));
}

void add_bound(GenericBound bound) {
Expand Down
3 changes: 2 additions & 1 deletion src/hir/from_ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <hir_typeck/helpers.hpp> // monomorph
#include <trans/target.hpp>

::HIR::ExprPtr LowerHIR_Expr(const ::AST::Expr& e);
::HIR::Module LowerHIR_Module(const ::AST::Module& module, ::HIR::ItemPath path, ::std::vector< ::HIR::SimplePath> traits = {});
::HIR::Function LowerHIR_Function(::HIR::ItemPath path, const ::AST::AttributeList& attrs, const ::AST::Function& f, const ::HIR::TypeRef& self_type);
::HIR::ValueItem LowerHIR_Static(::HIR::ItemPath p, const ::AST::AttributeList& attrs, const ::AST::Static& e, const Span& sp, const RcString& name);
Expand Down Expand Up @@ -58,7 +59,7 @@ ::HIR::GenericParams LowerHIR_GenericParams(const ::AST::GenericParams& gp, bool
rv.m_types.push_back({ tp.name(), LowerHIR_Type(tp.get_default()), true });
}
TU_ARMA(Value, tp) {
rv.m_values.push_back(HIR::ValueParamDef { tp.name().name, LowerHIR_Type(tp.type()) });
rv.m_values.push_back(HIR::ValueParamDef { tp.name().name, LowerHIR_Type(tp.type()), tp.default_value() ? LowerHIR_Expr(tp.default_value()) : HIR::ExprPtr() });
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/hir/generic_params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ struct ValueParamDef
{
RcString m_name;
::HIR::TypeRef m_type;
::HIR::ExprPtr m_default;
Ordering ord(const ValueParamDef& x) const {
ORD(m_name, x.m_name);
ORD(m_type, x.m_type);
//ORD(m_default, x.m_default);
return OrdEqual;
}
};
Expand Down
10 changes: 7 additions & 3 deletions src/hir_conv/bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,13 @@ namespace {
if( params.m_values.size() != param_defs.m_values.size() )
{
if( params.m_values.size() == 0 && fill_infer ) {
for(const auto& typ : param_defs.m_values) {
(void)typ;
params.m_values.push_back( ::HIR::ConstGeneric::make_Infer({}) );
for(const auto& val : param_defs.m_values) {
if( val.m_default ) {
TODO(sp, "Value generic defaults");
}
else {
params.m_values.push_back( ::HIR::ConstGeneric::make_Infer({}) );
}
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/parse/root.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,19 @@ AST::GenericParams Parse_GenericParams(TokenStream& lex)
GET_CHECK_TOK(tok, lex, TOK_COLON);
auto ty = Parse_Type(lex);

ret.add_value_param(lex.point_span(), mv$(attrs), mv$(param_name), mv$(ty));
GET_TOK(tok, lex);
AST::Expr val;
if( GET_TOK(tok, lex) == TOK_EQUAL )
{
if( lex.lookahead(0) == TOK_BRACE_OPEN ) {
val = Parse_ExprBlock(lex);
}
else {
val = Parse_Expr(lex);
}
GET_TOK(tok, lex);
}

ret.add_value_param(lex.point_span(), mv$(attrs), mv$(param_name), mv$(ty), mv$(val));
}
else
{
Expand Down

0 comments on commit 3b6ea38

Please sign in to comment.