Skip to content

Commit e9340fe

Browse files
authored
Merge pull request #5857 from augusto2112/variable-metadata-cast
[lldb][NFC] Change casting of VariableMetadata to use llvm's standard
2 parents 0483c58 + 8348b01 commit e9340fe

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ void SwiftASTManipulatorBase::VariableInfo::Print(
7373
else
7474
stream.PutCString("<no type>");
7575

76-
if (MetadataIs<VariableMetadataResult>())
76+
if (llvm::isa<VariableMetadataResult>(m_metadata.get()))
7777
stream.Printf(", is_result");
7878

79-
if (MetadataIs<VariableMetadataError>())
79+
if (llvm::isa<VariableMetadataError>(m_metadata.get()))
8080
stream.Printf(", is_error");
8181

8282
stream.PutChar(']');

lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.h

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "swift/AST/Decl.h"
2121
#include "swift/Basic/SourceLoc.h"
2222
#include "llvm/ADT/SmallVector.h"
23+
#include "llvm/Support/Casting.h"
24+
2325

2426
namespace swift {
2527
class CaseStmt;
@@ -38,25 +40,31 @@ class SwiftASTManipulatorBase {
3840
public:
3941
class VariableMetadata {
4042
public:
41-
VariableMetadata() {}
42-
virtual ~VariableMetadata() {}
43-
virtual unsigned GetType() = 0;
43+
VariableMetadata() = default;
44+
virtual ~VariableMetadata() = default;
45+
virtual unsigned GetType() const = 0;
4446
};
4547

4648
class VariableMetadataResult
4749
: public SwiftASTManipulatorBase::VariableMetadata {
4850
public:
4951
virtual ~VariableMetadataResult();
5052
constexpr static unsigned Type() { return 'Resu'; }
51-
unsigned GetType() override { return Type(); }
53+
unsigned GetType() const override { return Type(); }
54+
static bool classof(const VariableMetadata *VM) {
55+
return VM->GetType() == Type();
56+
}
5257
};
5358

5459
class VariableMetadataError
5560
: public SwiftASTManipulatorBase::VariableMetadata {
5661
public:
5762
virtual ~VariableMetadataError();
5863
constexpr static unsigned Type() { return 'Erro'; }
59-
unsigned GetType() override { return Type(); }
64+
unsigned GetType() const override { return Type(); }
65+
static bool classof(const VariableMetadata *VM) {
66+
return VM->GetType() == Type();
67+
}
6068
};
6169

6270
class VariableMetadataPersistent
@@ -67,7 +75,10 @@ class SwiftASTManipulatorBase {
6775
: m_persistent_variable_sp(persistent_variable_sp) {}
6876

6977
static constexpr unsigned Type() { return 'Pers'; }
70-
unsigned GetType() override { return Type(); }
78+
unsigned GetType() const override { return Type(); }
79+
static bool classof(const VariableMetadata *VM) {
80+
return VM->GetType() == Type();
81+
}
7182
lldb::ExpressionVariableSP m_persistent_variable_sp;
7283
};
7384

@@ -78,7 +89,10 @@ class SwiftASTManipulatorBase {
7889
: m_variable_sp(variable_sp) {}
7990

8091
static constexpr unsigned Type() { return 'Vari'; }
81-
unsigned GetType() override { return Type(); }
92+
unsigned GetType() const override { return Type(); }
93+
static bool classof(const VariableMetadata *VM) {
94+
return VM->GetType() == Type();
95+
}
8296
lldb::VariableSP m_variable_sp;
8397
};
8498

@@ -107,10 +121,6 @@ class SwiftASTManipulatorBase {
107121
: m_type(type), m_name(name), m_var_introducer(introducer),
108122
m_is_capture_list(is_capture_list), m_metadata(metadata) {}
109123

110-
template <class T> bool MetadataIs() const {
111-
return (m_metadata && m_metadata->GetType() == T::Type());
112-
}
113-
114124
void Print(Stream &stream) const;
115125

116126
void SetType(CompilerType new_type) { m_type = new_type; }

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -833,10 +833,10 @@ MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable,
833833
uint64_t offset = 0;
834834
bool needs_init = false;
835835

836-
bool is_result =
837-
variable.MetadataIs<SwiftASTManipulatorBase::VariableMetadataResult>();
838-
bool is_error =
839-
variable.MetadataIs<SwiftASTManipulatorBase::VariableMetadataError>();
836+
bool is_result = llvm::isa<SwiftASTManipulatorBase::VariableMetadataResult>(
837+
variable.m_metadata.get());
838+
bool is_error = llvm::isa<SwiftASTManipulatorBase::VariableMetadataError>(
839+
variable.m_metadata.get());
840840

841841
auto compiler_type = variable.GetType();
842842
// Add the persistent variable as a typeref compiler type.
@@ -910,14 +910,11 @@ MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable,
910910
if (log)
911911
log->Printf("Added %s variable to struct at offset %llu",
912912
is_result ? "result" : "error", (unsigned long long)offset);
913-
} else if (variable.MetadataIs<
914-
SwiftASTManipulatorBase::VariableMetadataVariable>()) {
913+
} else if (auto *variable_metadata = llvm::dyn_cast<
914+
SwiftASTManipulatorBase::VariableMetadataVariable>(
915+
variable.m_metadata.get())) {
915916
Status error;
916917

917-
SwiftASTManipulatorBase::VariableMetadataVariable *variable_metadata =
918-
static_cast<SwiftASTManipulatorBase::VariableMetadataVariable *>(
919-
variable.m_metadata.get());
920-
921918
offset = materializer.AddVariable(variable_metadata->m_variable_sp, error);
922919

923920
if (!error.Success()) {
@@ -931,12 +928,9 @@ MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable,
931928
log->Printf("Added variable %s to struct at offset %llu",
932929
variable_metadata->m_variable_sp->GetName().AsCString(),
933930
(unsigned long long)offset);
934-
} else if (variable.MetadataIs<
935-
SwiftASTManipulatorBase::VariableMetadataPersistent>()) {
936-
SwiftASTManipulatorBase::VariableMetadataPersistent *variable_metadata =
937-
static_cast<SwiftASTManipulatorBase::VariableMetadataPersistent *>(
938-
variable.m_metadata.get());
939-
931+
} else if (auto *variable_metadata = llvm::dyn_cast<
932+
SwiftASTManipulatorBase::VariableMetadataPersistent>(
933+
variable.m_metadata.get())) {
940934
needs_init = llvm::cast<SwiftExpressionVariable>(
941935
variable_metadata->m_persistent_variable_sp.get())
942936
->m_swift_flags &

0 commit comments

Comments
 (0)