From 5b18f24db06988952e01add94106d9f3b788eeb5 Mon Sep 17 00:00:00 2001 From: Jon Meow <46229924+jonmeow@users.noreply.github.com> Date: Thu, 26 Aug 2021 09:33:29 -0700 Subject: [PATCH] Switch allocation of FunctionDefinition to use Ptr (#785) --- executable_semantics/ast/declaration.h | 5 +++-- executable_semantics/interpreter/typecheck.cpp | 4 ++-- executable_semantics/syntax/parser.ypp | 10 +++++----- executable_semantics/syntax/syntax_helpers.cpp | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/executable_semantics/ast/declaration.h b/executable_semantics/ast/declaration.h index aa5ec83d200c..3acf1d030e21 100644 --- a/executable_semantics/ast/declaration.h +++ b/executable_semantics/ast/declaration.h @@ -13,6 +13,7 @@ #include "executable_semantics/ast/function_definition.h" #include "executable_semantics/ast/member.h" #include "executable_semantics/ast/pattern.h" +#include "executable_semantics/common/ptr.h" #include "llvm/Support/Compiler.h" namespace Carbon { @@ -58,7 +59,7 @@ class Declaration { class FunctionDeclaration : public Declaration { public: - FunctionDeclaration(const FunctionDefinition* definition) + FunctionDeclaration(Ptr definition) : Declaration(Kind::FunctionDeclaration, definition->line_num), definition(definition) {} @@ -69,7 +70,7 @@ class FunctionDeclaration : public Declaration { auto Definition() const -> const FunctionDefinition& { return *definition; } private: - const FunctionDefinition* definition; + Ptr definition; }; class ClassDeclaration : public Declaration { diff --git a/executable_semantics/interpreter/typecheck.cpp b/executable_semantics/interpreter/typecheck.cpp index 6c4f3c4b1b40..9a99a024e882 100644 --- a/executable_semantics/interpreter/typecheck.cpp +++ b/executable_semantics/interpreter/typecheck.cpp @@ -892,7 +892,7 @@ static auto CheckOrEnsureReturn(const Statement* stmt, bool omitted_ret_type, // TODO: Add checking to function definitions to ensure that // all deduced type parameters will be deduced. static auto TypeCheckFunDef(const FunctionDefinition* f, TypeEnv types, - Env values) -> struct FunctionDefinition* { + Env values) -> Ptr { // Bring the deduced parameters into scope for (const auto& deduced : f->deduced_parameters) { // auto t = InterpExp(values, deduced.type); @@ -913,7 +913,7 @@ static auto TypeCheckFunDef(const FunctionDefinition* f, TypeEnv types, f->is_omitted_return_type); auto body = CheckOrEnsureReturn(res.stmt, f->is_omitted_return_type, f->line_num); - return global_arena->RawNew( + return global_arena->New( f->line_num, f->name, f->deduced_parameters, f->param_pattern, global_arena->RawNew( ReifyType(return_type, f->line_num)), diff --git a/executable_semantics/syntax/parser.ypp b/executable_semantics/syntax/parser.ypp index 95560e5a3330..d1690a6ce895 100644 --- a/executable_semantics/syntax/parser.ypp +++ b/executable_semantics/syntax/parser.ypp @@ -95,8 +95,8 @@ void Carbon::Parser::error(const location_type&, const std::string& message) { %token string_literal %type designator %type >> declaration -%type function_declaration -%type function_definition +%type >> function_declaration +%type >> function_definition %type >> declaration_list %type statement %type if_statement @@ -511,7 +511,7 @@ deduced_params: function_definition: FN identifier deduced_params maybe_empty_tuple_pattern return_type block { - $$ = global_arena->RawNew( + $$ = global_arena->New( yylineno, $2, $3, $4, global_arena->RawNew($5.first), $5.second, $6); @@ -520,7 +520,7 @@ function_definition: { // The return type is not considered "omitted" because it's automatic from // the expression. - $$ = global_arena->RawNew( + $$ = global_arena->New( yylineno, $2, $3, $4, global_arena->RawNew(yylineno), true, global_arena->RawNew(yylineno, $6, true)); @@ -529,7 +529,7 @@ function_definition: function_declaration: FN identifier deduced_params maybe_empty_tuple_pattern return_type ";" { - $$ = global_arena->RawNew( + $$ = global_arena->New( yylineno, $2, $3, $4, global_arena->RawNew($5.first), $5.second, nullptr); } diff --git a/executable_semantics/syntax/syntax_helpers.cpp b/executable_semantics/syntax/syntax_helpers.cpp index e5c3079b8775..728e31ec2999 100644 --- a/executable_semantics/syntax/syntax_helpers.cpp +++ b/executable_semantics/syntax/syntax_helpers.cpp @@ -27,7 +27,7 @@ static void AddIntrinsics(std::list>* fs) { IntrinsicExpression::IntrinsicKind::Print), false); auto print = global_arena->New( - global_arena->RawNew( + global_arena->New( -1, "Print", std::vector(), global_arena->RawNew(-1, print_fields), global_arena->RawNew(