From eedc029e6625c41cd044635e47f8b21262ea3bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Sun, 22 Jan 2017 12:44:21 +0100 Subject: [PATCH] Add cpp_type_alias --- include/cppast/cpp_entity.hpp | 2 ++ include/cppast/cpp_entity_kind.hpp | 2 ++ include/cppast/cpp_enum.hpp | 8 +----- include/cppast/cpp_namespace.hpp | 8 +----- include/cppast/cpp_type_alias.cpp | 24 +++++++++++++++++ include/cppast/cpp_type_alias.hpp | 41 ++++++++++++++++++++++++++++++ src/cpp_entity_kind.cpp | 4 +++ src/cpp_enum.cpp | 10 ++++++++ src/cpp_namespace.cpp | 10 ++++++++ src/visitor.cpp | 1 + 10 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 include/cppast/cpp_type_alias.cpp create mode 100644 include/cppast/cpp_type_alias.hpp diff --git a/include/cppast/cpp_entity.hpp b/include/cppast/cpp_entity.hpp index 4a7d541..d7a5601 100644 --- a/include/cppast/cpp_entity.hpp +++ b/include/cppast/cpp_entity.hpp @@ -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 diff --git a/include/cppast/cpp_entity_kind.hpp b/include/cppast/cpp_entity_kind.hpp index 9033a61..7ac2974 100644 --- a/include/cppast/cpp_entity_kind.hpp +++ b/include/cppast/cpp_entity_kind.hpp @@ -19,6 +19,8 @@ namespace cppast using_directive_t, using_declaration_t, + type_alias_t, + enum_t, enum_value_t, diff --git a/include/cppast/cpp_enum.hpp b/include/cppast/cpp_enum.hpp index 436b768..d4d2518 100644 --- a/include/cppast/cpp_enum.hpp +++ b/include/cppast/cpp_enum.hpp @@ -25,13 +25,7 @@ namespace cppast /// \notes `value` may be `nullptr`, in which case the enum has an implicit value. static std::unique_ptr build( const cpp_entity_index& idx, cpp_entity_id id, std::string name, - std::unique_ptr value = nullptr) - { - auto result = std::unique_ptr( - 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 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. diff --git a/include/cppast/cpp_namespace.hpp b/include/cppast/cpp_namespace.hpp index a60e5f0..13810eb 100644 --- a/include/cppast/cpp_namespace.hpp +++ b/include/cppast/cpp_namespace.hpp @@ -90,13 +90,7 @@ namespace cppast /// \returns A newly created and registered namespace alias. static std::unique_ptr build(const cpp_entity_index& idx, cpp_entity_id id, std::string name, - cpp_namespace_ref target) - { - auto ptr = std::unique_ptr( - 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 diff --git a/include/cppast/cpp_type_alias.cpp b/include/cppast/cpp_type_alias.cpp new file mode 100644 index 0000000..e6dd628 --- /dev/null +++ b/include/cppast/cpp_type_alias.cpp @@ -0,0 +1,24 @@ +// Copyright (C) 2017 Jonathan Müller +// This file is subject to the license terms in the LICENSE file +// found in the top-level directory of this distribution. + +#include + +#include + +using namespace cppast; + +std::unique_ptr cpp_type_alias::build(const cpp_entity_index& idx, cpp_entity_id id, + std::string name, + std::unique_ptr type) +{ + auto result = + std::unique_ptr(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; +} diff --git a/include/cppast/cpp_type_alias.hpp b/include/cppast/cpp_type_alias.hpp new file mode 100644 index 0000000..b40ec4a --- /dev/null +++ b/include/cppast/cpp_type_alias.hpp @@ -0,0 +1,41 @@ +// Copyright (C) 2017 Jonathan Müller +// 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 +#include + +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 build(const cpp_entity_index& idx, cpp_entity_id id, + std::string name, + std::unique_ptr 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 type) + : cpp_entity(std::move(name)), type_(std::move(type)) + { + } + + cpp_entity_kind do_get_entity_kind() const noexcept override; + + std::unique_ptr type_; + }; +} // namespace cppast + +#endif // CPPAST_CPP_TYPE_ALIAS_HPP_INCLUDED diff --git a/src/cpp_entity_kind.cpp b/src/cpp_entity_kind.cpp index feed0e1..b580b76 100644 --- a/src/cpp_entity_kind.cpp +++ b/src/cpp_entity_kind.cpp @@ -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: diff --git a/src/cpp_enum.cpp b/src/cpp_enum.cpp index efe5640..578a725 100644 --- a/src/cpp_enum.cpp +++ b/src/cpp_enum.cpp @@ -8,6 +8,16 @@ using namespace cppast; +std::unique_ptr cpp_enum_value::build(const cpp_entity_index& idx, cpp_entity_id id, + std::string name, + std::unique_ptr value) +{ + auto result = + std::unique_ptr(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; diff --git a/src/cpp_namespace.cpp b/src/cpp_namespace.cpp index c53aeda..0d5904e 100644 --- a/src/cpp_namespace.cpp +++ b/src/cpp_namespace.cpp @@ -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::build(const cpp_entity_index& idx, + cpp_entity_id id, std::string name, + cpp_namespace_ref target) +{ + auto ptr = std::unique_ptr( + 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; diff --git a/src/visitor.cpp b/src/visitor.cpp index 0bef5e4..2a17259 100644 --- a/src/visitor.cpp +++ b/src/visitor.cpp @@ -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);