Add cpp_namespace
This commit is contained in:
parent
170c4045e2
commit
c25103e680
9 changed files with 113 additions and 9 deletions
|
|
@ -36,15 +36,20 @@ namespace cppast
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \returns A [ts::optional_ref]() to the parent entity in the AST.
|
/// \returns A [ts::optional_ref]() to the parent entity in the AST.
|
||||||
type_safe::optional_ref<cpp_entity> parent() const noexcept
|
type_safe::optional_ref<const cpp_entity> parent() const noexcept
|
||||||
{
|
{
|
||||||
return parent_;
|
return parent_;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/// \effects Creates it giving it the parent entity and the name.
|
/// \effects Creates it giving it the parent entity and the name.
|
||||||
cpp_entity(type_safe::optional_ref<cpp_entity> parent, std::string name)
|
cpp_entity(const cpp_entity& parent, std::string name)
|
||||||
: parent_(std::move(parent)), name_(std::move(name))
|
: parent_(parent), name_(std::move(name))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \effects Creates it giving it no parent and the name.
|
||||||
|
cpp_entity(std::nullptr_t, std::string name) : name_(std::move(name))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,10 +57,11 @@ namespace cppast
|
||||||
/// \returns The type of the entity.
|
/// \returns The type of the entity.
|
||||||
virtual cpp_entity_type do_get_entity_type() const noexcept = 0;
|
virtual cpp_entity_type do_get_entity_type() const noexcept = 0;
|
||||||
|
|
||||||
type_safe::optional_ref<cpp_entity> parent_;
|
type_safe::optional_ref<const cpp_entity> parent_;
|
||||||
std::string name_;
|
std::string name_;
|
||||||
|
|
||||||
friend detail::intrusive_list<cpp_entity>;
|
template <typename T>
|
||||||
|
friend struct detail::intrusive_list_access;
|
||||||
};
|
};
|
||||||
} // namespace cppast
|
} // namespace cppast
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ namespace cppast
|
||||||
enum class cpp_entity_type
|
enum class cpp_entity_type
|
||||||
{
|
{
|
||||||
file_t,
|
file_t,
|
||||||
|
|
||||||
|
namespace_t,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// \returns Whether or not a given entity type is one derived from [cppast::cpp_scope]().
|
/// \returns Whether or not a given entity type is one derived from [cppast::cpp_scope]().
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
namespace cppast
|
namespace cppast
|
||||||
{
|
{
|
||||||
/// A [cppast::cpp_entity]() referring to a file.
|
/// A [cppast::cpp_entity]() modelling a file.
|
||||||
///
|
///
|
||||||
/// This is the top-level entity of the AST.
|
/// This is the top-level entity of the AST.
|
||||||
class cpp_file final : public cpp_entity
|
class cpp_file final : public cpp_entity
|
||||||
|
|
@ -36,6 +36,12 @@ namespace cppast
|
||||||
return std::move(file_);
|
return std::move(file_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// \returns A reference to (not yet finished) file.
|
||||||
|
const cpp_file& get() const noexcept
|
||||||
|
{
|
||||||
|
return *file_;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<cpp_file> file_;
|
std::unique_ptr<cpp_file> file_;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
66
include/cppast/cpp_namespace.hpp
Normal file
66
include/cppast/cpp_namespace.hpp
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
// 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_NAMESPACE_HPP_INCLUDED
|
||||||
|
#define CPPAST_CPP_NAMESPACE_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include <cppast/cpp_scope.hpp>
|
||||||
|
|
||||||
|
namespace cppast
|
||||||
|
{
|
||||||
|
/// A [cppast::cpp_entity]() modelling a namespace.
|
||||||
|
class cpp_namespace : public cpp_scope
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// Builds a [cppast::cpp_namespace]().
|
||||||
|
class builder
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// \effects Sets the namespace name and whether it is inline.
|
||||||
|
explicit builder(const cpp_entity& parent, std::string name, bool is_inline)
|
||||||
|
: namespace_(new cpp_namespace(parent, std::move(name), is_inline))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \effects Adds an entity.
|
||||||
|
void add_child(std::unique_ptr<cpp_entity> child) noexcept
|
||||||
|
{
|
||||||
|
namespace_->add_child(std::move(child));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \returns The finished namespace.
|
||||||
|
std::unique_ptr<cpp_namespace> finish() noexcept
|
||||||
|
{
|
||||||
|
return std::move(namespace_);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \returns A reference to (not yet finished) namespace.
|
||||||
|
const cpp_namespace& get() const noexcept
|
||||||
|
{
|
||||||
|
return *namespace_;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<cpp_namespace> namespace_;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \returns Whether or not the namespace is an `inline namespace`.
|
||||||
|
bool is_inline() const noexcept
|
||||||
|
{
|
||||||
|
return inline_;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
cpp_namespace(const cpp_entity& parent, std::string name, bool is_inline)
|
||||||
|
: cpp_scope(parent, std::move(name)), inline_(is_inline)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
cpp_entity_type do_get_entity_type() const noexcept override;
|
||||||
|
|
||||||
|
bool inline_;
|
||||||
|
};
|
||||||
|
} // namespace cppast
|
||||||
|
|
||||||
|
#endif // CPPAST_CPP_NAMESPACE_HPP_INCLUDED
|
||||||
|
|
@ -44,6 +44,8 @@ namespace cppast
|
||||||
/// \effects Adds a new child to the scope.
|
/// \effects Adds a new child to the scope.
|
||||||
void add_child(std::unique_ptr<cpp_entity> ptr) noexcept
|
void add_child(std::unique_ptr<cpp_entity> ptr) noexcept
|
||||||
{
|
{
|
||||||
|
DEBUG_ASSERT(ptr->parent() == *this, detail::precondition_error_handler{},
|
||||||
|
"parent not set properly");
|
||||||
children_.push_back(std::move(ptr));
|
children_.push_back(std::move(ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,11 @@ namespace cppast
|
||||||
struct assert_handler : debug_assert::set_level<1>, debug_assert::default_handler
|
struct assert_handler : debug_assert::set_level<1>, debug_assert::default_handler
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct precondition_error_handler : debug_assert::set_level<1>,
|
||||||
|
debug_assert::default_handler
|
||||||
|
{
|
||||||
|
};
|
||||||
}
|
}
|
||||||
} // namespace cppast::detail
|
} // namespace cppast::detail
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,14 +29,14 @@ namespace cppast
|
||||||
struct intrusive_list_access
|
struct intrusive_list_access
|
||||||
{
|
{
|
||||||
template <typename U>
|
template <typename U>
|
||||||
static T* get_next(const intrusive_list_node<U>& obj)
|
static T* get_next(const U& obj)
|
||||||
{
|
{
|
||||||
static_assert(std::is_base_of<U, T>::value, "must be a base");
|
static_assert(std::is_base_of<U, T>::value, "must be a base");
|
||||||
return static_cast<T*>(obj.next_.get());
|
return static_cast<T*>(obj.next_.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
static T* set_next(intrusive_list_node<U>& obj, std::unique_ptr<T> node)
|
static T* set_next(U& obj, std::unique_ptr<T> node)
|
||||||
{
|
{
|
||||||
static_assert(std::is_base_of<U, T>::value, "must be a base");
|
static_assert(std::is_base_of<U, T>::value, "must be a base");
|
||||||
obj.next_ = std::move(node);
|
obj.next_ = std::move(node);
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,9 @@ bool cppast::is_scope(cpp_entity_type type) noexcept
|
||||||
{
|
{
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
|
case cpp_entity_type::namespace_t:
|
||||||
|
return true;
|
||||||
|
|
||||||
case cpp_entity_type::file_t:
|
case cpp_entity_type::file_t:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
14
src/cpp_namespace.cpp
Normal file
14
src/cpp_namespace.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
// 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_namespace.hpp>
|
||||||
|
|
||||||
|
#include <cppast/cpp_entity_type.hpp>
|
||||||
|
|
||||||
|
using namespace cppast;
|
||||||
|
|
||||||
|
cpp_entity_type cpp_namespace::do_get_entity_type() const noexcept
|
||||||
|
{
|
||||||
|
return cpp_entity_type::namespace_t;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue