mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-30 22:02:41 +01:00
Initial batch of AST matchers. (#953)
Also fixes an issue where llvm casts wouldn't work on AstNode.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "executable_semantics/ast/ast_rtti.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "llvm/Support/Casting.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
@@ -71,4 +72,40 @@ class AstNode {
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
// Ensure that LLVM casts from AstNode use dynamic_cast, because static_cast
|
||||
// doesn't work with a virtual base class.
|
||||
namespace llvm {
|
||||
template <typename To>
|
||||
struct cast_convert_val<To, const Carbon::AstNode*, const Carbon::AstNode*> {
|
||||
using ResultType = typename cast_retty<To, const Carbon::AstNode*>::ret_type;
|
||||
static ResultType doit(const Carbon::AstNode* node) {
|
||||
return dynamic_cast<ResultType>(node);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename To>
|
||||
struct cast_convert_val<To, Carbon::AstNode*, Carbon::AstNode*> {
|
||||
using ResultType = typename cast_retty<To, Carbon::AstNode*>::ret_type;
|
||||
static ResultType doit(Carbon::AstNode* node) {
|
||||
return dynamic_cast<ResultType>(node);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename To>
|
||||
struct cast_convert_val<To, const Carbon::AstNode, const Carbon::AstNode> {
|
||||
using ResultType = typename cast_retty<To, const Carbon::AstNode>::ret_type;
|
||||
static ResultType doit(const Carbon::AstNode& node) {
|
||||
return dynamic_cast<ResultType>(node);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename To>
|
||||
struct cast_convert_val<To, Carbon::AstNode, Carbon::AstNode> {
|
||||
using ResultType = typename cast_retty<To, Carbon::AstNode>::ret_type;
|
||||
static ResultType doit(Carbon::AstNode& node) {
|
||||
return dynamic_cast<ResultType>(node);
|
||||
}
|
||||
};
|
||||
} // namespace llvm
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_AST_NODE_H_
|
||||
|
||||
Reference in New Issue
Block a user