Skip to content

Commit 0ae08ad

Browse files
committed
TypeGraph: Add CaptureKeys node
1 parent 9055c58 commit 0ae08ad

File tree

10 files changed

+120
-1
lines changed

10 files changed

+120
-1
lines changed

oi/type_graph/NameGen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,9 @@ void NameGen::visit(DummyAllocator& d) {
199199
d.regenerateName();
200200
}
201201

202+
void NameGen::visit(CaptureKeys& c) {
203+
RecursiveVisitor::visit(c);
204+
c.regenerateName();
205+
}
206+
202207
} // namespace oi::detail::type_graph

oi/type_graph/NameGen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class NameGen final : public RecursiveVisitor {
4747
void visit(Typedef& td) override;
4848
void visit(Pointer& p) override;
4949
void visit(DummyAllocator& d) override;
50+
void visit(CaptureKeys& d) override;
5051

5152
static const inline std::string AnonPrefix = "__oi_anon";
5253

oi/type_graph/Printer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ void Printer::visit(const DummyAllocator& d) {
156156
print(d.allocType());
157157
}
158158

159+
void Printer::visit(const CaptureKeys& d) {
160+
prefix();
161+
out_ << "CaptureKeys" << std::endl;
162+
print(d.container());
163+
}
164+
159165
void Printer::prefix() {
160166
int indent = baseIndent_ + depth_ * 2;
161167
out_ << std::string(indent, ' ');

oi/type_graph/Printer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Printer : public ConstVisitor {
4141
void visit(const Pointer& p) override;
4242
void visit(const Dummy& d) override;
4343
void visit(const DummyAllocator& d) override;
44+
void visit(const CaptureKeys& d) override;
4445

4546
private:
4647
void prefix();

oi/type_graph/TopoSorter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ void TopoSorter::visit(Pointer& p) {
136136
acceptAfter(p.pointeeType());
137137
}
138138

139+
void TopoSorter::visit(CaptureKeys& c) {
140+
accept(c.container());
141+
sortedTypes_.push_back(c);
142+
}
143+
139144
/*
140145
* A type graph may contain cycles, so we need to slightly tweak the standard
141146
* topological sorting algorithm. Cycles can only be introduced by certain

oi/type_graph/TopoSorter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class TopoSorter : public RecursiveVisitor {
4747
void visit(Typedef& td) override;
4848
void visit(Pointer& p) override;
4949
void visit(Primitive& p) override;
50+
void visit(CaptureKeys& p) override;
5051

5152
private:
5253
std::unordered_set<Type*> visited_;

oi/type_graph/Types.h

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
X(Typedef) \
4949
X(Pointer) \
5050
X(Dummy) \
51-
X(DummyAllocator)
51+
X(DummyAllocator) \
52+
X(CaptureKeys)
5253

5354
struct ContainerInfo;
5455

@@ -730,6 +731,60 @@ class DummyAllocator : public Type {
730731
std::string inputName_;
731732
};
732733

734+
/*
735+
* CaptureKeys
736+
*
737+
* The held Container will have its keys captured.
738+
*/
739+
class CaptureKeys : public Type {
740+
public:
741+
explicit CaptureKeys(Container& c, const ContainerInfo& info)
742+
: container_(c), containerInfo_(info) {
743+
regenerateName();
744+
}
745+
746+
static inline constexpr bool has_node_id = false;
747+
748+
DECLARE_ACCEPT
749+
750+
virtual const std::string& name() const override {
751+
return name_;
752+
}
753+
754+
virtual std::string_view inputName() const override {
755+
return container_.inputName();
756+
}
757+
758+
void regenerateName() {
759+
name_ = "OICaptureKeys<" + container_.name() + ">";
760+
}
761+
762+
virtual size_t size() const override {
763+
return container_.size();
764+
}
765+
766+
virtual uint64_t align() const override {
767+
return container_.align();
768+
}
769+
770+
virtual NodeId id() const override {
771+
return -1;
772+
}
773+
774+
const ContainerInfo& containerInfo() const {
775+
return containerInfo_;
776+
}
777+
778+
Container& container() const {
779+
return container_;
780+
}
781+
782+
private:
783+
Container& container_;
784+
const ContainerInfo& containerInfo_;
785+
std::string name_;
786+
};
787+
733788
Type& stripTypedefs(Type& type);
734789

735790
} // namespace oi::detail::type_graph

oi/type_graph/Visitor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ class RecursiveVisitor : public Visitor {
108108
virtual void visit(DummyAllocator& d) {
109109
accept(d.allocType());
110110
}
111+
virtual void visit(CaptureKeys& c) {
112+
accept(c.container());
113+
}
111114
};
112115

113116
/*

test/test_name_gen.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,31 @@ TEST(NameGenTest, DummyAllocator) {
399399
EXPECT_EQ(myalloc.inputName(), "BigAllocator");
400400
}
401401

402+
TEST(NameGenTest, CaptureKeys) {
403+
auto myparam1 = Class{0, Class::Kind::Struct, "MyParam", 13};
404+
auto myparam2 = Class{1, Class::Kind::Struct, "MyParam", 13};
405+
406+
auto mycontainer = getVector();
407+
mycontainer.templateParams.push_back(myparam1);
408+
mycontainer.templateParams.push_back(myparam2);
409+
410+
auto captureKeys = CaptureKeys{mycontainer, mycontainer.containerInfo_};
411+
412+
NameGen nameGen;
413+
nameGen.generateNames({captureKeys});
414+
415+
EXPECT_EQ(myparam1.name(), "MyParam_0");
416+
EXPECT_EQ(myparam2.name(), "MyParam_1");
417+
EXPECT_EQ(mycontainer.name(), "std::vector<MyParam_0, MyParam_1>");
418+
EXPECT_EQ(captureKeys.name(),
419+
"OICaptureKeys<std::vector<MyParam_0, MyParam_1>>");
420+
421+
EXPECT_EQ(myparam1.inputName(), "MyParam");
422+
EXPECT_EQ(myparam2.inputName(), "MyParam");
423+
EXPECT_EQ(mycontainer.inputName(), "std::vector<MyParam, MyParam>");
424+
EXPECT_EQ(captureKeys.inputName(), "std::vector<MyParam, MyParam>");
425+
}
426+
402427
TEST(NameGenTest, Cycle) {
403428
auto classA = Class{0, Class::Kind::Class, "ClassA", 69};
404429
auto classB = Class{1, Class::Kind::Class, "ClassB", 69};

test/test_topo_sorter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,20 @@ MyStruct
299299
MyClass
300300
)");
301301
}
302+
303+
TEST(TopoSorterTest, CaptureKeys) {
304+
auto myparam1 = Class{1, Class::Kind::Struct, "MyParam1", 13};
305+
auto myparam2 = Class{2, Class::Kind::Struct, "MyParam2", 13};
306+
auto mycontainer = getMap();
307+
mycontainer.templateParams.push_back((myparam1));
308+
mycontainer.templateParams.push_back((myparam2));
309+
310+
auto captureKeys = CaptureKeys{mycontainer, mycontainer.containerInfo_};
311+
312+
test({captureKeys}, R"(
313+
MyParam1
314+
MyParam2
315+
std::map
316+
OICaptureKeys<std::map>
317+
)");
318+
}

0 commit comments

Comments
 (0)