mirror of
https://github.com/JakeHillion/object-introspection.git
synced 2024-11-09 21:24:14 +00:00
TypeGraph: Change Type::name() to return a reference instead of a copy
Names which were generated on-demand are now stored in member variables, which are set during the ctor and can be regenerated when required (by NameGen).
This commit is contained in:
parent
57d2f3d380
commit
dca99fc4ad
@ -139,6 +139,11 @@ void NameGen::visit(Enum& e) {
|
|||||||
e.setName(name);
|
e.setName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NameGen::visit(Array& a) {
|
||||||
|
accept(a.elementType());
|
||||||
|
a.regenerateName();
|
||||||
|
}
|
||||||
|
|
||||||
void NameGen::visit(Typedef& td) {
|
void NameGen::visit(Typedef& td) {
|
||||||
/*
|
/*
|
||||||
* Treat like class names.
|
* Treat like class names.
|
||||||
@ -158,4 +163,14 @@ void NameGen::visit(Typedef& td) {
|
|||||||
accept(td.underlyingType());
|
accept(td.underlyingType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NameGen::visit(Pointer& p) {
|
||||||
|
accept(p.pointeeType());
|
||||||
|
p.regenerateName();
|
||||||
|
}
|
||||||
|
|
||||||
|
void NameGen::visit(DummyAllocator& d) {
|
||||||
|
accept(d.allocType());
|
||||||
|
d.regenerateName();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace oi::detail::type_graph
|
} // namespace oi::detail::type_graph
|
||||||
|
@ -43,7 +43,10 @@ class NameGen final : public RecursiveVisitor {
|
|||||||
void visit(Class& c) override;
|
void visit(Class& c) override;
|
||||||
void visit(Container& c) override;
|
void visit(Container& c) override;
|
||||||
void visit(Enum& e) override;
|
void visit(Enum& e) override;
|
||||||
|
void visit(Array& a) override;
|
||||||
void visit(Typedef& td) override;
|
void visit(Typedef& td) override;
|
||||||
|
void visit(Pointer& p) override;
|
||||||
|
void visit(DummyAllocator& d) override;
|
||||||
|
|
||||||
static const inline std::string AnonPrefix = "__oi_anon";
|
static const inline std::string AnonPrefix = "__oi_anon";
|
||||||
|
|
||||||
|
@ -29,8 +29,8 @@ namespace oi::detail::type_graph {
|
|||||||
OI_TYPE_LIST
|
OI_TYPE_LIST
|
||||||
#undef X
|
#undef X
|
||||||
|
|
||||||
std::string Primitive::name() const {
|
std::string Primitive::getName(Kind kind) {
|
||||||
switch (kind_) {
|
switch (kind) {
|
||||||
case Kind::Int8:
|
case Kind::Int8:
|
||||||
return "int8_t";
|
return "int8_t";
|
||||||
case Kind::Int16:
|
case Kind::Int16:
|
||||||
|
@ -80,8 +80,7 @@ class Type {
|
|||||||
virtual void accept(Visitor& v) = 0;
|
virtual void accept(Visitor& v) = 0;
|
||||||
virtual void accept(ConstVisitor& v) const = 0;
|
virtual void accept(ConstVisitor& v) const = 0;
|
||||||
|
|
||||||
// TODO don't always return a copy for name()
|
virtual const std::string& name() const = 0;
|
||||||
virtual std::string name() const = 0;
|
|
||||||
virtual size_t size() const = 0;
|
virtual size_t size() const = 0;
|
||||||
virtual uint64_t align() const = 0;
|
virtual uint64_t align() const = 0;
|
||||||
virtual NodeId id() const = 0;
|
virtual NodeId id() const = 0;
|
||||||
@ -210,7 +209,7 @@ class Class : public Type {
|
|||||||
return kind_;
|
return kind_;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual std::string name() const override {
|
virtual const std::string& name() const override {
|
||||||
return name_;
|
return name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -287,7 +286,7 @@ class Container : public Type {
|
|||||||
return containerInfo_.typeName;
|
return containerInfo_.typeName;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual std::string name() const override {
|
virtual const std::string& name() const override {
|
||||||
return name_;
|
return name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -339,7 +338,7 @@ class Enum : public Type {
|
|||||||
|
|
||||||
DECLARE_ACCEPT
|
DECLARE_ACCEPT
|
||||||
|
|
||||||
virtual std::string name() const override {
|
virtual const std::string& name() const override {
|
||||||
return name_;
|
return name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -368,14 +367,20 @@ class Array : public Type {
|
|||||||
public:
|
public:
|
||||||
Array(NodeId id, Type& elementType, size_t len)
|
Array(NodeId id, Type& elementType, size_t len)
|
||||||
: elementType_(elementType), len_(len), id_(id) {
|
: elementType_(elementType), len_(len), id_(id) {
|
||||||
|
regenerateName();
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline constexpr bool has_node_id = true;
|
static inline constexpr bool has_node_id = true;
|
||||||
|
|
||||||
DECLARE_ACCEPT
|
DECLARE_ACCEPT
|
||||||
|
|
||||||
virtual std::string name() const override {
|
virtual const std::string& name() const override {
|
||||||
return "OIArray<" + elementType_.name() + ", " + std::to_string(len_) + ">";
|
return name_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void regenerateName() {
|
||||||
|
name_ = std::string{"OIArray<"} + elementType_.name() + ", " +
|
||||||
|
std::to_string(len_) + ">";
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual size_t size() const override {
|
virtual size_t size() const override {
|
||||||
@ -402,6 +407,8 @@ class Array : public Type {
|
|||||||
Type& elementType_;
|
Type& elementType_;
|
||||||
size_t len_;
|
size_t len_;
|
||||||
NodeId id_ = -1;
|
NodeId id_ = -1;
|
||||||
|
|
||||||
|
std::string name_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Primitive : public Type {
|
class Primitive : public Type {
|
||||||
@ -428,14 +435,16 @@ class Primitive : public Type {
|
|||||||
// stubbed out due to incomplete DWARF
|
// stubbed out due to incomplete DWARF
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit Primitive(Kind kind) : kind_(kind) {
|
explicit Primitive(Kind kind) : kind_(kind), name_(getName(kind)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline constexpr bool has_node_id = false;
|
static inline constexpr bool has_node_id = false;
|
||||||
|
|
||||||
DECLARE_ACCEPT
|
DECLARE_ACCEPT
|
||||||
|
|
||||||
virtual std::string name() const override;
|
virtual const std::string& name() const override {
|
||||||
|
return name_;
|
||||||
|
}
|
||||||
virtual size_t size() const override;
|
virtual size_t size() const override;
|
||||||
virtual uint64_t align() const override {
|
virtual uint64_t align() const override {
|
||||||
return size();
|
return size();
|
||||||
@ -449,6 +458,9 @@ class Primitive : public Type {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Kind kind_;
|
Kind kind_;
|
||||||
|
std::string name_;
|
||||||
|
|
||||||
|
static std::string getName(Kind kind);
|
||||||
};
|
};
|
||||||
|
|
||||||
class Typedef : public Type {
|
class Typedef : public Type {
|
||||||
@ -461,7 +473,7 @@ class Typedef : public Type {
|
|||||||
|
|
||||||
DECLARE_ACCEPT
|
DECLARE_ACCEPT
|
||||||
|
|
||||||
virtual std::string name() const override {
|
virtual const std::string& name() const override {
|
||||||
return name_;
|
return name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -495,14 +507,19 @@ class Pointer : public Type {
|
|||||||
public:
|
public:
|
||||||
explicit Pointer(NodeId id, Type& pointeeType)
|
explicit Pointer(NodeId id, Type& pointeeType)
|
||||||
: pointeeType_(pointeeType), id_(id) {
|
: pointeeType_(pointeeType), id_(id) {
|
||||||
|
regenerateName();
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline constexpr bool has_node_id = true;
|
static inline constexpr bool has_node_id = true;
|
||||||
|
|
||||||
DECLARE_ACCEPT
|
DECLARE_ACCEPT
|
||||||
|
|
||||||
virtual std::string name() const override {
|
virtual const std::string& name() const override {
|
||||||
return pointeeType_.name() + "*";
|
return name_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void regenerateName() {
|
||||||
|
name_ = pointeeType_.name() + "*";
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual size_t size() const override {
|
virtual size_t size() const override {
|
||||||
@ -524,6 +541,8 @@ class Pointer : public Type {
|
|||||||
private:
|
private:
|
||||||
Type& pointeeType_;
|
Type& pointeeType_;
|
||||||
NodeId id_ = -1;
|
NodeId id_ = -1;
|
||||||
|
|
||||||
|
std::string name_;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -533,16 +552,19 @@ class Pointer : public Type {
|
|||||||
*/
|
*/
|
||||||
class Dummy : public Type {
|
class Dummy : public Type {
|
||||||
public:
|
public:
|
||||||
explicit Dummy(size_t size, uint64_t align) : size_(size), align_(align) {
|
explicit Dummy(size_t size, uint64_t align)
|
||||||
|
: size_(size),
|
||||||
|
align_(align),
|
||||||
|
name_(std::string{"DummySizedOperator<"} + std::to_string(size) + ", " +
|
||||||
|
std::to_string(align) + ">") {
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline constexpr bool has_node_id = false;
|
static inline constexpr bool has_node_id = false;
|
||||||
|
|
||||||
DECLARE_ACCEPT
|
DECLARE_ACCEPT
|
||||||
|
|
||||||
virtual std::string name() const override {
|
virtual const std::string& name() const override {
|
||||||
return "DummySizedOperator<" + std::to_string(size_) + ", " +
|
return name_;
|
||||||
std::to_string(align_) + ">";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual size_t size() const override {
|
virtual size_t size() const override {
|
||||||
@ -560,6 +582,8 @@ class Dummy : public Type {
|
|||||||
private:
|
private:
|
||||||
size_t size_;
|
size_t size_;
|
||||||
uint64_t align_;
|
uint64_t align_;
|
||||||
|
|
||||||
|
std::string name_;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -572,15 +596,20 @@ class DummyAllocator : public Type {
|
|||||||
public:
|
public:
|
||||||
explicit DummyAllocator(Type& type, size_t size, uint64_t align)
|
explicit DummyAllocator(Type& type, size_t size, uint64_t align)
|
||||||
: type_(type), size_(size), align_(align) {
|
: type_(type), size_(size), align_(align) {
|
||||||
|
regenerateName();
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline constexpr bool has_node_id = false;
|
static inline constexpr bool has_node_id = false;
|
||||||
|
|
||||||
DECLARE_ACCEPT
|
DECLARE_ACCEPT
|
||||||
|
|
||||||
virtual std::string name() const override {
|
virtual const std::string& name() const override {
|
||||||
return "DummyAllocator<" + type_.name() + ", " + std::to_string(size_) +
|
return name_;
|
||||||
", " + std::to_string(align_) + ">";
|
}
|
||||||
|
|
||||||
|
void regenerateName() {
|
||||||
|
name_ = std::string{"DummyAllocator<"} + type_.name() + ", " +
|
||||||
|
std::to_string(size_) + ", " + std::to_string(align_) + ">";
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual size_t size() const override {
|
virtual size_t size() const override {
|
||||||
@ -603,6 +632,8 @@ class DummyAllocator : public Type {
|
|||||||
Type& type_;
|
Type& type_;
|
||||||
size_t size_;
|
size_t size_;
|
||||||
uint64_t align_;
|
uint64_t align_;
|
||||||
|
|
||||||
|
std::string name_;
|
||||||
};
|
};
|
||||||
|
|
||||||
Type& stripTypedefs(Type& type);
|
Type& stripTypedefs(Type& type);
|
||||||
|
Loading…
Reference in New Issue
Block a user