From a9bb06ca1447afb7e6c31f72644bcc8f07bc22ff Mon Sep 17 00:00:00 2001 From: Geoff Romer Date: Wed, 10 Nov 2021 09:33:00 -0800 Subject: [PATCH] Make NamedEntityInterface non-movable (#946) --- executable_semantics/ast/declaration.cpp | 11 ++--- executable_semantics/ast/declaration.h | 13 +++--- executable_semantics/ast/static_scope.h | 5 +++ .../interpreter/exec_program.cpp | 2 +- .../interpreter/interpreter.cpp | 20 +++++---- .../interpreter/resolve_names.cpp | 9 ++-- .../interpreter/type_checker.cpp | 42 ++++++++++--------- executable_semantics/interpreter/value.cpp | 4 +- executable_semantics/interpreter/value.h | 8 ++-- executable_semantics/syntax/parser.ypp | 29 +++++++------ 10 files changed, 82 insertions(+), 61 deletions(-) diff --git a/executable_semantics/ast/declaration.cpp b/executable_semantics/ast/declaration.cpp index 2960796e1500..61555732519a 100644 --- a/executable_semantics/ast/declaration.cpp +++ b/executable_semantics/ast/declaration.cpp @@ -29,8 +29,9 @@ void Declaration::Print(llvm::raw_ostream& out) const { case Kind::ChoiceDeclaration: { const auto& choice = cast(*this); out << "choice " << choice.name() << " {\n"; - for (const auto& alt : choice.alternatives()) { - out << "alt " << alt.name() << " " << alt.signature() << ";\n"; + for (Nonnull alt : + choice.alternatives()) { + out << "alt " << alt->name() << " " << alt->signature() << ";\n"; } out << "}\n"; break; @@ -49,12 +50,12 @@ void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const { if (!deduced_parameters_.empty()) { out << "["; unsigned int i = 0; - for (const auto& deduced : deduced_parameters_) { + for (Nonnull deduced : deduced_parameters_) { if (i != 0) { out << ", "; } - out << deduced.name() << ":! "; - deduced.type().Print(out); + out << deduced->name() << ":! "; + deduced->type().Print(out); ++i; } out << "]"; diff --git a/executable_semantics/ast/declaration.h b/executable_semantics/ast/declaration.h index ae3fcb48bf42..1606af0a0fe2 100644 --- a/executable_semantics/ast/declaration.h +++ b/executable_semantics/ast/declaration.h @@ -107,7 +107,7 @@ struct GenericBinding : public NamedEntityInterface { class FunctionDeclaration : public Declaration { public: FunctionDeclaration(SourceLocation source_loc, std::string name, - std::vector deduced_params, + std::vector> deduced_params, Nonnull param_pattern, Nonnull return_type, bool is_omitted_return_type, @@ -127,7 +127,8 @@ class FunctionDeclaration : public Declaration { void PrintDepth(int depth, llvm::raw_ostream& out) const; auto name() const -> const std::string& { return name_; } - auto deduced_parameters() const -> llvm::ArrayRef { + auto deduced_parameters() const + -> llvm::ArrayRef> { return deduced_parameters_; } auto param_pattern() const -> const TuplePattern& { return *param_pattern_; } @@ -146,7 +147,7 @@ class FunctionDeclaration : public Declaration { private: std::string name_; - std::vector deduced_parameters_; + std::vector> deduced_parameters_; Nonnull param_pattern_; Nonnull return_type_; bool is_omitted_return_type_; @@ -204,7 +205,7 @@ class ChoiceDeclaration : public Declaration { }; ChoiceDeclaration(SourceLocation source_loc, std::string name, - std::vector alternatives) + std::vector> alternatives) : Declaration(Kind::ChoiceDeclaration, source_loc), name_(std::move(name)), alternatives_(std::move(alternatives)) {} @@ -214,7 +215,7 @@ class ChoiceDeclaration : public Declaration { } auto name() const -> const std::string& { return name_; } - auto alternatives() const -> llvm::ArrayRef { + auto alternatives() const -> llvm::ArrayRef> { return alternatives_; } @@ -224,7 +225,7 @@ class ChoiceDeclaration : public Declaration { private: std::string name_; - std::vector alternatives_; + std::vector> alternatives_; StaticScope static_scope_; }; diff --git a/executable_semantics/ast/static_scope.h b/executable_semantics/ast/static_scope.h index ce9bdc05e7e2..90061c9764b5 100644 --- a/executable_semantics/ast/static_scope.h +++ b/executable_semantics/ast/static_scope.h @@ -33,7 +33,12 @@ class NamedEntityInterface { Member, }; + NamedEntityInterface() = default; virtual ~NamedEntityInterface() = default; + + NamedEntityInterface(NamedEntityInterface&&) = delete; + auto operator=(NamedEntityInterface&&) -> NamedEntityInterface& = delete; + // TODO: This is unused, but is intended for casts after lookup. virtual auto named_entity_kind() const -> NamedEntityKind = 0; virtual auto source_loc() const -> SourceLocation = 0; diff --git a/executable_semantics/interpreter/exec_program.cpp b/executable_semantics/interpreter/exec_program.cpp index a282a8475918..316da2df17f9 100644 --- a/executable_semantics/interpreter/exec_program.cpp +++ b/executable_semantics/interpreter/exec_program.cpp @@ -30,7 +30,7 @@ static void AddIntrinsics(Nonnull arena, IntrinsicExpression::Intrinsic::Print), false)})); auto print = arena->New( - source_loc, "Print", std::vector(), + source_loc, "Print", std::vector>(), arena->New(source_loc, print_params), arena->New(arena->New(source_loc)), /*is_omitted_return_type=*/false, print_return); diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index 249331028ef8..84f465e6cb83 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -115,10 +115,11 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) { const auto& func_def = cast(d); Env new_env = *env; // Bring the deduced parameters into scope. - for (const auto& deduced : func_def.deduced_parameters()) { + for (Nonnull deduced : + func_def.deduced_parameters()) { AllocationId a = - heap_.AllocateValue(arena_->New(deduced.name())); - new_env.Set(deduced.name(), a); + heap_.AllocateValue(arena_->New(deduced->name())); + new_env.Set(deduced->name(), a); } Nonnull f = arena_->New(&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(d); std::vector alts; - for (const auto& alternative : choice.alternatives()) { - auto t = InterpExp(Env(arena_), &alternative.signature()); - alts.push_back({.name = alternative.name(), .value = t}); + for (Nonnull alternative : + choice.alternatives()) { + auto t = InterpExp(Env(arena_), &alternative->signature()); + alts.push_back({.name = alternative->name(), .value = t}); } auto ct = arena_->New(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(std::vector(), - act->results()[0], - act->results()[1])}; + return Done{arena_->New( + std::vector>(), act->results()[0], + act->results()[1])}; } } case Expression::Kind::ContinuationTypeLiteral: { diff --git a/executable_semantics/interpreter/resolve_names.cpp b/executable_semantics/interpreter/resolve_names.cpp index b847447fc749..604849a2ec02 100644 --- a/executable_semantics/interpreter/resolve_names.cpp +++ b/executable_semantics/interpreter/resolve_names.cpp @@ -133,8 +133,8 @@ void PopulateNamesInDeclaration(Arena* arena, Declaration& declaration, case Declaration::Kind::FunctionDeclaration: { auto& func = cast(declaration); static_scope.Add(func.name(), &declaration); - for (const auto& param : func.deduced_parameters()) { - func.static_scope().Add(param.name(), ¶m); + for (Nonnull 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(declaration); static_scope.Add(choice.name(), &declaration); - for (auto& alt : choice.alternatives()) { - choice.static_scope().Add(alt.name(), &alt); + for (Nonnull 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 diff --git a/executable_semantics/interpreter/type_checker.cpp b/executable_semantics/interpreter/type_checker.cpp index 135a99cf38b3..e9f6a21de806 100644 --- a/executable_semantics/interpreter/type_checker.cpp +++ b/executable_semantics/interpreter/type_checker.cpp @@ -390,8 +390,8 @@ auto TypeChecker::Substitute(TypeEnv dict, Nonnull type) const auto& fn_type = cast(*type); auto param = Substitute(dict, &fn_type.parameters()); auto ret = Substitute(dict, &fn_type.return_type()); - return arena_->New(std::vector(), param, - ret); + return arena_->New( + std::vector>(), param, ret); } case Value::Kind::PointerType: { return arena_->New( @@ -545,9 +545,10 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, << "choice " << choice.name() << " does not have a field named " << access.field(); } - SetStaticType(&access, arena_->New( - std::vector(), - *parameter_types, &aggregate_type)); + SetStaticType(&access, + arena_->New( + std::vector>(), + *parameter_types, &aggregate_type)); return TCResult(res.types); } default: @@ -657,13 +658,14 @@ auto TypeChecker::TypeCheckExp(Nonnull 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 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 deduced : f->deduced_parameters()) { // auto t = interpreter_.InterpExp(values, deduced.type); - types.Set(deduced.name(), arena_->New(deduced.name())); - AllocationId a = interpreter_.AllocateValue(*types.Get(deduced.name())); - values.Set(deduced.name(), a); + types.Set(deduced->name(), arena_->New(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 { // Bring the deduced parameters into scope - for (const auto& deduced : fun_def->deduced_parameters()) { + for (Nonnull deduced : fun_def->deduced_parameters()) { // auto t = interpreter_.InterpExp(values, deduced.type); - types.Set(deduced.name(), arena_->New(deduced.name())); - AllocationId a = interpreter_.AllocateValue(*types.Get(deduced.name())); - values.Set(deduced.name(), a); + types.Set(deduced->name(), arena_->New(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 d, TypeCheckContext* tops) { case Declaration::Kind::ChoiceDeclaration: { const auto& choice = cast(*d); std::vector 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 alternative : + choice.alternatives()) { + auto t = + interpreter_.InterpExp(tops->values, &alternative->signature()); + alts.push_back({.name = alternative->name(), .value = t}); } auto ct = arena_->New(choice.name(), std::move(alts)); AllocationId a = interpreter_.AllocateValue(ct); diff --git a/executable_semantics/interpreter/value.cpp b/executable_semantics/interpreter/value.cpp index b09cbe596632..5bbf3922e408 100644 --- a/executable_semantics/interpreter/value.cpp +++ b/executable_semantics/interpreter/value.cpp @@ -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 deduced : fn_type.deduced()) { if (i != 0) { out << ", "; } - out << deduced.name() << ":! " << deduced.type(); + out << deduced->name() << ":! " << deduced->type(); ++i; } out << "]"; diff --git a/executable_semantics/interpreter/value.h b/executable_semantics/interpreter/value.h index 488e49e54e10..d30b3b5d5cfa 100644 --- a/executable_semantics/interpreter/value.h +++ b/executable_semantics/interpreter/value.h @@ -335,7 +335,7 @@ class TypeType : public Value { // A function type. class FunctionType : public Value { public: - FunctionType(std::vector deduced, + FunctionType(std::vector> deduced, Nonnull parameters, Nonnull return_type) : Value(Kind::FunctionType), @@ -347,12 +347,14 @@ class FunctionType : public Value { return value->kind() == Kind::FunctionType; } - auto deduced() const -> llvm::ArrayRef { return deduced_; } + auto deduced() const -> llvm::ArrayRef> { + return deduced_; + } auto parameters() const -> const Value& { return *parameters_; } auto return_type() const -> const Value& { return *return_type_; } private: - std::vector deduced_; + std::vector> deduced_; Nonnull parameters_; Nonnull return_type_; }; diff --git a/executable_semantics/syntax/parser.ypp b/executable_semantics/syntax/parser.ypp index 01fefc7f4a67..e543c813b006 100644 --- a/executable_semantics/syntax/parser.ypp +++ b/executable_semantics/syntax/parser.ypp @@ -108,9 +108,9 @@ %type > block %type >> statement_list %type > expression -%type > generic_binding -%type > deduced_params -%type > deduced_param_list +%type > generic_binding +%type >> deduced_params +%type >> deduced_param_list %type > pattern %type > non_expression_pattern %type , bool>> return_type @@ -131,9 +131,9 @@ %type > maybe_empty_tuple_pattern %type > paren_pattern_base %type > paren_pattern_contents -%type > alternative -%type > alternative_list -%type > alternative_list_contents +%type > alternative +%type >> alternative_list +%type >> alternative_list_contents %type > clause %type > clause_list @@ -632,14 +632,16 @@ return_type: ; generic_binding: identifier COLON_BANG expression - { $$ = GenericBinding(context.source_loc(), std::move($1), $3); } + { + $$ = arena->New(context.source_loc(), std::move($1), $3); + } ; deduced_param_list: // Empty - { $$ = std::vector(); } + { $$ = std::vector>(); } | generic_binding { - $$ = std::vector(); + $$ = std::vector>(); $$.push_back($1); } | generic_binding COMMA deduced_param_list @@ -650,7 +652,7 @@ deduced_param_list: ; deduced_params: // Empty - { $$ = std::vector(); } + { $$ = std::vector>(); } | LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET { $$ = $2; } ; @@ -696,10 +698,13 @@ member_list: ; alternative: identifier tuple - { $$ = ChoiceDeclaration::Alternative(context.source_loc(), $1, $2); } + { + $$ = arena->New(context.source_loc(), $1, + $2); + } | identifier { - $$ = ChoiceDeclaration::Alternative( + $$ = arena->New( context.source_loc(), $1, arena->New(context.source_loc())); }