Skip to content

Commit

Permalink
TopoSorter: Only allow certain params to be incomplete
Browse files Browse the repository at this point in the history
For the containers which are allowed to be declared with incomplete
types, it is only the contained types which are allowed to be
incomplete. Other template parameters (e.g. allocators) must always be
defined before use.
  • Loading branch information
ajor committed Oct 25, 2023
1 parent 5daed4c commit e7581ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
16 changes: 7 additions & 9 deletions oi/type_graph/TopoSorter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,31 @@ namespace {
* Other containers are not required to do this, but might still have this
* behaviour.
*/
bool containerAllowsIncompleteParams(const Container& c) {
bool containerAllowsIncompleteParam(const Container& c, size_t i) {
switch (c.containerInfo_.ctype) {
case SEQ_TYPE:
case LIST_TYPE:
case UNIQ_PTR_TYPE:
case SHRD_PTR_TYPE:
// Also std::forward_list, if we ever support that
// Would be good to have this as an option in the TOML files
return true;
return i == 0;
default:
return false;
}
}
} // namespace

void TopoSorter::visit(Container& c) {
if (!containerAllowsIncompleteParams(c)) {
for (const auto& param : c.templateParams) {
for (size_t i = 0; i < c.templateParams.size(); i++) {
const auto& param = c.templateParams[i];
if (containerAllowsIncompleteParam(c, i)) {
acceptAfter(param.type());
} else {
accept(param.type());
}
}
sortedTypes_.push_back(c);
if (containerAllowsIncompleteParams(c)) {
for (const auto& param : c.templateParams) {
acceptAfter(param.type());
}
}
}

void TopoSorter::visit(Enum& e) {
Expand Down
10 changes: 8 additions & 2 deletions test/test_topo_sorter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,30 @@ std::map
}

TEST(TopoSorterTest, ContainersVector) {
// std::vector allows forward declared template parameters
// std::vector allows a forward declared type
auto myparam = Class{1, Class::Kind::Struct, "MyParam", 13};
auto myalloc = Class{1, Class::Kind::Struct, "MyAlloc", 0};
auto mycontainer = getVector();
mycontainer.templateParams.push_back((myparam));
mycontainer.templateParams.push_back((myalloc));

test({mycontainer}, R"(
MyAlloc
std::vector
MyParam
)");
}

TEST(TopoSorterTest, ContainersList) {
// std::list allows forward declared template parameters
// std::list allows a forward declared type
auto myparam = Class{1, Class::Kind::Struct, "MyParam", 13};
auto myalloc = Class{1, Class::Kind::Struct, "MyAlloc", 0};
auto mycontainer = getList();
mycontainer.templateParams.push_back((myparam));
mycontainer.templateParams.push_back((myalloc));

test({mycontainer}, R"(
MyAlloc
std::list
MyParam
)");
Expand Down

0 comments on commit e7581ad

Please sign in to comment.