diff --git a/common/ostream.h b/common/ostream.h index 8033d131db60..6482cb2d8329 100644 --- a/common/ostream.h +++ b/common/ostream.h @@ -62,28 +62,6 @@ void PrintTo(T* p, std::ostream* out) { } } -// Helper to support printing the ID for a type that has a method -// `void PrintID(llvm::raw_ostream& out) const`. Usage: -// -// out << PrintAsID(obj); -template -class PrintAsID { - public: - explicit PrintAsID(const T& object) : object_(&object) {} - - friend auto operator<<(llvm::raw_ostream& out, const PrintAsID& self) - -> llvm::raw_ostream& { - self.object_->PrintID(out); - return out; - } - - private: - const T* object_; -}; - -template -PrintAsID(const T&) -> PrintAsID; - } // namespace Carbon namespace llvm { diff --git a/explorer/ast/BUILD b/explorer/ast/BUILD index 27f19b867c23..ca1cabeecaa9 100644 --- a/explorer/ast/BUILD +++ b/explorer/ast/BUILD @@ -57,6 +57,7 @@ cc_library( "//explorer/base:decompose", "//explorer/base:error_builders", "//explorer/base:nonnull", + "//explorer/base:print_as_id", "//explorer/base:source_location", "@llvm-project//llvm:Support", ], @@ -153,6 +154,7 @@ cc_library( "//common:ostream", "//explorer/base:error_builders", "//explorer/base:nonnull", + "//explorer/base:print_as_id", "//explorer/base:source_location", "//explorer/base:trace_stream", "@llvm-project//llvm:Support", diff --git a/explorer/ast/declaration.cpp b/explorer/ast/declaration.cpp index cf85a7a4fc34..f1bfce8d14f0 100644 --- a/explorer/ast/declaration.cpp +++ b/explorer/ast/declaration.cpp @@ -5,6 +5,7 @@ #include "explorer/ast/declaration.h" #include "explorer/ast/value.h" +#include "explorer/base/print_as_id.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/Casting.h" diff --git a/explorer/ast/static_scope.cpp b/explorer/ast/static_scope.cpp index e0cbde161c71..0815adf194d3 100644 --- a/explorer/ast/static_scope.cpp +++ b/explorer/ast/static_scope.cpp @@ -8,8 +8,8 @@ #include "common/ostream.h" #include "explorer/base/error_builders.h" +#include "explorer/base/print_as_id.h" #include "llvm/ADT/ScopeExit.h" -#include "llvm/Support/Error.h" namespace Carbon { diff --git a/explorer/base/BUILD b/explorer/base/BUILD index 31e0878296bb..8dcf5cd281d3 100644 --- a/explorer/base/BUILD +++ b/explorer/base/BUILD @@ -72,6 +72,14 @@ cc_library( ], ) +cc_library( + name = "print_as_id", + hdrs = ["print_as_id.h"], + deps = [ + "//common:ostream", + ], +) + cc_library( name = "source_location", hdrs = ["source_location.h"], diff --git a/explorer/base/print_as_id.h b/explorer/base/print_as_id.h new file mode 100644 index 000000000000..5ff97aa60d1a --- /dev/null +++ b/explorer/base/print_as_id.h @@ -0,0 +1,36 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef CARBON_EXPLORER_BASE_PRINT_AS_ID_H_ +#define CARBON_EXPLORER_BASE_PRINT_AS_ID_H_ + +#include "common/ostream.h" + +namespace Carbon { + +// Helper to support printing the ID for a type that has a method +// `void PrintID(llvm::raw_ostream& out) const`. Usage: +// +// out << PrintAsID(obj); +template +class PrintAsID { + public: + explicit PrintAsID(const T& object) : object_(&object) {} + + friend auto operator<<(llvm::raw_ostream& out, const PrintAsID& self) + -> llvm::raw_ostream& { + self.object_->PrintID(out); + return out; + } + + private: + const T* object_; +}; + +template +PrintAsID(const T&) -> PrintAsID; + +} // namespace Carbon + +#endif // CARBON_EXPLORER_BASE_PRINT_AS_ID_H_ diff --git a/explorer/interpreter/BUILD b/explorer/interpreter/BUILD index 7383217d2106..1bc1be134f7a 100644 --- a/explorer/interpreter/BUILD +++ b/explorer/interpreter/BUILD @@ -25,6 +25,7 @@ cc_library( "//explorer/base:arena", "//explorer/base:error_builders", "//explorer/base:nonnull", + "//explorer/base:print_as_id", "//explorer/base:source_location", "@llvm-project//llvm:Support", ], @@ -129,6 +130,7 @@ cc_library( "//explorer/ast:expression_category", "//explorer/base:arena", "//explorer/base:error_builders", + "//explorer/base:print_as_id", "//explorer/base:source_location", "//explorer/base:trace_stream", "@llvm-project//llvm:Support", @@ -144,6 +146,7 @@ cc_library( "//explorer/ast", "//explorer/base:error_builders", "//explorer/base:nonnull", + "//explorer/base:print_as_id", "//explorer/base:trace_stream", "@llvm-project//llvm:Support", ], @@ -159,6 +162,7 @@ cc_library( "//explorer/ast", "//explorer/ast:static_scope", "//explorer/base:arena", + "//explorer/base:print_as_id", "//explorer/base:trace_stream", "@llvm-project//llvm:Support", ], @@ -221,6 +225,7 @@ cc_library( "//explorer/base:arena", "//explorer/base:error_builders", "//explorer/base:nonnull", + "//explorer/base:print_as_id", "//explorer/base:source_location", "//explorer/base:trace_stream", "@llvm-project//llvm:Support", @@ -242,6 +247,7 @@ cc_library( "//explorer/ast:static_scope", "//explorer/base:error_builders", "//explorer/base:nonnull", + "//explorer/base:print_as_id", "//explorer/base:trace_stream", "@llvm-project//llvm:Support", ], diff --git a/explorer/interpreter/action.cpp b/explorer/interpreter/action.cpp index ec995c4fe7c6..0c0b0c2e3c2c 100644 --- a/explorer/interpreter/action.cpp +++ b/explorer/interpreter/action.cpp @@ -16,6 +16,7 @@ #include "explorer/ast/expression.h" #include "explorer/ast/value.h" #include "explorer/base/arena.h" +#include "explorer/base/print_as_id.h" #include "explorer/base/source_location.h" #include "explorer/interpreter/stack.h" #include "llvm/ADT/StringExtras.h" diff --git a/explorer/interpreter/interpreter.cpp b/explorer/interpreter/interpreter.cpp index 80fc0827a237..c603974c9b45 100644 --- a/explorer/interpreter/interpreter.cpp +++ b/explorer/interpreter/interpreter.cpp @@ -23,6 +23,7 @@ #include "explorer/ast/value.h" #include "explorer/base/arena.h" #include "explorer/base/error_builders.h" +#include "explorer/base/print_as_id.h" #include "explorer/base/source_location.h" #include "explorer/base/trace_stream.h" #include "explorer/interpreter/action.h" diff --git a/explorer/interpreter/resolve_control_flow.cpp b/explorer/interpreter/resolve_control_flow.cpp index a0ac58cb4678..acfcd522f4e4 100644 --- a/explorer/interpreter/resolve_control_flow.cpp +++ b/explorer/interpreter/resolve_control_flow.cpp @@ -8,8 +8,8 @@ #include "explorer/ast/return_term.h" #include "explorer/ast/statement.h" #include "explorer/base/error_builders.h" +#include "explorer/base/print_as_id.h" #include "llvm/Support/Casting.h" -#include "llvm/Support/Error.h" using llvm::cast; diff --git a/explorer/interpreter/resolve_names.cpp b/explorer/interpreter/resolve_names.cpp index 02f62a38eae9..9fbb2df592c7 100644 --- a/explorer/interpreter/resolve_names.cpp +++ b/explorer/interpreter/resolve_names.cpp @@ -11,10 +11,10 @@ #include "explorer/ast/pattern.h" #include "explorer/ast/statement.h" #include "explorer/ast/static_scope.h" +#include "explorer/base/print_as_id.h" #include "explorer/interpreter/stack_space.h" #include "llvm/ADT/DenseMap.h" #include "llvm/Support/Casting.h" -#include "llvm/Support/Error.h" using llvm::cast; using llvm::dyn_cast; diff --git a/explorer/interpreter/resolve_unformed.cpp b/explorer/interpreter/resolve_unformed.cpp index b9bd2bdcea84..9b091b5989d0 100644 --- a/explorer/interpreter/resolve_unformed.cpp +++ b/explorer/interpreter/resolve_unformed.cpp @@ -11,6 +11,7 @@ #include "explorer/ast/expression.h" #include "explorer/ast/pattern.h" #include "explorer/base/nonnull.h" +#include "explorer/base/print_as_id.h" #include "explorer/interpreter/stack_space.h" using llvm::cast; diff --git a/explorer/interpreter/type_checker.cpp b/explorer/interpreter/type_checker.cpp index ac77dd40423a..2a2627c3e0fa 100644 --- a/explorer/interpreter/type_checker.cpp +++ b/explorer/interpreter/type_checker.cpp @@ -23,6 +23,7 @@ #include "explorer/ast/value_transform.h" #include "explorer/base/arena.h" #include "explorer/base/error_builders.h" +#include "explorer/base/print_as_id.h" #include "explorer/base/source_location.h" #include "explorer/base/trace_stream.h" #include "explorer/interpreter/impl_scope.h"