From a8f56797b86f76db464cf3f6b891df25cee3125b Mon Sep 17 00:00:00 2001 From: Geoff Romer Date: Thu, 29 Jul 2021 14:17:35 -0700 Subject: [PATCH] Add Dump methods to printable types. (#681) --- executable_semantics/ast/BUILD | 6 ++++++ executable_semantics/ast/declaration.h | 2 ++ executable_semantics/ast/expression.cpp | 1 + executable_semantics/ast/expression.h | 2 ++ executable_semantics/ast/function_definition.h | 2 ++ executable_semantics/ast/member.h | 2 ++ executable_semantics/ast/statement.h | 3 +++ executable_semantics/interpreter/BUILD | 10 +++++++++- executable_semantics/interpreter/action.h | 2 ++ executable_semantics/interpreter/address.h | 3 +++ executable_semantics/interpreter/field_path.h | 3 +++ executable_semantics/interpreter/frame.cpp | 3 +-- executable_semantics/interpreter/frame.h | 2 ++ executable_semantics/interpreter/heap.h | 3 +++ executable_semantics/interpreter/value.h | 2 ++ 15 files changed, 43 insertions(+), 3 deletions(-) diff --git a/executable_semantics/ast/BUILD b/executable_semantics/ast/BUILD index e443dd91dcbb..58ce70b160fb 100644 --- a/executable_semantics/ast/BUILD +++ b/executable_semantics/ast/BUILD @@ -20,6 +20,7 @@ cc_library( "//common:ostream", "//executable_semantics/interpreter:address", "//executable_semantics/interpreter:containers", + "@llvm-project//llvm:Support", ], ) @@ -31,6 +32,7 @@ cc_library( "//common:indirect_value", "//common:ostream", "//executable_semantics/common:error", + "@llvm-project//llvm:Support", ], ) @@ -41,6 +43,7 @@ cc_library( deps = [ ":expression", ":statement", + "@llvm-project//llvm:Support", ], ) @@ -51,6 +54,7 @@ cc_library( deps = [ ":expression", "//common:ostream", + "@llvm-project//llvm:Support", ], ) @@ -62,6 +66,7 @@ cc_library( ":expression", "//common:check", "//common:ostream", + "@llvm-project//llvm:Support", ], ) @@ -71,5 +76,6 @@ cc_library( deps = [ ":member", "//common:ostream", + "@llvm-project//llvm:Support", ], ) diff --git a/executable_semantics/ast/declaration.h b/executable_semantics/ast/declaration.h index 9499f5960403..f0cd0fffceb0 100644 --- a/executable_semantics/ast/declaration.h +++ b/executable_semantics/ast/declaration.h @@ -14,6 +14,7 @@ #include "executable_semantics/ast/struct_definition.h" #include "executable_semantics/interpreter/address.h" #include "executable_semantics/interpreter/dictionary.h" +#include "llvm/Support/Compiler.h" namespace Carbon { @@ -84,6 +85,7 @@ class Declaration { auto GetVariableDeclaration() const -> const VariableDeclaration&; void Print(llvm::raw_ostream& out) const; + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } inline auto tag() const -> DeclarationKind { return std::visit([](const auto& t) { return t.Kind; }, value); diff --git a/executable_semantics/ast/expression.cpp b/executable_semantics/ast/expression.cpp index b2e0b070dca7..d198d524e520 100644 --- a/executable_semantics/ast/expression.cpp +++ b/executable_semantics/ast/expression.cpp @@ -6,6 +6,7 @@ #include "executable_semantics/common/error.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/Support/raw_ostream.h" namespace Carbon { diff --git a/executable_semantics/ast/expression.h b/executable_semantics/ast/expression.h index 2d9ece6dc976..0790c27d5501 100644 --- a/executable_semantics/ast/expression.h +++ b/executable_semantics/ast/expression.h @@ -11,6 +11,7 @@ #include #include "common/ostream.h" +#include "llvm/Support/Compiler.h" namespace Carbon { @@ -179,6 +180,7 @@ struct Expression { auto GetFunctionTypeLiteral() const -> const FunctionTypeLiteral&; void Print(llvm::raw_ostream& out) const; + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } inline auto tag() const -> ExpressionKind { return std::visit([](const auto& t) { return t.Kind; }, value); diff --git a/executable_semantics/ast/function_definition.h b/executable_semantics/ast/function_definition.h index ca64123558c8..8ea810077db5 100644 --- a/executable_semantics/ast/function_definition.h +++ b/executable_semantics/ast/function_definition.h @@ -8,6 +8,7 @@ #include "common/ostream.h" #include "executable_semantics/ast/expression.h" #include "executable_semantics/ast/statement.h" +#include "llvm/Support/Compiler.h" namespace Carbon { @@ -33,6 +34,7 @@ struct FunctionDefinition { void Print(llvm::raw_ostream& out) const { PrintDepth(-1, out); } void PrintDepth(int depth, llvm::raw_ostream& out) const; + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } int line_num; std::string name; diff --git a/executable_semantics/ast/member.h b/executable_semantics/ast/member.h index 68f7afae88ba..5b0bbbd84ec7 100644 --- a/executable_semantics/ast/member.h +++ b/executable_semantics/ast/member.h @@ -9,6 +9,7 @@ #include "common/ostream.h" #include "executable_semantics/ast/expression.h" +#include "llvm/Support/Compiler.h" namespace Carbon { @@ -27,6 +28,7 @@ struct Member { auto GetFieldMember() const -> const FieldMember&; void Print(llvm::raw_ostream& out) const; + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } inline auto tag() const -> MemberKind { return std::visit([](const auto& t) { return t.Kind; }, value); diff --git a/executable_semantics/ast/statement.h b/executable_semantics/ast/statement.h index 499deb032a3b..6cb54c72170d 100644 --- a/executable_semantics/ast/statement.h +++ b/executable_semantics/ast/statement.h @@ -9,6 +9,7 @@ #include "common/ostream.h" #include "executable_semantics/ast/expression.h" +#include "llvm/Support/Compiler.h" namespace Carbon { @@ -163,7 +164,9 @@ struct Statement { auto GetRun() const -> const Run&; auto GetAwait() const -> const Await&; + void Print(llvm::raw_ostream& out) const { PrintDepth(-1, out); } void PrintDepth(int depth, llvm::raw_ostream& out) const; + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } inline auto tag() const -> StatementKind { return std::visit([](const auto& t) { return t.Kind; }, value); diff --git a/executable_semantics/interpreter/BUILD b/executable_semantics/interpreter/BUILD index 49118772d1bb..259dd15b1b63 100644 --- a/executable_semantics/interpreter/BUILD +++ b/executable_semantics/interpreter/BUILD @@ -17,6 +17,7 @@ cc_library( "//executable_semantics/ast:expression", "//executable_semantics/ast:function_definition", "//executable_semantics/ast:statement", + "@llvm-project//llvm:Support", ], ) @@ -26,6 +27,7 @@ cc_library( deps = [ ":field_path", "//common:ostream", + "@llvm-project//llvm:Support", ], ) @@ -44,7 +46,10 @@ cc_library( cc_library( name = "field_path", hdrs = ["field_path.h"], - deps = ["//common:ostream"], + deps = [ + "//common:ostream", + "@llvm-project//llvm:Support", + ], ) cc_library( @@ -56,6 +61,7 @@ cc_library( ":address", ":containers", "//common:ostream", + "@llvm-project//llvm:Support", ], ) @@ -67,6 +73,7 @@ cc_library( ":address", ":value", "//common:ostream", + "@llvm-project//llvm:Support", ], ) @@ -120,5 +127,6 @@ cc_library( "//common:ostream", "//executable_semantics/ast:function_definition", "//executable_semantics/ast:statement", + "@llvm-project//llvm:Support", ], ) diff --git a/executable_semantics/interpreter/action.h b/executable_semantics/interpreter/action.h index c544ffb726db..52fc93efe3c5 100644 --- a/executable_semantics/interpreter/action.h +++ b/executable_semantics/interpreter/action.h @@ -12,6 +12,7 @@ #include "executable_semantics/ast/statement.h" #include "executable_semantics/interpreter/stack.h" #include "executable_semantics/interpreter/value.h" +#include "llvm/Support/Compiler.h" namespace Carbon { @@ -56,6 +57,7 @@ struct Action { auto GetValAction() const -> const ValAction&; void Print(llvm::raw_ostream& out) const; + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } inline auto tag() const -> ActionKind { return std::visit([](const auto& t) { return t.Kind; }, value); diff --git a/executable_semantics/interpreter/address.h b/executable_semantics/interpreter/address.h index 705bf793a515..c98ecb5cd879 100644 --- a/executable_semantics/interpreter/address.h +++ b/executable_semantics/interpreter/address.h @@ -11,6 +11,7 @@ #include "common/ostream.h" #include "executable_semantics/interpreter/field_path.h" +#include "llvm/Support/Compiler.h" namespace Carbon { @@ -42,6 +43,8 @@ class Address { out << "Address(" << index << ")" << field_path; } + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } + // If *this represents the address of an object with a field named // `field_name`, this method returns the address of that field. auto SubobjectAddress(std::string field_name) const -> Address { diff --git a/executable_semantics/interpreter/field_path.h b/executable_semantics/interpreter/field_path.h index df2064cd584c..4694dc95312a 100644 --- a/executable_semantics/interpreter/field_path.h +++ b/executable_semantics/interpreter/field_path.h @@ -9,6 +9,7 @@ #include #include "common/ostream.h" +#include "llvm/Support/Compiler.h" namespace Carbon { @@ -50,6 +51,8 @@ class FieldPath { } } + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } + private: // The representation of FieldPath describes how to locate a Value within // another Value, so its implementation details are tied to the implementation diff --git a/executable_semantics/interpreter/frame.cpp b/executable_semantics/interpreter/frame.cpp index ca21ab3e6ed9..3f852bb3a911 100644 --- a/executable_semantics/interpreter/frame.cpp +++ b/executable_semantics/interpreter/frame.cpp @@ -4,8 +4,7 @@ #include "executable_semantics/interpreter/frame.h" -#include - +#include "common/ostream.h" #include "executable_semantics/interpreter/action.h" namespace Carbon { diff --git a/executable_semantics/interpreter/frame.h b/executable_semantics/interpreter/frame.h index f7517087dd5b..01b49b884952 100644 --- a/executable_semantics/interpreter/frame.h +++ b/executable_semantics/interpreter/frame.h @@ -13,6 +13,7 @@ #include "executable_semantics/interpreter/address.h" #include "executable_semantics/interpreter/dictionary.h" #include "executable_semantics/interpreter/stack.h" +#include "llvm/Support/Compiler.h" namespace Carbon { @@ -51,6 +52,7 @@ struct Frame { : name(std::move(std::move(n))), scopes(s), todo(c), continuation() {} void Print(llvm::raw_ostream& out) const; + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } }; } // namespace Carbon diff --git a/executable_semantics/interpreter/heap.h b/executable_semantics/interpreter/heap.h index bb820da477d3..27d42c91cb14 100644 --- a/executable_semantics/interpreter/heap.h +++ b/executable_semantics/interpreter/heap.h @@ -10,6 +10,7 @@ #include "common/ostream.h" #include "executable_semantics/interpreter/address.h" #include "executable_semantics/interpreter/value.h" +#include "llvm/Support/Compiler.h" namespace Carbon { @@ -42,6 +43,8 @@ class Heap { // Print all the values on the heap to the stream `out`. void Print(llvm::raw_ostream& out) const; + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } + private: // Signal an error if the address is no longer alive. void CheckAlive(const Address& address, int line_num); diff --git a/executable_semantics/interpreter/value.h b/executable_semantics/interpreter/value.h index 6d99d1a398f6..d545cbdf6c6b 100644 --- a/executable_semantics/interpreter/value.h +++ b/executable_semantics/interpreter/value.h @@ -17,6 +17,7 @@ #include "executable_semantics/interpreter/address.h" #include "executable_semantics/interpreter/field_path.h" #include "executable_semantics/interpreter/stack.h" +#include "llvm/Support/Compiler.h" namespace Carbon { @@ -242,6 +243,7 @@ struct Value { int line_num) const -> const Value*; void Print(llvm::raw_ostream& out) const; + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } private: std::variant