Skip to content

Commit ed0d2d8

Browse files
committed
WIP: do not extend the type system.
Revert adding DefaultParamType. Instead, use CallExpr::infer. This only works if the parameter and argument lists materialize as tuples. Single parameters are not encapsulated like that and need special treatment later down the line.
1 parent a730077 commit ed0d2d8

6 files changed

Lines changed: 30 additions & 109 deletions

File tree

include/artic/types.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -301,34 +301,6 @@ struct ImplicitParamType : public Type {
301301
friend class TypeTable;
302302
};
303303

304-
struct DefaultParamType : public Type {
305-
const Type* underlying;
306-
const ast::Expr* expr;
307-
308-
void print(Printer&) const override;
309-
bool equals(const Type*) const override;
310-
size_t hash() const override;
311-
bool contains(const Type*) const override;
312-
313-
const Type* replace(const ReplaceMap&) const override;
314-
315-
const thorin::Type* convert(Emitter&) const override;
316-
std::string stringify(Emitter&) const override;
317-
318-
size_t order(std::unordered_set<const Type*>&) const override;
319-
void variance(TypeVarMap<TypeVariance>&, bool) const override;
320-
void bounds(TypeVarMap<TypeBounds>&, const Type*, bool) const override;
321-
bool is_sized(std::unordered_set<const Type*>&) const override;
322-
private:
323-
DefaultParamType(TypeTable& type_table, const Type* underlying, const ast::Expr* expr)
324-
: Type(type_table)
325-
, underlying(underlying)
326-
, expr(expr)
327-
{}
328-
329-
friend class TypeTable;
330-
};
331-
332304
/// Function type (can represent continuations when the codomain is a `NoRetType`).
333305
struct FnType : public Type {
334306
const Type* dom;
@@ -700,7 +672,6 @@ class TypeTable {
700672
const PtrType* ptr_type(const Type*, bool, size_t);
701673
const RefType* ref_type(const Type*, bool, size_t);
702674
const ImplicitParamType* implicit_param_type(const Type*);
703-
const DefaultParamType* default_param_type(const Type*, const ast::Expr*);
704675
const FnType* fn_type(const Type*, const Type*);
705676
const FnType* cn_type(const Type*);
706677
const BottomType* bottom_type();

src/check.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static bool is_unit(Ptr<ast::Expr>& expr) {
161161

162162
static bool is_tuple_type_with_implicits(const artic::Type* type) {
163163
if (auto tuple_t = type->isa<artic::TupleType>(); tuple_t && !is_unit_type(tuple_t))
164-
return std::any_of(tuple_t->args.begin(), tuple_t->args.end(), [&](auto arg){ return arg->template isa<ImplicitParamType>() || arg->template isa<DefaultParamType>(); });
164+
return std::any_of(tuple_t->args.begin(), tuple_t->args.end(), [&](auto arg){ return arg->template isa<ImplicitParamType>(); });
165165
return false;
166166
}
167167

@@ -174,15 +174,6 @@ const Type* TypeChecker::coerce(Ptr<ast::Expr>& expr, const Type* expected) {
174174
expr.swap(summoned);
175175
return implicit->underlying;
176176
}
177-
} else if (auto default_type = expected->isa<DefaultParamType>()) {
178-
if (is_unit(expr)) {
179-
Ptr<ast::SummonExpr> summoned = make_ptr<ast::SummonExpr>(expr->loc, Ptr<ast::Type>());
180-
summoned->type = default_type->underlying;
181-
summoned->resolved = default_type->expr;
182-
Ptr<ast::Expr> swapexp = std::move(summoned);
183-
expr.swap(swapexp);
184-
return default_type->underlying;
185-
}
186177
} else if (is_tuple_type_with_implicits(expected)) {
187178
auto loc = expr->loc;
188179
auto deconstructed = expr->isa<ast::TupleExpr>();
@@ -207,13 +198,6 @@ const Type* TypeChecker::coerce(Ptr<ast::Expr>& expr, const Type* expected) {
207198
args.push_back(std::move(summoned));
208199
continue;
209200
}
210-
if (auto default_type = tuple_t->args[i]->isa<DefaultParamType>()) {
211-
Ptr<ast::SummonExpr> summoned = make_ptr<ast::SummonExpr>(loc, Ptr<ast::Type>());
212-
summoned->type = default_type->underlying;
213-
summoned->resolved = default_type->expr;
214-
args.push_back(std::move(summoned));
215-
continue;
216-
}
217201

218202
bad_arguments(loc, "non-implicit arguments", i, tuple_t->args.size());
219203
}
@@ -1184,6 +1168,33 @@ const artic::Type* CallExpr::infer(TypeChecker& checker) {
11841168
auto [ref_type, callee_type] = remove_ref(checker.infer(*callee));
11851169
if (auto fn_type = callee_type->isa<artic::FnType>()) {
11861170
checker.coerce(callee, fn_type);
1171+
1172+
artic::ast::TupleExpr* args_tuple = arg->isa<TupleExpr>();
1173+
1174+
const artic::TupleType* from_tuple = fn_type->dom->isa<artic::TupleType>();
1175+
const artic::ast::PathExpr* path_expr = callee_path(callee.get());
1176+
const artic::ast::FnDecl* fn_decl = nullptr;
1177+
const artic::ast::FnExpr* decl = nullptr;
1178+
const artic::ast::TuplePtrn* fn_params = nullptr;
1179+
1180+
if (path_expr) {
1181+
fn_decl = path_expr->path.start_decl->isa<FnDecl>();
1182+
if (fn_decl) {
1183+
decl = fn_decl->fn.get();
1184+
fn_params = decl->param->isa<TuplePtrn>();
1185+
1186+
if (fn_params) {
1187+
for (int i = args_tuple->args.size(); i < fn_params->args.size(); i++) {
1188+
auto default_param = fn_params->args[i]->isa<DefaultParamPtrn>();
1189+
if (default_param) {
1190+
auto default_expr = default_param->default_expr.get();
1191+
args_tuple->args.push_back(std::unique_ptr<Expr>(default_expr));
1192+
}
1193+
}
1194+
}
1195+
}
1196+
}
1197+
11871198
checker.coerce(arg, fn_type->dom);
11881199
return fn_type->codom;
11891200
} else {
@@ -1902,7 +1913,7 @@ const artic::Type * ImplicitParamPtrn::check(artic::TypeChecker& checker, const
19021913
const artic::Type* DefaultParamPtrn::infer(artic::TypeChecker& checker) {
19031914
checker.infer(*default_expr);
19041915
checker.check(*underlying, default_expr->type);
1905-
return checker.type_table.default_param_type(default_expr->type, &*default_expr);
1916+
return default_expr->type;
19061917
}
19071918

19081919
const artic::Type *DefaultParamPtrn::check(artic::TypeChecker& checker, const artic::Type* expected) {

src/emit.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,6 @@ const thorin::Def* Emitter::down_cast(const thorin::Def* def, const Type* from,
688688

689689
if (to->isa<ImplicitParamType>())
690690
return def;
691-
if (to->isa<DefaultParamType>())
692-
return def;
693691

694692
auto to_ptr_type = to->isa<PtrType>();
695693
// Casting a value to a pointer to the type of the value effectively creates an allocation
@@ -1906,10 +1904,6 @@ std::string ImplicitParamType::stringify(Emitter& emitter) const {
19061904
return "implicit_" + underlying->stringify(emitter);
19071905
}
19081906

1909-
std::string DefaultParamType::stringify(Emitter& emitter) const {
1910-
return "default_" + underlying->stringify(emitter);
1911-
}
1912-
19131907
std::string FnType::stringify(Emitter& emitter) const {
19141908
return "fn_" + dom->stringify(emitter) + "_" + codom->stringify(emitter);
19151909
}
@@ -1918,10 +1912,6 @@ const thorin::Type* ImplicitParamType::convert(artic::Emitter& emitter) const {
19181912
return underlying->convert(emitter);
19191913
}
19201914

1921-
const thorin::Type* DefaultParamType::convert(artic::Emitter& emitter) const {
1922-
return underlying->convert(emitter);
1923-
}
1924-
19251915
const thorin::Type* FnType::convert(Emitter& emitter) const {
19261916
if (codom->isa<BottomType>())
19271917
return emitter.continuation_type_with_mem(dom->convert(emitter));

src/print.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -779,11 +779,6 @@ void ImplicitParamType::print(artic::Printer& p) const {
779779
underlying->print(p);
780780
}
781781

782-
void DefaultParamType::print(artic::Printer& p) const {
783-
p << "default ";
784-
underlying->print(p);
785-
}
786-
787782
void FnType::print(Printer& p) const {
788783
p << log::keyword_style("fn") << ' ';
789784
if (!dom->isa<TupleType>()) p << '(';

src/summoner.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ void TypedExpr::resolve_summons(artic::Summoner& summoner) {
6060
}
6161

6262
void SummonExpr::resolve_summons(artic::Summoner& summoner) {
63-
if (!resolved)
64-
resolved = summoner.resolve(type, loc);
63+
resolved = summoner.resolve(type, loc);
6564
}
6665

6766
void FieldExpr::resolve_summons(artic::Summoner& summoner) {

src/types.cpp

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@ bool ImplicitParamType::equals(const artic::Type* other) const {
5858
other->as<ImplicitParamType>()->underlying == underlying;
5959
}
6060

61-
bool DefaultParamType::equals(const artic::Type* other) const {
62-
return
63-
other->isa<DefaultParamType>() &&
64-
other->as<DefaultParamType>()->underlying == underlying &&
65-
other->as<DefaultParamType>()->expr == expr;
66-
}
67-
6861
bool FnType::equals(const Type* other) const {
6962
return
7063
other->isa<FnType>() &&
@@ -127,13 +120,6 @@ size_t ImplicitParamType::hash() const {
127120
.combine(underlying);
128121
}
129122

130-
size_t DefaultParamType::hash() const {
131-
return fnv::Hash()
132-
.combine(typeid(*this).hash_code())
133-
.combine(underlying)
134-
.combine(expr);
135-
}
136-
137123
size_t FnType::hash() const {
138124
return fnv::Hash()
139125
.combine(typeid(*this).hash_code())
@@ -178,10 +164,6 @@ bool ImplicitParamType::contains(const artic::Type* type) const {
178164
return type == this || underlying->contains(type);
179165
}
180166

181-
bool DefaultParamType::contains(const artic::Type* type) const {
182-
return type == this || underlying->contains(type);
183-
}
184-
185167
bool FnType::contains(const Type* type) const {
186168
return type == this || dom->contains(type) || codom->contains(type);
187169
}
@@ -224,10 +206,6 @@ const Type* ImplicitParamType::replace(const artic::ReplaceMap& map) const {
224206
return type_table.implicit_param_type(underlying->replace(map));
225207
}
226208

227-
const Type* DefaultParamType::replace(const artic::ReplaceMap& map) const {
228-
return type_table.default_param_type(underlying->replace(map), expr);
229-
}
230-
231209
const Type* FnType::replace(const std::unordered_map<const TypeVar*, const Type*>& map) const {
232210
return type_table.fn_type(dom->replace(map), codom->replace(map));
233211
}
@@ -255,10 +233,6 @@ size_t ImplicitParamType::order(std::unordered_set<const Type*>& seen) const {
255233
return underlying->order(seen);
256234
}
257235

258-
size_t DefaultParamType::order(std::unordered_set<const Type*>& seen) const {
259-
return underlying->order(seen);
260-
}
261-
262236
size_t FnType::order(std::unordered_set<const Type*>& seen) const {
263237
return 1 + std::max(dom->order(seen), codom->order(seen));
264238
}
@@ -320,10 +294,6 @@ void ImplicitParamType::variance(TypeVarMap<artic::TypeVariance>& vars, bool dir
320294
return underlying->variance(vars, dir);
321295
}
322296

323-
void DefaultParamType::variance(TypeVarMap<artic::TypeVariance>& vars, bool dir) const {
324-
return underlying->variance(vars, dir);
325-
}
326-
327297
void TypeVar::variance(std::unordered_map<const TypeVar*, TypeVariance>& vars, bool dir) const {
328298
if (auto it = vars.find(this); it != vars.end()) {
329299
bool var_dir = it->second == TypeVariance::Covariant ? true : false;
@@ -363,10 +333,6 @@ void ImplicitParamType::bounds(TypeVarMap<artic::TypeBounds>& bounds, const arti
363333
underlying->bounds(bounds, type, dir);
364334
}
365335

366-
void DefaultParamType::bounds(TypeVarMap<artic::TypeBounds>& bounds, const artic::Type* type, bool dir) const {
367-
underlying->bounds(bounds, type, dir);
368-
}
369-
370336
void FnType::bounds(std::unordered_map<const TypeVar*, TypeBounds>& bounds, const Type* type, bool dir) const {
371337
if (auto fn_type = type->isa<FnType>()) {
372338
dom->bounds(bounds, fn_type->dom, !dir);
@@ -404,10 +370,6 @@ bool ImplicitParamType::is_sized(std::unordered_set<const Type*>& seen) const {
404370
return underlying->is_sized(seen);
405371
}
406372

407-
bool DefaultParamType::is_sized(std::unordered_set<const Type*>& seen) const {
408-
return underlying->is_sized(seen);
409-
}
410-
411373
bool FnType::is_sized(std::unordered_set<const Type*>& seen) const {
412374
return dom->is_sized(seen) && codom->is_sized(seen);
413375
}
@@ -529,9 +491,6 @@ bool Type::subtype(const Type* other) const {
529491
if (auto implicit = other->isa<ImplicitParamType>())
530492
return this->subtype(implicit->underlying) || is_unit_type(this);
531493

532-
if (auto default_type = other->isa<DefaultParamType>())
533-
return this->subtype(default_type->underlying) || is_unit_type(this);
534-
535494
auto other_ptr_type = other->isa<PtrType>();
536495

537496
// Take the address of values automatically:
@@ -719,10 +678,6 @@ const ImplicitParamType* TypeTable::implicit_param_type(const Type* underlying)
719678
return insert<ImplicitParamType>(underlying);
720679
}
721680

722-
const DefaultParamType* TypeTable::default_param_type(const Type* underlying, const ast::Expr* expr) {
723-
return insert<DefaultParamType>(underlying, expr);
724-
}
725-
726681
const FnType* TypeTable::fn_type(const Type* dom, const Type* codom) {
727682
return insert<FnType>(dom, codom);
728683
}

0 commit comments

Comments
 (0)