Parse cpp_alias_template
This commit is contained in:
parent
cd4a25e959
commit
d6f0997fb6
14 changed files with 550 additions and 82 deletions
136
test/cpp_alias_template.cpp
Normal file
136
test/cpp_alias_template.cpp
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com>
|
||||
// This file is subject to the license terms in the LICENSE file
|
||||
// found in the top-level directory of this distribution.
|
||||
|
||||
#include <cppast/cpp_alias_template.hpp>
|
||||
|
||||
#include "test_parser.hpp"
|
||||
|
||||
using namespace cppast;
|
||||
|
||||
TEST_CASE("cpp_alias_template")
|
||||
{
|
||||
// no need to check advanced types here nor template parameters
|
||||
auto code = R"(
|
||||
template <typename T>
|
||||
using a = int;
|
||||
|
||||
template <int I, typename T = void>
|
||||
using b = T;
|
||||
|
||||
template <typename T>
|
||||
using c = const T*;
|
||||
|
||||
template <typename T>
|
||||
using d = a<void>;
|
||||
|
||||
template <int I>
|
||||
using e = const b<I>;
|
||||
|
||||
template <int I>
|
||||
using f = b<I < a<int>{(0,1)}, int>;
|
||||
|
||||
template <typename T, template <typename> class Templ>
|
||||
using g = Templ<T>;
|
||||
|
||||
template <typename T>
|
||||
using h = g<T, a>;
|
||||
)";
|
||||
|
||||
auto check_parameters =
|
||||
[](const cpp_alias_template& alias,
|
||||
std::initializer_list<std::pair<cpp_entity_kind, const char*>> params) {
|
||||
// no need to check more
|
||||
auto cur = params.begin();
|
||||
for (auto& param : alias.parameters())
|
||||
{
|
||||
REQUIRE(cur != params.end());
|
||||
REQUIRE(param.kind() == cur->first);
|
||||
REQUIRE(param.name() == cur->second);
|
||||
++cur;
|
||||
}
|
||||
REQUIRE(cur == params.end());
|
||||
};
|
||||
|
||||
cpp_entity_index idx;
|
||||
auto file = parse(idx, "cpp_alias_template.cpp", code);
|
||||
auto count = test_visit<cpp_alias_template>(*file, [&](const cpp_alias_template& alias) {
|
||||
if (alias.name() == "a")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
REQUIRE(equal_types(idx, alias.type_alias().underlying_type(),
|
||||
*cpp_builtin_type::build("int")));
|
||||
}
|
||||
else if (alias.name() == "b")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::non_type_template_parameter_t, "I"},
|
||||
{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
REQUIRE(equal_types(idx, alias.type_alias().underlying_type(),
|
||||
*cpp_template_parameter_type::build(
|
||||
cpp_template_type_parameter_ref(cpp_entity_id(""), "T"))));
|
||||
}
|
||||
else if (alias.name() == "c")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
auto param = cpp_template_parameter_type::build(
|
||||
cpp_template_type_parameter_ref(cpp_entity_id(""), "T"));
|
||||
REQUIRE(equal_types(idx, alias.type_alias().underlying_type(),
|
||||
*cpp_pointer_type::build(
|
||||
cpp_cv_qualified_type::build(std::move(param), cpp_cv_const))));
|
||||
}
|
||||
else if (alias.name() == "d")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
|
||||
cpp_template_instantiation_type::builder builder(
|
||||
cpp_template_ref(cpp_entity_id(""), "a"));
|
||||
builder.add_argument(cpp_unexposed_type::build("void"));
|
||||
REQUIRE(equal_types(idx, alias.type_alias().underlying_type(), *builder.finish()));
|
||||
}
|
||||
else if (alias.name() == "e")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::non_type_template_parameter_t, "I"}});
|
||||
|
||||
cpp_template_instantiation_type::builder builder(
|
||||
cpp_template_ref(cpp_entity_id(""), "b"));
|
||||
builder.add_argument(
|
||||
cpp_unexposed_expression::build(cpp_builtin_type::build("int"), "I"));
|
||||
REQUIRE(equal_types(idx, alias.type_alias().underlying_type(),
|
||||
*cpp_cv_qualified_type::build(builder.finish(), cpp_cv_const)));
|
||||
}
|
||||
else if (alias.name() == "f")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::non_type_template_parameter_t, "I"}});
|
||||
|
||||
cpp_template_instantiation_type::builder builder(
|
||||
cpp_template_ref(cpp_entity_id(""), "b"));
|
||||
builder.add_argument(cpp_unexposed_expression::build(cpp_builtin_type::build("int"),
|
||||
"I < a<int>{(0 , 1)}"));
|
||||
builder.add_argument(cpp_unexposed_type::build("int"));
|
||||
REQUIRE(equal_types(idx, alias.type_alias().underlying_type(), *builder.finish()));
|
||||
}
|
||||
else if (alias.name() == "g")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"},
|
||||
{cpp_entity_kind::template_template_parameter_t, "Templ"}});
|
||||
|
||||
cpp_template_instantiation_type::builder builder(
|
||||
cpp_template_ref(cpp_entity_id(""), "Templ"));
|
||||
builder.add_argument(cpp_unexposed_type::build("T"));
|
||||
REQUIRE(equal_types(idx, alias.type_alias().underlying_type(), *builder.finish()));
|
||||
}
|
||||
else if (alias.name() == "h")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
|
||||
cpp_template_instantiation_type::builder builder(
|
||||
cpp_template_ref(cpp_entity_id(""), "g"));
|
||||
builder.add_argument(cpp_unexposed_type::build("T"));
|
||||
builder.add_argument(cpp_template_ref(cpp_entity_id("magic-allow-empty"), "a"));
|
||||
REQUIRE(equal_types(idx, alias.type_alias().underlying_type(), *builder.finish()));
|
||||
}
|
||||
else
|
||||
REQUIRE(false);
|
||||
});
|
||||
REQUIRE(count == 8u);
|
||||
}
|
||||
|
|
@ -6,11 +6,30 @@
|
|||
|
||||
#include <cppast/cpp_array_type.hpp>
|
||||
#include <cppast/cpp_function_type.hpp>
|
||||
#include <cppast/cpp_template.hpp>
|
||||
#include <cppast/cpp_template_parameter.hpp>
|
||||
|
||||
#include "test_parser.hpp"
|
||||
|
||||
using namespace cppast;
|
||||
|
||||
template <typename T, class Predicate>
|
||||
bool equal_ref(const cpp_entity_index& idx, const basic_cpp_entity_ref<T, Predicate>& parsed,
|
||||
const basic_cpp_entity_ref<T, Predicate>& synthesized)
|
||||
{
|
||||
if (parsed.name() != synthesized.name())
|
||||
return false;
|
||||
else if (parsed.is_overloaded() != synthesized.is_overloaded())
|
||||
return false;
|
||||
else if (parsed.is_overloaded())
|
||||
return false;
|
||||
|
||||
auto entities = parsed.get(idx);
|
||||
if (entities.size() != 1u)
|
||||
return synthesized.id()[0u] == cpp_entity_id("magic-allow-empty");
|
||||
return entities[0u]->name().empty() || full_name(*entities[0u]) == parsed.name();
|
||||
}
|
||||
|
||||
bool equal_types(const cpp_entity_index& idx, const cpp_type& parsed, const cpp_type& synthesized)
|
||||
{
|
||||
if (parsed.kind() != synthesized.kind())
|
||||
|
|
@ -24,15 +43,9 @@ bool equal_types(const cpp_entity_index& idx, const cpp_type& parsed, const cpp_
|
|||
|
||||
case cpp_type_kind::user_defined:
|
||||
{
|
||||
auto user_parsed = static_cast<const cpp_user_defined_type&>(parsed).entity();
|
||||
auto user_synthesized = static_cast<const cpp_user_defined_type&>(synthesized).entity();
|
||||
if (user_parsed.name() != user_synthesized.name())
|
||||
return false;
|
||||
else if (user_parsed.is_overloaded())
|
||||
return false;
|
||||
auto entities = user_parsed.get(idx);
|
||||
REQUIRE(entities.size() == 1u);
|
||||
return entities[0u]->name().empty() || full_name(*entities[0u]) == user_parsed.name();
|
||||
auto& user_parsed = static_cast<const cpp_user_defined_type&>(parsed);
|
||||
auto& user_synthesized = static_cast<const cpp_user_defined_type&>(synthesized);
|
||||
return equal_ref(idx, user_parsed.entity(), user_synthesized.entity());
|
||||
}
|
||||
|
||||
case cpp_type_kind::cv_qualified:
|
||||
|
|
@ -69,16 +82,7 @@ bool equal_types(const cpp_entity_index& idx, const cpp_type& parsed, const cpp_
|
|||
|
||||
auto& size_a = array_a.size().value();
|
||||
auto& size_b = array_b.size().value();
|
||||
if (size_a.kind() != size_b.kind())
|
||||
return false;
|
||||
else if (size_a.kind() == cpp_expression_kind::literal)
|
||||
return static_cast<const cpp_literal_expression&>(size_a).value()
|
||||
== static_cast<const cpp_literal_expression&>(size_b).value();
|
||||
else if (size_a.kind() == cpp_expression_kind::unexposed)
|
||||
return static_cast<const cpp_unexposed_expression&>(size_a).expression()
|
||||
== static_cast<const cpp_unexposed_expression&>(size_b).expression();
|
||||
else
|
||||
break;
|
||||
return equal_expressions(size_a, size_b);
|
||||
}
|
||||
|
||||
case cpp_type_kind::function:
|
||||
|
|
@ -92,7 +96,7 @@ bool equal_types(const cpp_entity_index& idx, const cpp_type& parsed, const cpp_
|
|||
return false;
|
||||
|
||||
auto iter_a = func_a.parameter_types().begin();
|
||||
auto iter_b = func_a.parameter_types().begin();
|
||||
auto iter_b = func_b.parameter_types().begin();
|
||||
while (iter_a != func_a.parameter_types().end() && iter_b != func_b.parameter_types().end())
|
||||
{
|
||||
if (!equal_types(idx, *iter_a, *iter_b))
|
||||
|
|
@ -115,7 +119,7 @@ bool equal_types(const cpp_entity_index& idx, const cpp_type& parsed, const cpp_
|
|||
return false;
|
||||
|
||||
auto iter_a = func_a.parameter_types().begin();
|
||||
auto iter_b = func_a.parameter_types().begin();
|
||||
auto iter_b = func_b.parameter_types().begin();
|
||||
while (iter_a != func_a.parameter_types().end() && iter_b != func_b.parameter_types().end())
|
||||
{
|
||||
if (!equal_types(idx, *iter_a, *iter_b))
|
||||
|
|
@ -135,11 +139,50 @@ bool equal_types(const cpp_entity_index& idx, const cpp_type& parsed, const cpp_
|
|||
return equal_types(idx, obj_a.object_type(), obj_b.object_type());
|
||||
}
|
||||
|
||||
// TODO: implement equality when those can be parsed
|
||||
case cpp_type_kind::template_parameter:
|
||||
break;
|
||||
{
|
||||
auto& entity_parsed = static_cast<const cpp_template_parameter_type&>(parsed).entity();
|
||||
auto& entity_synthesized =
|
||||
static_cast<const cpp_template_parameter_type&>(synthesized).entity();
|
||||
return equal_ref(idx, entity_parsed, entity_synthesized);
|
||||
}
|
||||
case cpp_type_kind::template_instantiation:
|
||||
break;
|
||||
{
|
||||
auto& inst_parsed = static_cast<const cpp_template_instantiation_type&>(parsed);
|
||||
auto& inst_synthesized = static_cast<const cpp_template_instantiation_type&>(synthesized);
|
||||
|
||||
if (!equal_ref(idx, inst_parsed.primary_template(), inst_synthesized.primary_template()))
|
||||
return false;
|
||||
|
||||
auto iter_a = inst_parsed.arguments().begin();
|
||||
auto iter_b = inst_synthesized.arguments().begin();
|
||||
while (iter_a != inst_parsed.arguments().end()
|
||||
&& iter_b != inst_synthesized.arguments().end())
|
||||
{
|
||||
if (iter_a->type().has_value() && iter_b->type().has_value())
|
||||
{
|
||||
if (!equal_types(idx, iter_a->type().value(), iter_b->type().value()))
|
||||
return false;
|
||||
}
|
||||
else if (iter_a->expression().has_value() && iter_b->expression().has_value())
|
||||
{
|
||||
if (!equal_expressions(iter_a->expression().value(), iter_b->expression().value()))
|
||||
return false;
|
||||
}
|
||||
else if (iter_a->template_ref().has_value() && iter_b->template_ref().has_value())
|
||||
{
|
||||
if (!equal_ref(idx, iter_a->template_ref().value(), iter_b->template_ref().value()))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
++iter_a;
|
||||
++iter_b;
|
||||
}
|
||||
return iter_a == inst_parsed.arguments().end()
|
||||
&& iter_b == inst_synthesized.arguments().end();
|
||||
}
|
||||
// TODO: implement equality when those can be parsed
|
||||
case cpp_type_kind::dependent:
|
||||
break;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue