Add cpp_type_alias

This commit is contained in:
Jonathan Müller 2017-01-22 12:44:21 +01:00
commit eedc029e66
10 changed files with 96 additions and 14 deletions

View file

@ -12,6 +12,8 @@
namespace cppast
{
enum class cpp_entity_kind;
class cpp_entity_index;
struct cpp_entity_id;
/// The base class for all entities in the C++ AST.
class cpp_entity : detail::intrusive_list_node<cpp_entity>

View file

@ -19,6 +19,8 @@ namespace cppast
using_directive_t,
using_declaration_t,
type_alias_t,
enum_t,
enum_value_t,

View file

@ -25,13 +25,7 @@ namespace cppast
/// \notes `value` may be `nullptr`, in which case the enum has an implicit value.
static std::unique_ptr<cpp_enum_value> build(
const cpp_entity_index& idx, cpp_entity_id id, std::string name,
std::unique_ptr<cpp_expression> value = nullptr)
{
auto result = std::unique_ptr<cpp_enum_value>(
new cpp_enum_value(std::move(name), std::move(value)));
idx.register_entity(std::move(id), type_safe::ref(*result));
return result;
}
std::unique_ptr<cpp_expression> value = nullptr);
/// \returns A [ts::optional_ref]() to the [cppast::cpp_expression]() that is the enum value.
/// \notes It only has an associated expression if the value is explictly given.

View file

@ -90,13 +90,7 @@ namespace cppast
/// \returns A newly created and registered namespace alias.
static std::unique_ptr<cpp_namespace_alias> build(const cpp_entity_index& idx,
cpp_entity_id id, std::string name,
cpp_namespace_ref target)
{
auto ptr = std::unique_ptr<cpp_namespace_alias>(
new cpp_namespace_alias(std::move(name), std::move(target)));
idx.register_entity(std::move(id), type_safe::ref(*ptr));
return ptr;
}
cpp_namespace_ref target);
/// \returns The [cppast::cpp_namespace_ref]() to the aliased namespace.
const cpp_namespace_ref& target() const noexcept

View file

@ -0,0 +1,24 @@
// 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_type_alias.hpp>
#include <cppast/cpp_entity_kind.hpp>
using namespace cppast;
std::unique_ptr<cpp_type_alias> cpp_type_alias::build(const cpp_entity_index& idx, cpp_entity_id id,
std::string name,
std::unique_ptr<cpp_type> type)
{
auto result =
std::unique_ptr<cpp_type_alias>(new cpp_type_alias(std::move(name), std::move(type)));
idx.register_entity(std::move(id), type_safe::cref(*result));
return result;
}
cpp_entity_kind cpp_type_alias::do_get_entity_kind() const noexcept
{
return cpp_entity_kind::type_alias_t;
}

View file

@ -0,0 +1,41 @@
// 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.
#ifndef CPPAST_CPP_TYPE_ALIAS_HPP_INCLUDED
#define CPPAST_CPP_TYPE_ALIAS_HPP_INCLUDED
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_type.hpp>
namespace cppast
{
/// A [cppast::cpp_entity]() modelling a type alias/typedef.
/// \notes There is no distinction between `using` and `typedef` type aliases made in the AST.
class cpp_type_alias final : public cpp_entity
{
public:
/// \returns A newly created and registered type alias.
static std::unique_ptr<cpp_type_alias> build(const cpp_entity_index& idx, cpp_entity_id id,
std::string name,
std::unique_ptr<cpp_type> type);
/// \returns A reference to the aliased [cppast::cpp_type]().
const cpp_type& underlying_type() const noexcept
{
return *type_;
}
private:
cpp_type_alias(std::string name, std::unique_ptr<cpp_type> type)
: cpp_entity(std::move(name)), type_(std::move(type))
{
}
cpp_entity_kind do_get_entity_kind() const noexcept override;
std::unique_ptr<cpp_type> type_;
};
} // namespace cppast
#endif // CPPAST_CPP_TYPE_ALIAS_HPP_INCLUDED

View file

@ -22,6 +22,9 @@ const char* cppast::to_string(cpp_entity_kind kind) noexcept
case cpp_entity_kind::using_declaration_t:
return "using declaration";
case cpp_entity_kind::type_alias_t:
return "type alias";
case cpp_entity_kind::enum_t:
return "enum";
case cpp_entity_kind::enum_value_t:
@ -39,6 +42,7 @@ bool cppast::is_type(cpp_entity_kind kind) noexcept
switch (kind)
{
case cpp_entity_kind::enum_t:
case cpp_entity_kind::type_alias_t:
return true;
case cpp_entity_kind::file_t:

View file

@ -8,6 +8,16 @@
using namespace cppast;
std::unique_ptr<cpp_enum_value> cpp_enum_value::build(const cpp_entity_index& idx, cpp_entity_id id,
std::string name,
std::unique_ptr<cpp_expression> value)
{
auto result =
std::unique_ptr<cpp_enum_value>(new cpp_enum_value(std::move(name), std::move(value)));
idx.register_entity(std::move(id), type_safe::ref(*result));
return result;
}
cpp_entity_kind cpp_enum_value::do_get_entity_kind() const noexcept
{
return cpp_entity_kind::enum_value_t;

View file

@ -18,6 +18,16 @@ bool detail::cpp_namespace_ref_predicate::operator()(const cpp_entity& e)
return e.kind() == cpp_entity_kind::namespace_t;
}
std::unique_ptr<cpp_namespace_alias> cpp_namespace_alias::build(const cpp_entity_index& idx,
cpp_entity_id id, std::string name,
cpp_namespace_ref target)
{
auto ptr = std::unique_ptr<cpp_namespace_alias>(
new cpp_namespace_alias(std::move(name), std::move(target)));
idx.register_entity(std::move(id), type_safe::ref(*ptr));
return ptr;
}
cpp_entity_kind cpp_namespace_alias::do_get_entity_kind() const noexcept
{
return cpp_entity_kind::namespace_alias_t;

View file

@ -45,6 +45,7 @@ bool detail::visit(const cpp_entity& e, detail::visitor_callback_t cb, void* fun
case cpp_entity_kind::namespace_alias_t:
case cpp_entity_kind::using_directive_t:
case cpp_entity_kind::using_declaration_t:
case cpp_entity_kind::type_alias_t:
case cpp_entity_kind::enum_value_t:
return cb(functor, e, visitor_info::leave_entity);