Add simple cpp_template_parameter implementations

This commit is contained in:
Jonathan Müller 2017-01-26 17:34:59 +01:00
commit 67a1b01efc
9 changed files with 187 additions and 12 deletions

View file

@ -41,6 +41,9 @@ namespace cppast
constructor_t,
destructor_t,
template_type_parameter_t,
non_type_template_parameter_t,
count,
};

View file

@ -5,6 +5,7 @@
#ifndef CPPAST_CPP_FUNCTION_HPP_INCLUDED
#define CPPAST_CPP_FUNCTION_HPP_INCLUDED
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_entity_container.hpp>
#include <cppast/cpp_storage_specifiers.hpp>
#include <cppast/cpp_variable_base.hpp>
@ -12,7 +13,7 @@
namespace cppast
{
/// A [cppast::cpp_entity]() modelling a function parameter.
class cpp_function_parameter final : public cpp_variable_base
class cpp_function_parameter final : public cpp_entity, public cpp_variable_base
{
public:
/// \returns A newly created and registered function parameter.
@ -23,7 +24,7 @@ namespace cppast
private:
cpp_function_parameter(std::string name, std::unique_ptr<cpp_type> type,
std::unique_ptr<cpp_expression> def)
: cpp_variable_base(std::move(name), std::move(type), std::move(def))
: cpp_entity(std::move(name)), cpp_variable_base(std::move(type), std::move(def))
{
}

View file

@ -5,12 +5,13 @@
#ifndef CPPAST_CPP_MEMBER_VARIABLE_HPP_INCLUDED
#define CPPAST_CPP_MEMBER_VARIABLE_HPP_INCLUDED
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_variable_base.hpp>
namespace cppast
{
/// Base class for all kinds of member variables.
class cpp_member_variable_base : public cpp_variable_base
class cpp_member_variable_base : public cpp_entity, public cpp_variable_base
{
public:
/// \returns Whether or not the member variable is declared `mutable`.
@ -22,7 +23,9 @@ namespace cppast
protected:
cpp_member_variable_base(std::string name, std::unique_ptr<cpp_type> type,
std::unique_ptr<cpp_expression> def, bool is_mutable)
: cpp_variable_base(std::move(name), std::move(type), std::move(def)), mutable_(is_mutable)
: cpp_entity(std::move(name)),
cpp_variable_base(std::move(type), std::move(def)),
mutable_(is_mutable)
{
}

View file

@ -0,0 +1,105 @@
// 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_TEMPLATE_PARAMETER_HPP_INCLUDED
#define CPPAST_CPP_TEMPLATE_PARAMETER_HPP_INCLUDED
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_variable_base.hpp>
namespace cppast
{
/// Base class for all entities modelling a template parameter of some kind.
class cpp_template_parameter : public cpp_entity
{
public:
/// \returns Whether or not the parameter is variadic.
bool is_variadic() const noexcept
{
return variadic_;
}
protected:
cpp_template_parameter(std::string name, bool variadic)
: cpp_entity(std::move(name)), variadic_(variadic)
{
}
private:
bool variadic_;
};
/// The kind of keyword used in a template parameter.
enum class cpp_template_keyword
{
keyword_class,
keyword_typename
};
/// \returns The string associated of the keyword.
const char* to_string(cpp_template_keyword kw) noexcept;
/// A [cppast::cpp_entity]() modelling a C++ template type parameter.
class cpp_template_type_parameter final : public cpp_template_parameter
{
public:
/// \returns A newly created and registered template type parameter.
/// \notes The `default_type` may be `nullptr` in which case the parameter has no default.
static std::unique_ptr<cpp_template_type_parameter> build(
const cpp_entity_index& idx, cpp_entity_id id, std::string name,
cpp_template_keyword kw, bool variadic,
std::unique_ptr<cpp_type> default_type = nullptr);
/// \returns A [ts::optional_ref]() to the default type.
type_safe::optional_ref<const cpp_type> default_type() const noexcept
{
return type_safe::opt_cref(default_type_.get());
}
/// \returns The keyword used in the template parameter.
cpp_template_keyword keyword() const noexcept
{
return keyword_;
}
private:
cpp_template_type_parameter(std::string name, cpp_template_keyword kw, bool variadic,
std::unique_ptr<cpp_type> default_type)
: cpp_template_parameter(std::move(name), variadic),
default_type_(std::move(default_type)),
keyword_(kw)
{
}
cpp_entity_kind do_get_entity_kind() const noexcept override;
std::unique_ptr<cpp_type> default_type_;
cpp_template_keyword keyword_;
};
/// A [cppast::cpp_entity]() modelling a non-type template parameter.
class cpp_non_type_template_parameter final : public cpp_template_parameter,
public cpp_variable_base
{
public:
/// \returns A newly created and registered non type template parameter.
/// \notes The `default_value` may be `nullptr` in which case the parameter has no default.
static std::unique_ptr<cpp_non_type_template_parameter> build(
const cpp_entity_index& idx, cpp_entity_id id, std::string name,
std::unique_ptr<cpp_type> type, bool is_variadic,
std::unique_ptr<cpp_expression> default_value = nullptr);
private:
cpp_non_type_template_parameter(std::string name, std::unique_ptr<cpp_type> type,
bool variadic, std::unique_ptr<cpp_expression> def)
: cpp_template_parameter(std::move(name), variadic),
cpp_variable_base(std::move(type), std::move(def))
{
}
cpp_entity_kind do_get_entity_kind() const noexcept override;
};
} // namespace cppast
#endif // CPPAST_CPP_TEMPLATE_PARAMETER_HPP_INCLUDED

View file

@ -5,6 +5,7 @@
#ifndef CPPAST_CPP_VARIABLE_HPP_INCLUDED
#define CPPAST_CPP_VARIABLE_HPP_INCLUDED
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_storage_specifiers.hpp>
#include <cppast/cpp_variable_base.hpp>
@ -13,7 +14,7 @@ namespace cppast
/// A [cppast::cpp_entity]() modelling a C++ variable.
/// \notes This is not a member variable,
/// use [cppast::cpp_member_variable]() for that.
class cpp_variable final : public cpp_variable_base
class cpp_variable final : public cpp_entity, public cpp_variable_base
{
public:
/// \returns A newly created and registered variable.
@ -39,7 +40,8 @@ namespace cppast
cpp_variable(std::string name, std::unique_ptr<cpp_type> type,
std::unique_ptr<cpp_expression> def, cpp_storage_specifiers spec,
bool is_constexpr)
: cpp_variable_base(std::move(name), std::move(type), std::move(def)),
: cpp_entity(std::move(name)),
cpp_variable_base(std::move(type), std::move(def)),
storage_(spec),
is_constexpr_(is_constexpr)
{

View file

@ -5,17 +5,16 @@
#ifndef CPPAST_CPP_VARIABLE_BASE_HPP_INCLUDED
#define CPPAST_CPP_VARIABLE_BASE_HPP_INCLUDED
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_expression.hpp>
#include <cppast/cpp_type.hpp>
namespace cppast
{
/// Base class for all [cppast::cpp_entity]() modelling some kind of variable.
/// Additional base class for all [cppast::cpp_entity]() modelling some kind of variable.
///
/// Examples are [cppast::cpp_variable]() or [cppast::cpp_function_parameter](),
/// or anything that is name/type/default-value triple.
class cpp_variable_base : public cpp_entity
class cpp_variable_base
{
public:
/// \returns A reference to the [cppast::cpp_type]() of the variable.
@ -31,12 +30,13 @@ namespace cppast
}
protected:
cpp_variable_base(std::string name, std::unique_ptr<cpp_type> type,
std::unique_ptr<cpp_expression> def)
: cpp_entity(std::move(name)), type_(std::move(type)), default_(std::move(def))
cpp_variable_base(std::unique_ptr<cpp_type> type, std::unique_ptr<cpp_expression> def)
: type_(std::move(type)), default_(std::move(def))
{
}
~cpp_variable_base() noexcept = default;
private:
std::unique_ptr<cpp_type> type_;
std::unique_ptr<cpp_expression> default_;

View file

@ -60,6 +60,11 @@ const char* cppast::to_string(cpp_entity_kind kind) noexcept
case cpp_entity_kind::destructor_t:
return "destructor";
case cpp_entity_kind::template_type_parameter_t:
return "template type parameter";
case cpp_entity_kind::non_type_template_parameter_t:
return "non type template parameter";
case cpp_entity_kind::count:
break;
}
@ -94,6 +99,8 @@ bool cppast::is_type(cpp_entity_kind kind) noexcept
case cpp_entity_kind::conversion_op_t:
case cpp_entity_kind::constructor_t:
case cpp_entity_kind::destructor_t:
case cpp_entity_kind::template_type_parameter_t:
case cpp_entity_kind::non_type_template_parameter_t:
case cpp_entity_kind::count:
break;
}

View file

@ -0,0 +1,52 @@
// 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_template_parameter.hpp>
#include <cppast/cpp_entity_kind.hpp>
using namespace cppast;
const char* cppast::to_string(cpp_template_keyword kw) noexcept
{
switch (kw)
{
case cpp_template_keyword::keyword_class:
return "class";
case cpp_template_keyword::keyword_typename:
return "typename";
}
return "should not get here";
}
std::unique_ptr<cpp_template_type_parameter> cpp_template_type_parameter::build(
const cpp_entity_index& idx, cpp_entity_id id, std::string name, cpp_template_keyword kw,
bool variadic, std::unique_ptr<cpp_type> default_type)
{
std::unique_ptr<cpp_template_type_parameter> result(
new cpp_template_type_parameter(std::move(name), kw, variadic, std::move(default_type)));
idx.register_entity(std::move(id), type_safe::cref(*result));
return result;
}
cpp_entity_kind cpp_template_type_parameter::do_get_entity_kind() const noexcept
{
return cpp_entity_kind::template_type_parameter_t;
}
std::unique_ptr<cpp_non_type_template_parameter> cpp_non_type_template_parameter::build(
const cpp_entity_index& idx, cpp_entity_id id, std::string name, std::unique_ptr<cpp_type> type,
bool is_variadic, std::unique_ptr<cpp_expression> default_value)
{
std::unique_ptr<cpp_non_type_template_parameter> result(
new cpp_non_type_template_parameter(std::move(name), std::move(type), is_variadic,
std::move(default_value)));
idx.register_entity(std::move(id), type_safe::cref(*result));
return result;
}
cpp_entity_kind cpp_non_type_template_parameter::do_get_entity_kind() const noexcept
{
return cpp_entity_kind::non_type_template_parameter_t;
}

View file

@ -70,6 +70,8 @@ bool detail::visit(const cpp_entity& e, detail::visitor_callback_t cb, void* fun
case cpp_entity_kind::bitfield_t:
case cpp_entity_kind::function_parameter_t:
case cpp_entity_kind::destructor_t:
case cpp_entity_kind::template_type_parameter_t:
case cpp_entity_kind::non_type_template_parameter_t:
return cb(functor, e, visitor_info::leaf_entity);
case cpp_entity_kind::count: