Add cpp_variable

This commit is contained in:
Jonathan Müller 2017-01-22 13:06:18 +01:00
commit 664b5a6d4e
5 changed files with 89 additions and 0 deletions

View file

@ -24,6 +24,8 @@ namespace cppast
enum_t,
enum_value_t,
variable_t,
count,
};

View 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.
#ifndef CPPAST_CPP_VARIABLE_HPP_INCLUDED
#define CPPAST_CPP_VARIABLE_HPP_INCLUDED
#include <cppast/cpp_variable_base.hpp>
namespace cppast
{
/// Storage class and other specifiers for a [cppast::cpp_variable]().
enum cpp_variable_specifiers
{
cpp_var_none = 0,
cpp_var_static = 1,
cpp_var_extern = 2,
cpp_var_thread_local = 4,
cpp_var_constexpr = 8,
};
/// 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
{
public:
/// \returns A newly created and registered variable.
/// \notes The default value may be `nullptr` indicating no default value.
static std::unique_ptr<cpp_variable> build(const cpp_entity_index& idx, cpp_entity_id id,
std::string name, std::unique_ptr<cpp_type> type,
std::unique_ptr<cpp_expression> def,
cpp_variable_specifiers spec);
/// \returns The [cppast::cpp_variable_specifiers]() on that variable.
cpp_variable_specifiers specifiers() const noexcept
{
return specifiers_;
}
private:
cpp_variable(std::string name, std::unique_ptr<cpp_type> type,
std::unique_ptr<cpp_expression> def, cpp_variable_specifiers spec)
: cpp_variable_base(std::move(name), std::move(type), std::move(def)), specifiers_(spec)
{
}
cpp_entity_kind do_get_entity_kind() const noexcept override;
cpp_variable_specifiers specifiers_;
};
} // namespace cppast
#endif // CPPAST_CPP_VARIABLE_HPP_INCLUDED

View file

@ -30,6 +30,9 @@ const char* cppast::to_string(cpp_entity_kind kind) noexcept
case cpp_entity_kind::enum_value_t:
return "enum value";
case cpp_entity_kind::variable_t:
return "variable";
case cpp_entity_kind::count:
break;
}
@ -51,6 +54,7 @@ bool cppast::is_type(cpp_entity_kind kind) noexcept
case cpp_entity_kind::using_directive_t:
case cpp_entity_kind::using_declaration_t:
case cpp_entity_kind::enum_value_t:
case cpp_entity_kind::variable_t:
case cpp_entity_kind::count:
break;
}

25
src/cpp_variable.cpp Normal file
View file

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

View file

@ -47,6 +47,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::type_alias_t:
case cpp_entity_kind::enum_value_t:
case cpp_entity_kind::variable_t:
return cb(functor, e, visitor_info::leaf_entity);
case cpp_entity_kind::count: