From 925c60b669d598a50be25f5b623eab97c7616f57 Mon Sep 17 00:00:00 2001 From: Jon Meow <46229924+jonmeow@users.noreply.github.com> Date: Thu, 19 Aug 2021 11:33:32 -0700 Subject: [PATCH] Change Declaration passing to use Ptr (#758) --- executable_semantics/ast/BUILD | 1 + .../ast/abstract_syntax_tree.h | 3 ++- executable_semantics/common/ptr.h | 4 +--- .../interpreter/interpreter.cpp | 6 ++--- .../interpreter/interpreter.h | 2 +- .../interpreter/typecheck.cpp | 22 +++++++++---------- executable_semantics/interpreter/typecheck.h | 7 +++--- executable_semantics/syntax/parser.ypp | 7 +++--- .../syntax/syntax_helpers.cpp | 16 +++++++------- executable_semantics/syntax/syntax_helpers.h | 3 ++- 10 files changed, 37 insertions(+), 34 deletions(-) diff --git a/executable_semantics/ast/BUILD b/executable_semantics/ast/BUILD index 090d8754767a..480a04048087 100644 --- a/executable_semantics/ast/BUILD +++ b/executable_semantics/ast/BUILD @@ -19,6 +19,7 @@ cc_library( ":pattern", ":struct_definition", "//common:ostream", + "//executable_semantics/common:ptr", "@llvm-project//llvm:Support", ], ) diff --git a/executable_semantics/ast/abstract_syntax_tree.h b/executable_semantics/ast/abstract_syntax_tree.h index a3cf42a6ebec..655912ef21c2 100644 --- a/executable_semantics/ast/abstract_syntax_tree.h +++ b/executable_semantics/ast/abstract_syntax_tree.h @@ -8,9 +8,10 @@ #include #include "executable_semantics/ast/declaration.h" +#include "executable_semantics/common/ptr.h" namespace Carbon { -using AST = std::list; +using AST = std::list>; } #endif // EXECUTABLE_SEMANTICS_AST_ABSTRACT_SYNTAX_TREE_H_ diff --git a/executable_semantics/common/ptr.h b/executable_semantics/common/ptr.h index 044458f63d1e..f9290fefb68a 100644 --- a/executable_semantics/common/ptr.h +++ b/executable_semantics/common/ptr.h @@ -20,9 +20,7 @@ class Ptr { template >* = nullptr> - Ptr(Ptr other) { - return Ptr(other.ptr); - } + Ptr(Ptr other) : ptr(other.Get()) {} Ptr(std::nullptr_t) = delete; diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index 21bdf2093f9e..87aeac77b22e 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -183,8 +183,8 @@ void InitEnv(const Declaration& d, Env* env) { } } -static void InitGlobals(const std::list& fs) { - for (const auto* d : fs) { +static void InitGlobals(const std::list>& fs) { + for (const auto d : fs) { InitEnv(*d, &globals); } } @@ -1236,7 +1236,7 @@ void Step() { } // Interpret the whole porogram. -auto InterpProgram(const std::list& fs) -> int { +auto InterpProgram(const std::list>& fs) -> int { state = global_arena->RawNew(); // Runtime state. if (tracing_output) { llvm::outs() << "********** initializing globals **********\n"; diff --git a/executable_semantics/interpreter/interpreter.h b/executable_semantics/interpreter/interpreter.h index 559a15d5df9e..395bfcc73e04 100644 --- a/executable_semantics/interpreter/interpreter.h +++ b/executable_semantics/interpreter/interpreter.h @@ -40,7 +40,7 @@ void PrintEnv(Env values, llvm::raw_ostream& out); auto PatternMatch(const Value* p, const Value* v, int line_num) -> std::optional; -auto InterpProgram(const std::list& fs) -> int; +auto InterpProgram(const std::list>& fs) -> int; auto InterpExp(Env values, const Expression* e) -> const Value*; auto InterpPattern(Env values, const Pattern* p) -> const Value*; diff --git a/executable_semantics/interpreter/typecheck.cpp b/executable_semantics/interpreter/typecheck.cpp index 6eee31c85599..a4b067ab4922 100644 --- a/executable_semantics/interpreter/typecheck.cpp +++ b/executable_semantics/interpreter/typecheck.cpp @@ -989,16 +989,16 @@ static auto GetName(const Declaration& d) -> const std::string& { } } -auto MakeTypeChecked(const Declaration& d, const TypeEnv& types, - const Env& values) -> const Declaration* { - switch (d.Tag()) { +auto MakeTypeChecked(const Ptr d, const TypeEnv& types, + const Env& values) -> Ptr { + switch (d->Tag()) { case Declaration::Kind::FunctionDeclaration: - return global_arena->RawNew(TypeCheckFunDef( - &cast(d).Definition(), types, values)); + return global_arena->New(TypeCheckFunDef( + &cast(*d).Definition(), types, values)); case Declaration::Kind::StructDeclaration: { const StructDefinition& struct_def = - cast(d).Definition(); + cast(*d).Definition(); std::list fields; for (Member* m : struct_def.members) { switch (m->Tag()) { @@ -1008,16 +1008,16 @@ auto MakeTypeChecked(const Declaration& d, const TypeEnv& types, break; } } - return global_arena->RawNew( + return global_arena->New( struct_def.line_num, struct_def.name, std::move(fields)); } case Declaration::Kind::ChoiceDeclaration: // TODO - return &d; + return d; case Declaration::Kind::VariableDeclaration: { - const auto& var = cast(d); + const auto& var = cast(*d); // Signals a type error if the initializing expression does not have // the declared type of the variable, otherwise returns this // declaration with annotated types. @@ -1033,7 +1033,7 @@ auto MakeTypeChecked(const Declaration& d, const TypeEnv& types, const Value* declared_type = InterpExp(values, type); ExpectType(var.LineNumber(), "initializer of variable", declared_type, type_checked_initializer.type); - return &d; + return d; } } } @@ -1095,7 +1095,7 @@ static void TopLevel(const Declaration& d, TypeCheckContext* tops) { } } -auto TopLevel(const std::list& fs) -> TypeCheckContext { +auto TopLevel(const std::list>& fs) -> TypeCheckContext { TypeCheckContext tops; bool found_main = false; diff --git a/executable_semantics/interpreter/typecheck.h b/executable_semantics/interpreter/typecheck.h index b9565c618400..33ddf65ba3fb 100644 --- a/executable_semantics/interpreter/typecheck.h +++ b/executable_semantics/interpreter/typecheck.h @@ -10,6 +10,7 @@ #include "common/ostream.h" #include "executable_semantics/ast/expression.h" #include "executable_semantics/ast/statement.h" +#include "executable_semantics/common/ptr.h" #include "executable_semantics/interpreter/dictionary.h" #include "executable_semantics/interpreter/interpreter.h" @@ -55,9 +56,9 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values, const Value*& ret_type, bool is_omitted_ret_type) -> TCStatement; -auto MakeTypeChecked(const Declaration& decl, const TypeEnv& types, - const Env& values) -> const Declaration*; -auto TopLevel(const std::list& fs) -> TypeCheckContext; +auto MakeTypeChecked(const Ptr d, const TypeEnv& types, + const Env& values) -> Ptr; +auto TopLevel(const std::list>& fs) -> TypeCheckContext; } // namespace Carbon diff --git a/executable_semantics/syntax/parser.ypp b/executable_semantics/syntax/parser.ypp index 1e96da9a5aad..4375c78d0dc2 100644 --- a/executable_semantics/syntax/parser.ypp +++ b/executable_semantics/syntax/parser.ypp @@ -69,6 +69,7 @@ #include "executable_semantics/ast/function_definition.h" #include "executable_semantics/ast/pattern.h" #include "executable_semantics/common/arena.h" +#include "executable_semantics/common/ptr.h" #include "executable_semantics/ast/paren_contents.h" namespace Carbon { @@ -95,7 +96,7 @@ void Carbon::Parser::error(const location_type&, const std::string& message) { %type declaration %type function_declaration %type function_definition -%type > declaration_list +%type >> declaration_list %type statement %type if_statement %type optional_else @@ -584,11 +585,11 @@ declaration: ; declaration_list: // Empty - { $$ = std::list(); } + { $$ = std::list>(); } | declaration declaration_list { $$ = $2; - $$.push_front($1); + $$.push_front(Ptr($1)); } ; %% diff --git a/executable_semantics/syntax/syntax_helpers.cpp b/executable_semantics/syntax/syntax_helpers.cpp index 260f41b4b202..e5c3079b8775 100644 --- a/executable_semantics/syntax/syntax_helpers.cpp +++ b/executable_semantics/syntax/syntax_helpers.cpp @@ -15,7 +15,7 @@ namespace Carbon { // Adds builtins, currently only Print(). Note Print() is experimental, not // standardized, but is made available for printing state in tests. -static void AddIntrinsics(std::list* fs) { +static void AddIntrinsics(std::list>* fs) { std::vector print_fields = {TuplePattern::Field( "0", global_arena->RawNew( -1, "format_str", @@ -26,7 +26,7 @@ static void AddIntrinsics(std::list* fs) { global_arena->RawNew( IntrinsicExpression::IntrinsicKind::Print), false); - auto* print = global_arena->RawNew( + auto print = global_arena->New( global_arena->RawNew( -1, "Print", std::vector(), global_arena->RawNew(-1, print_fields), @@ -36,11 +36,11 @@ static void AddIntrinsics(std::list* fs) { fs->insert(fs->begin(), print); } -void ExecProgram(std::list fs) { +void ExecProgram(std::list> fs) { AddIntrinsics(&fs); if (tracing_output) { llvm::outs() << "********** source program **********\n"; - for (const auto* decl : fs) { + for (const auto decl : fs) { llvm::outs() << *decl; } llvm::outs() << "********** type checking **********\n"; @@ -49,14 +49,14 @@ void ExecProgram(std::list fs) { TypeCheckContext p = TopLevel(fs); TypeEnv top = p.types; Env ct_top = p.values; - std::list new_decls; - for (const auto* decl : fs) { - new_decls.push_back(MakeTypeChecked(*decl, top, ct_top)); + std::list> new_decls; + for (const auto decl : fs) { + new_decls.push_back(MakeTypeChecked(decl, top, ct_top)); } if (tracing_output) { llvm::outs() << "\n"; llvm::outs() << "********** type checking complete **********\n"; - for (const auto* decl : new_decls) { + for (const auto decl : new_decls) { llvm::outs() << *decl; } llvm::outs() << "********** starting execution **********\n"; diff --git a/executable_semantics/syntax/syntax_helpers.h b/executable_semantics/syntax/syntax_helpers.h index b0a18f61fe8a..58ca916c5e01 100644 --- a/executable_semantics/syntax/syntax_helpers.h +++ b/executable_semantics/syntax/syntax_helpers.h @@ -12,11 +12,12 @@ #include #include "executable_semantics/ast/declaration.h" +#include "executable_semantics/common/ptr.h" namespace Carbon { // Runs the top-level declaration list. -void ExecProgram(std::list fs); +void ExecProgram(std::list> fs); } // namespace Carbon