Add cpp_class
This commit is contained in:
parent
ddadcfe88c
commit
f32af61614
5 changed files with 204 additions and 1 deletions
131
include/cppast/cpp_class.hpp
Normal file
131
include/cppast/cpp_class.hpp
Normal file
|
|
@ -0,0 +1,131 @@
|
||||||
|
// 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_CLASS_HPP_INCLUDED
|
||||||
|
#define CPPAST_CPP_CLASS_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include <cppast/cpp_entity.hpp>
|
||||||
|
#include <cppast/cpp_entity_container.hpp>
|
||||||
|
|
||||||
|
namespace cppast
|
||||||
|
{
|
||||||
|
/// The keyword used on the declaration of a [cppast::cpp_class]().
|
||||||
|
enum class cpp_class_kind
|
||||||
|
{
|
||||||
|
class_t,
|
||||||
|
struct_t,
|
||||||
|
union_t,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \returns The keyword as a string.
|
||||||
|
const char* to_string(cpp_class_kind kind) noexcept;
|
||||||
|
|
||||||
|
/// The C++ access specifiers.
|
||||||
|
enum cpp_access_specifier_kind
|
||||||
|
{
|
||||||
|
cpp_public,
|
||||||
|
cpp_protected,
|
||||||
|
cpp_private
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \returns The access specifier keyword as a string.
|
||||||
|
const char* to_string(cpp_access_specifier_kind access) noexcept;
|
||||||
|
|
||||||
|
/// A [cppast::cpp_entity]() modelling a C++ access specifier.
|
||||||
|
class cpp_access_specifier final : public cpp_entity
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// \returns A newly created access specifier.
|
||||||
|
/// \notes It is not meant to be registered at the [cppast::cpp_entity_index](),
|
||||||
|
/// as nothing can refer to it.
|
||||||
|
static std::unique_ptr<cpp_access_specifier> build(cpp_access_specifier_kind kind)
|
||||||
|
{
|
||||||
|
return std::unique_ptr<cpp_access_specifier>(new cpp_access_specifier(kind));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \returns The kind of access specifier.
|
||||||
|
cpp_access_specifier_kind access_specifier() const noexcept
|
||||||
|
{
|
||||||
|
return access_;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
cpp_access_specifier(cpp_access_specifier_kind access)
|
||||||
|
: cpp_entity(to_string(access)), access_(access)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
cpp_entity_kind do_get_entity_kind() const noexcept override;
|
||||||
|
|
||||||
|
cpp_access_specifier_kind access_;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// A [cppast::cpp_entity]() modelling a C++ class.
|
||||||
|
class cpp_class final : public cpp_entity, public cpp_entity_container<cpp_class, cpp_entity>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// Builds a [cppast::cpp_class]().
|
||||||
|
class builder
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// \effects Sets the name and kind and whether it is `final`.
|
||||||
|
explicit builder(std::string name, cpp_class_kind kind, bool is_final)
|
||||||
|
: class_(new cpp_class(std::move(name), kind, is_final))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \effects Builds a [cppast::cpp_access_specifier]() and adds it.
|
||||||
|
void access_specifier(cpp_access_specifier_kind access)
|
||||||
|
{
|
||||||
|
add_child(cpp_access_specifier::build(access));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \effects Adds an entity.
|
||||||
|
void add_child(std::unique_ptr<cpp_entity> child) noexcept
|
||||||
|
{
|
||||||
|
class_->add_child(std::move(child));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \effects Registers the class in the [cppast::cpp_entity_index](),
|
||||||
|
/// using the given [cppast::cpp_entity_id]().
|
||||||
|
/// \returns The finished class.
|
||||||
|
std::unique_ptr<cpp_class> finish(const cpp_entity_index& idx,
|
||||||
|
cpp_entity_id id) noexcept;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<cpp_class> class_;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \returns The keyword used in the declaration of the class.
|
||||||
|
cpp_class_kind class_kind() const noexcept
|
||||||
|
{
|
||||||
|
return kind_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \returns Whether or not the class was declared `final`.
|
||||||
|
bool is_final() const noexcept
|
||||||
|
{
|
||||||
|
return final_;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
cpp_class(std::string name, cpp_class_kind kind, bool final)
|
||||||
|
: cpp_entity(std::move(name)), kind_(kind), final_(final)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
cpp_entity_kind do_get_entity_kind() const noexcept override;
|
||||||
|
|
||||||
|
/// \returns The name of the namespace.
|
||||||
|
type_safe::optional<std::string> do_get_scope_name() const override
|
||||||
|
{
|
||||||
|
return name();
|
||||||
|
}
|
||||||
|
|
||||||
|
cpp_class_kind kind_;
|
||||||
|
bool final_;
|
||||||
|
};
|
||||||
|
} // namespace cppast
|
||||||
|
|
||||||
|
#endif // CPPAST_CPP_CLASS_HPP_INCLUDED
|
||||||
|
|
@ -26,6 +26,9 @@ namespace cppast
|
||||||
enum_t,
|
enum_t,
|
||||||
enum_value_t,
|
enum_value_t,
|
||||||
|
|
||||||
|
class_t,
|
||||||
|
access_specifier_t,
|
||||||
|
|
||||||
variable_t,
|
variable_t,
|
||||||
|
|
||||||
function_parameter_t,
|
function_parameter_t,
|
||||||
|
|
|
||||||
57
src/cpp_class.cpp
Normal file
57
src/cpp_class.cpp
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
// 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_class.hpp>
|
||||||
|
|
||||||
|
#include <cppast/cpp_entity_index.hpp>
|
||||||
|
#include <cppast/cpp_entity_kind.hpp>
|
||||||
|
|
||||||
|
using namespace cppast;
|
||||||
|
|
||||||
|
std::unique_ptr<cpp_class> cpp_class::builder::finish(const cpp_entity_index& idx,
|
||||||
|
cpp_entity_id id) noexcept
|
||||||
|
{
|
||||||
|
idx.register_entity(std::move(id), type_safe::ref(*class_));
|
||||||
|
return std::move(class_);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* cppast::to_string(cpp_class_kind kind) noexcept
|
||||||
|
{
|
||||||
|
switch (kind)
|
||||||
|
{
|
||||||
|
case cpp_class_kind::class_t:
|
||||||
|
return "class";
|
||||||
|
case cpp_class_kind::struct_t:
|
||||||
|
return "struct";
|
||||||
|
case cpp_class_kind::union_t:
|
||||||
|
return "union";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "should not get here";
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* cppast::to_string(cpp_access_specifier_kind access) noexcept
|
||||||
|
{
|
||||||
|
switch (access)
|
||||||
|
{
|
||||||
|
case cpp_public:
|
||||||
|
return "public";
|
||||||
|
case cpp_protected:
|
||||||
|
return "protected";
|
||||||
|
case cpp_private:
|
||||||
|
return "private";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "should not get here either";
|
||||||
|
}
|
||||||
|
|
||||||
|
cpp_entity_kind cpp_access_specifier::do_get_entity_kind() const noexcept
|
||||||
|
{
|
||||||
|
return cpp_entity_kind::access_specifier_t;
|
||||||
|
}
|
||||||
|
|
||||||
|
cpp_entity_kind cpp_class::do_get_entity_kind() const noexcept
|
||||||
|
{
|
||||||
|
return cpp_entity_kind::class_t;
|
||||||
|
}
|
||||||
|
|
@ -33,6 +33,11 @@ const char* cppast::to_string(cpp_entity_kind kind) noexcept
|
||||||
case cpp_entity_kind::enum_value_t:
|
case cpp_entity_kind::enum_value_t:
|
||||||
return "enum value";
|
return "enum value";
|
||||||
|
|
||||||
|
case cpp_entity_kind::class_t:
|
||||||
|
return "class";
|
||||||
|
case cpp_entity_kind::access_specifier_t:
|
||||||
|
return "access specifier";
|
||||||
|
|
||||||
case cpp_entity_kind::variable_t:
|
case cpp_entity_kind::variable_t:
|
||||||
return "variable";
|
return "variable";
|
||||||
|
|
||||||
|
|
@ -52,8 +57,9 @@ bool cppast::is_type(cpp_entity_kind kind) noexcept
|
||||||
{
|
{
|
||||||
switch (kind)
|
switch (kind)
|
||||||
{
|
{
|
||||||
case cpp_entity_kind::enum_t:
|
|
||||||
case cpp_entity_kind::type_alias_t:
|
case cpp_entity_kind::type_alias_t:
|
||||||
|
case cpp_entity_kind::enum_t:
|
||||||
|
case cpp_entity_kind::class_t:
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case cpp_entity_kind::file_t:
|
case cpp_entity_kind::file_t:
|
||||||
|
|
@ -63,6 +69,8 @@ bool cppast::is_type(cpp_entity_kind kind) noexcept
|
||||||
case cpp_entity_kind::using_directive_t:
|
case cpp_entity_kind::using_directive_t:
|
||||||
case cpp_entity_kind::using_declaration_t:
|
case cpp_entity_kind::using_declaration_t:
|
||||||
case cpp_entity_kind::enum_value_t:
|
case cpp_entity_kind::enum_value_t:
|
||||||
|
case cpp_entity_kind::access_specifier_t:
|
||||||
|
break;
|
||||||
case cpp_entity_kind::variable_t:
|
case cpp_entity_kind::variable_t:
|
||||||
case cpp_entity_kind::function_parameter_t:
|
case cpp_entity_kind::function_parameter_t:
|
||||||
case cpp_entity_kind::function_t:
|
case cpp_entity_kind::function_t:
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include <cppast/cpp_entity.hpp>
|
#include <cppast/cpp_entity.hpp>
|
||||||
#include <cppast/cpp_entity_kind.hpp>
|
#include <cppast/cpp_entity_kind.hpp>
|
||||||
|
#include <cppast/cpp_class.hpp>
|
||||||
#include <cppast/cpp_enum.hpp>
|
#include <cppast/cpp_enum.hpp>
|
||||||
#include <cppast/cpp_file.hpp>
|
#include <cppast/cpp_file.hpp>
|
||||||
#include <cppast/cpp_function.hpp>
|
#include <cppast/cpp_function.hpp>
|
||||||
|
|
@ -45,6 +46,8 @@ bool detail::visit(const cpp_entity& e, detail::visitor_callback_t cb, void* fun
|
||||||
return handle_container<cpp_namespace>(e, cb, functor);
|
return handle_container<cpp_namespace>(e, cb, functor);
|
||||||
case cpp_entity_kind::enum_t:
|
case cpp_entity_kind::enum_t:
|
||||||
return handle_container<cpp_enum>(e, cb, functor);
|
return handle_container<cpp_enum>(e, cb, functor);
|
||||||
|
case cpp_entity_kind::class_t:
|
||||||
|
return handle_container<cpp_class>(e, cb, functor);
|
||||||
case cpp_entity_kind::function_t:
|
case cpp_entity_kind::function_t:
|
||||||
return handle_container<cpp_function>(e, cb, functor);
|
return handle_container<cpp_function>(e, cb, functor);
|
||||||
|
|
||||||
|
|
@ -53,6 +56,7 @@ bool detail::visit(const cpp_entity& e, detail::visitor_callback_t cb, void* fun
|
||||||
case cpp_entity_kind::using_declaration_t:
|
case cpp_entity_kind::using_declaration_t:
|
||||||
case cpp_entity_kind::type_alias_t:
|
case cpp_entity_kind::type_alias_t:
|
||||||
case cpp_entity_kind::enum_value_t:
|
case cpp_entity_kind::enum_value_t:
|
||||||
|
case cpp_entity_kind::access_specifier_t:
|
||||||
case cpp_entity_kind::variable_t:
|
case cpp_entity_kind::variable_t:
|
||||||
case cpp_entity_kind::function_parameter_t:
|
case cpp_entity_kind::function_parameter_t:
|
||||||
return cb(functor, e, visitor_info::leaf_entity);
|
return cb(functor, e, visitor_info::leaf_entity);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue