Make NamedEntityInterface non-movable (#946)

This commit is contained in:
Geoff Romer
2021-11-10 09:33:00 -08:00
committed by GitHub
parent e8d9b7675b
commit a9bb06ca14
10 changed files with 82 additions and 61 deletions
@@ -30,7 +30,7 @@ static void AddIntrinsics(Nonnull<Arena*> arena,
IntrinsicExpression::Intrinsic::Print),
false)}));
auto print = arena->New<FunctionDeclaration>(
source_loc, "Print", std::vector<GenericBinding>(),
source_loc, "Print", std::vector<Nonnull<GenericBinding*>>(),
arena->New<TuplePattern>(source_loc, print_params),
arena->New<ExpressionPattern>(arena->New<TupleLiteral>(source_loc)),
/*is_omitted_return_type=*/false, print_return);
@@ -115,10 +115,11 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) {
const auto& func_def = cast<FunctionDeclaration>(d);
Env new_env = *env;
// Bring the deduced parameters into scope.
for (const auto& deduced : func_def.deduced_parameters()) {
for (Nonnull<const GenericBinding*> deduced :
func_def.deduced_parameters()) {
AllocationId a =
heap_.AllocateValue(arena_->New<VariableType>(deduced.name()));
new_env.Set(deduced.name(), a);
heap_.AllocateValue(arena_->New<VariableType>(deduced->name()));
new_env.Set(deduced->name(), a);
}
Nonnull<const FunctionValue*> f = arena_->New<FunctionValue>(&func_def);
AllocationId a = heap_.AllocateValue(f);
@@ -152,9 +153,10 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) {
case Declaration::Kind::ChoiceDeclaration: {
const auto& choice = cast<ChoiceDeclaration>(d);
std::vector<NamedValue> alts;
for (const auto& alternative : choice.alternatives()) {
auto t = InterpExp(Env(arena_), &alternative.signature());
alts.push_back({.name = alternative.name(), .value = t});
for (Nonnull<const ChoiceDeclaration::Alternative*> alternative :
choice.alternatives()) {
auto t = InterpExp(Env(arena_), &alternative->signature());
alts.push_back({.name = alternative->name(), .value = t});
}
auto ct = arena_->New<ChoiceType>(choice.name(), std::move(alts));
AllocationId a = heap_.AllocateValue(ct);
@@ -710,9 +712,9 @@ auto Interpreter::StepExp() -> Transition {
} else {
// { { rt :: fn pt -> [] :: C, E, F} :: S, H}
// -> { fn pt -> rt :: {C, E, F} :: S, H}
return Done{arena_->New<FunctionType>(std::vector<GenericBinding>(),
act->results()[0],
act->results()[1])};
return Done{arena_->New<FunctionType>(
std::vector<Nonnull<const GenericBinding*>>(), act->results()[0],
act->results()[1])};
}
}
case Expression::Kind::ContinuationTypeLiteral: {
@@ -133,8 +133,8 @@ void PopulateNamesInDeclaration(Arena* arena, Declaration& declaration,
case Declaration::Kind::FunctionDeclaration: {
auto& func = cast<FunctionDeclaration>(declaration);
static_scope.Add(func.name(), &declaration);
for (const auto& param : func.deduced_parameters()) {
func.static_scope().Add(param.name(), &param);
for (Nonnull<const GenericBinding*> param : func.deduced_parameters()) {
func.static_scope().Add(param->name(), param);
}
PopulateNamesInPattern(func.param_pattern(), func.static_scope());
PopulateNamesInStatement(arena, func.body(), static_scope);
@@ -151,8 +151,9 @@ void PopulateNamesInDeclaration(Arena* arena, Declaration& declaration,
case Declaration::Kind::ChoiceDeclaration: {
auto& choice = cast<ChoiceDeclaration>(declaration);
static_scope.Add(choice.name(), &declaration);
for (auto& alt : choice.alternatives()) {
choice.static_scope().Add(alt.name(), &alt);
for (Nonnull<const ChoiceDeclaration::Alternative*> alt :
choice.alternatives()) {
choice.static_scope().Add(alt->name(), alt);
}
// Populate name into declared_names.
// Init the choice's declared_names, and populate it with the
@@ -390,8 +390,8 @@ auto TypeChecker::Substitute(TypeEnv dict, Nonnull<const Value*> type)
const auto& fn_type = cast<FunctionType>(*type);
auto param = Substitute(dict, &fn_type.parameters());
auto ret = Substitute(dict, &fn_type.return_type());
return arena_->New<FunctionType>(std::vector<GenericBinding>(), param,
ret);
return arena_->New<FunctionType>(
std::vector<Nonnull<const GenericBinding*>>(), param, ret);
}
case Value::Kind::PointerType: {
return arena_->New<PointerType>(
@@ -545,9 +545,10 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
<< "choice " << choice.name() << " does not have a field named "
<< access.field();
}
SetStaticType(&access, arena_->New<FunctionType>(
std::vector<GenericBinding>(),
*parameter_types, &aggregate_type));
SetStaticType(&access,
arena_->New<FunctionType>(
std::vector<Nonnull<const GenericBinding*>>(),
*parameter_types, &aggregate_type));
return TCResult(res.types);
}
default:
@@ -657,13 +658,14 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
auto deduced_args =
ArgumentDeduction(e->source_loc(), TypeEnv(arena_), parameters,
&call.argument().static_type());
for (auto& deduced_param : fun_t.deduced()) {
for (Nonnull<const GenericBinding*> deduced_param :
fun_t.deduced()) {
// TODO: change the following to a CHECK once the real checking
// has been added to the type checking of function signatures.
if (!deduced_args.Get(deduced_param.name())) {
if (!deduced_args.Get(deduced_param->name())) {
FATAL_COMPILATION_ERROR(e->source_loc())
<< "could not deduce type argument for type parameter "
<< deduced_param.name();
<< deduced_param->name();
}
}
parameters = Substitute(deduced_args, parameters);
@@ -1027,11 +1029,11 @@ void TypeChecker::ExpectReturnOnAllPaths(
auto TypeChecker::TypeCheckFunDef(FunctionDeclaration* f, TypeEnv types,
Env values) -> TCResult {
// Bring the deduced parameters into scope
for (const auto& deduced : f->deduced_parameters()) {
for (Nonnull<const GenericBinding*> deduced : f->deduced_parameters()) {
// auto t = interpreter_.InterpExp(values, deduced.type);
types.Set(deduced.name(), arena_->New<VariableType>(deduced.name()));
AllocationId a = interpreter_.AllocateValue(*types.Get(deduced.name()));
values.Set(deduced.name(), a);
types.Set(deduced->name(), arena_->New<VariableType>(deduced->name()));
AllocationId a = interpreter_.AllocateValue(*types.Get(deduced->name()));
values.Set(deduced->name(), a);
}
// Type check the parameter pattern
auto param_res =
@@ -1068,11 +1070,11 @@ auto TypeChecker::TypeOfFunDef(TypeEnv types, Env values,
FunctionDeclaration* fun_def)
-> Nonnull<const Value*> {
// Bring the deduced parameters into scope
for (const auto& deduced : fun_def->deduced_parameters()) {
for (Nonnull<const GenericBinding*> deduced : fun_def->deduced_parameters()) {
// auto t = interpreter_.InterpExp(values, deduced.type);
types.Set(deduced.name(), arena_->New<VariableType>(deduced.name()));
AllocationId a = interpreter_.AllocateValue(*types.Get(deduced.name()));
values.Set(deduced.name(), a);
types.Set(deduced->name(), arena_->New<VariableType>(deduced->name()));
AllocationId a = interpreter_.AllocateValue(*types.Get(deduced->name()));
values.Set(deduced->name(), a);
}
// Type check the parameter pattern
TypeCheckPattern(&fun_def->param_pattern(), types, values, std::nullopt);
@@ -1204,9 +1206,11 @@ void TypeChecker::TopLevel(Nonnull<Declaration*> d, TypeCheckContext* tops) {
case Declaration::Kind::ChoiceDeclaration: {
const auto& choice = cast<ChoiceDeclaration>(*d);
std::vector<NamedValue> alts;
for (const auto& alternative : choice.alternatives()) {
auto t = interpreter_.InterpExp(tops->values, &alternative.signature());
alts.push_back({.name = alternative.name(), .value = t});
for (Nonnull<const ChoiceDeclaration::Alternative*> alternative :
choice.alternatives()) {
auto t =
interpreter_.InterpExp(tops->values, &alternative->signature());
alts.push_back({.name = alternative->name(), .value = t});
}
auto ct = arena_->New<ChoiceType>(choice.name(), std::move(alts));
AllocationId a = interpreter_.AllocateValue(ct);
+2 -2
View File
@@ -213,11 +213,11 @@ void Value::Print(llvm::raw_ostream& out) const {
if (fn_type.deduced().size() > 0) {
out << "[";
unsigned int i = 0;
for (const auto& deduced : fn_type.deduced()) {
for (Nonnull<const GenericBinding*> deduced : fn_type.deduced()) {
if (i != 0) {
out << ", ";
}
out << deduced.name() << ":! " << deduced.type();
out << deduced->name() << ":! " << deduced->type();
++i;
}
out << "]";
+5 -3
View File
@@ -335,7 +335,7 @@ class TypeType : public Value {
// A function type.
class FunctionType : public Value {
public:
FunctionType(std::vector<GenericBinding> deduced,
FunctionType(std::vector<Nonnull<const GenericBinding*>> deduced,
Nonnull<const Value*> parameters,
Nonnull<const Value*> return_type)
: Value(Kind::FunctionType),
@@ -347,12 +347,14 @@ class FunctionType : public Value {
return value->kind() == Kind::FunctionType;
}
auto deduced() const -> llvm::ArrayRef<GenericBinding> { return deduced_; }
auto deduced() const -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
return deduced_;
}
auto parameters() const -> const Value& { return *parameters_; }
auto return_type() const -> const Value& { return *return_type_; }
private:
std::vector<GenericBinding> deduced_;
std::vector<Nonnull<const GenericBinding*>> deduced_;
Nonnull<const Value*> parameters_;
Nonnull<const Value*> return_type_;
};