From 170c4045e20a60997dad038b5ce7b601e0228c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Fri, 20 Jan 2017 23:21:19 +0100 Subject: [PATCH] Add cpp_file --- include/cppast/cpp_entity.hpp | 2 +- include/cppast/cpp_entity_type.hpp | 2 +- include/cppast/cpp_file.hpp | 69 ++++++++++++++++++++++++ include/cppast/detail/intrusive_list.hpp | 10 ++-- src/cpp_entity_type.cpp | 2 + src/cpp_file.cpp | 14 +++++ 6 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 include/cppast/cpp_file.hpp create mode 100644 src/cpp_file.cpp diff --git a/include/cppast/cpp_entity.hpp b/include/cppast/cpp_entity.hpp index 98c3085..049a6bc 100644 --- a/include/cppast/cpp_entity.hpp +++ b/include/cppast/cpp_entity.hpp @@ -55,7 +55,7 @@ namespace cppast type_safe::optional_ref parent_; std::string name_; - friend detail::intrusive_list_access; + friend detail::intrusive_list; }; } // namespace cppast diff --git a/include/cppast/cpp_entity_type.hpp b/include/cppast/cpp_entity_type.hpp index 13a23b0..8f1efe4 100644 --- a/include/cppast/cpp_entity_type.hpp +++ b/include/cppast/cpp_entity_type.hpp @@ -10,7 +10,7 @@ namespace cppast /// All possible types of C++ entities. enum class cpp_entity_type { - + file_t, }; /// \returns Whether or not a given entity type is one derived from [cppast::cpp_scope](). diff --git a/include/cppast/cpp_file.hpp b/include/cppast/cpp_file.hpp new file mode 100644 index 0000000..6c78dd4 --- /dev/null +++ b/include/cppast/cpp_file.hpp @@ -0,0 +1,69 @@ +// 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_FILE_HPP_INCLUDED +#define CPPAST_CPP_FILE_HPP_INCLUDED + +#include + +namespace cppast +{ + /// A [cppast::cpp_entity]() referring to a file. + /// + /// This is the top-level entity of the AST. + class cpp_file final : public cpp_entity + { + public: + /// Builds a [cppast::cpp_file](). + class builder + { + public: + /// \effects Sets the file name. + explicit builder(std::string name) : file_(new cpp_file(std::move(name))) + { + } + + /// \effects Adds an entity. + void add_child(std::unique_ptr child) noexcept + { + file_->children_.push_back(std::move(child)); + } + + /// \returns The finished file. + std::unique_ptr finish() noexcept + { + return std::move(file_); + } + + private: + std::unique_ptr file_; + }; + + using iterator = detail::intrusive_list::const_iterator; + + /// \returns A const iterator to the first child. + iterator begin() const noexcept + { + return children_.begin(); + } + + /// \returns A const iterator to the last child. + iterator end() const noexcept + { + return children_.end(); + } + + private: + cpp_file(std::string name) : cpp_entity(nullptr, std::move(name)) + { + } + + /// \returns [cpp_entity_type::file_t](). + cpp_entity_type do_get_entity_type() const noexcept override; + + detail::intrusive_list children_; + }; +} // namespace cppast + +#endif // CPPAST_CPP_FILE_HPP_INCLUDED diff --git a/include/cppast/detail/intrusive_list.hpp b/include/cppast/detail/intrusive_list.hpp index 4e685f8..cb5cb9d 100644 --- a/include/cppast/detail/intrusive_list.hpp +++ b/include/cppast/detail/intrusive_list.hpp @@ -16,9 +16,6 @@ namespace cppast { namespace detail { - template - class intrusive_list; - template class intrusive_list_node { @@ -103,7 +100,8 @@ namespace cppast T* cur_; - friend intrusive_list; + template + friend class intrusive_list; }; template @@ -115,12 +113,12 @@ namespace cppast //=== modifiers ===// void push_back(std::unique_ptr obj) noexcept { - DEBUG_ASSERT(obj, detail::assert_handler{}); + DEBUG_ASSERT(obj != nullptr, detail::assert_handler{}); if (last_) { auto ptr = intrusive_list_access::set_next(last_.value(), std::move(obj)); - last_ = type_safe::opt_cref(ptr); + last_ = *ptr; } else { diff --git a/src/cpp_entity_type.cpp b/src/cpp_entity_type.cpp index 20b530d..4f5525f 100644 --- a/src/cpp_entity_type.cpp +++ b/src/cpp_entity_type.cpp @@ -10,6 +10,8 @@ bool cppast::is_scope(cpp_entity_type type) noexcept { switch (type) { + case cpp_entity_type::file_t: + break; } return false; diff --git a/src/cpp_file.cpp b/src/cpp_file.cpp new file mode 100644 index 0000000..c814913 --- /dev/null +++ b/src/cpp_file.cpp @@ -0,0 +1,14 @@ +// 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; + +cpp_entity_type cpp_file::do_get_entity_type() const noexcept +{ + return cpp_entity_type::file_t; +}