From 5069f2f167d2f60780716915dbd94ce7bb8079ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20R=C3=BCth?= Date: Mon, 31 Jan 2022 09:19:12 -0500 Subject: [PATCH] Add forward declarations (#116) --- include/cppast/code_generator.hpp | 8 -- include/cppast/compile_config.hpp | 1 + include/cppast/cpp_attribute.hpp | 2 - include/cppast/cpp_entity.hpp | 6 - include/cppast/cpp_entity_index.hpp | 6 +- include/cppast/cpp_entity_kind.hpp | 1 + include/cppast/cpp_entity_ref.hpp | 1 - include/cppast/cpp_forward_declarable.hpp | 5 - include/cppast/cpp_function.hpp | 2 +- .../cppast/cpp_storage_class_specifiers.hpp | 2 + include/cppast/cpp_template_parameter.hpp | 2 - include/cppast/cpp_token.hpp | 2 + include/cppast/cpp_type.hpp | 9 +- include/cppast/cppast_fwd.hpp | 127 ++++++++++++++++++ include/cppast/detail/intrusive_list.hpp | 2 +- include/cppast/diagnostic.hpp | 2 + include/cppast/libclang_parser.hpp | 3 - include/cppast/parser.hpp | 1 - src/CMakeLists.txt | 1 + src/libclang/libclang_parser.cpp | 1 + src/libclang/type_parser.cpp | 19 +++ 21 files changed, 163 insertions(+), 40 deletions(-) create mode 100644 include/cppast/cppast_fwd.hpp diff --git a/include/cppast/code_generator.hpp b/include/cppast/code_generator.hpp index fe2e4e4..8614f81 100644 --- a/include/cppast/code_generator.hpp +++ b/include/cppast/code_generator.hpp @@ -15,8 +15,6 @@ namespace cppast { -enum cpp_access_specifier_kind : int; - /// A simple string view implementation, like [std::string_view](). /// /// It "views" - stores a pointer to - some kind of string. @@ -521,12 +519,6 @@ private: /// \returns Whether or not any code was actually written. bool generate_code(code_generator& generator, const cpp_entity& e); -/// \exclude -class cpp_template_argument; - -/// \exclude -class cpp_token_string; - /// \exclude namespace detail { diff --git a/include/cppast/compile_config.hpp b/include/cppast/compile_config.hpp index ab4b960..66b1bac 100644 --- a/include/cppast/compile_config.hpp +++ b/include/cppast/compile_config.hpp @@ -12,6 +12,7 @@ #include #include +#include namespace cppast { diff --git a/include/cppast/cpp_attribute.hpp b/include/cppast/cpp_attribute.hpp index 3bf95fb..4c288bd 100644 --- a/include/cppast/cpp_attribute.hpp +++ b/include/cppast/cpp_attribute.hpp @@ -107,8 +107,6 @@ type_safe::optional_ref has_attribute(const cpp_attribute_l type_safe::optional_ref has_attribute(const cpp_attribute_list& attributes, cpp_attribute_kind kind); -class cpp_entity; - /// \group has_attribute type_safe::optional_ref has_attribute(const cpp_entity& e, const std::string& name); diff --git a/include/cppast/cpp_entity.hpp b/include/cppast/cpp_entity.hpp index 0997c83..1764256 100644 --- a/include/cppast/cpp_entity.hpp +++ b/include/cppast/cpp_entity.hpp @@ -16,12 +16,6 @@ namespace cppast { -class cpp_entity; -enum class cpp_entity_kind; -class cpp_entity_index; -struct cpp_entity_id; -class cpp_template_parameter; -class cpp_template; /// The name of a scope. /// diff --git a/include/cppast/cpp_entity_index.hpp b/include/cppast/cpp_entity_index.hpp index a36dfed..0f13f7e 100644 --- a/include/cppast/cpp_entity_index.hpp +++ b/include/cppast/cpp_entity_index.hpp @@ -15,12 +15,10 @@ #include #include +#include + namespace cppast { -class cpp_entity; -class cpp_file; -class cpp_namespace; - /// \exclude namespace detail { diff --git a/include/cppast/cpp_entity_kind.hpp b/include/cppast/cpp_entity_kind.hpp index 1346092..520b5f1 100644 --- a/include/cppast/cpp_entity_kind.hpp +++ b/include/cppast/cpp_entity_kind.hpp @@ -6,6 +6,7 @@ #define CPPAST_CPP_ENTITY_KIND_HPP_INCLUDED #include +#include namespace cppast { diff --git a/include/cppast/cpp_entity_ref.hpp b/include/cppast/cpp_entity_ref.hpp index f5157d3..b67be84 100644 --- a/include/cppast/cpp_entity_ref.hpp +++ b/include/cppast/cpp_entity_ref.hpp @@ -14,7 +14,6 @@ namespace cppast { -enum class cpp_entity_kind; /// A basic reference to some kind of [cppast::cpp_entity](). /// diff --git a/include/cppast/cpp_forward_declarable.hpp b/include/cppast/cpp_forward_declarable.hpp index 33fc083..c236f32 100644 --- a/include/cppast/cpp_forward_declarable.hpp +++ b/include/cppast/cpp_forward_declarable.hpp @@ -86,11 +86,6 @@ private: /// \returns Whether or not the given entity is a definition. bool is_definition(const cpp_entity& e) noexcept; -class cpp_enum; -class cpp_class; -class cpp_variable; -class cpp_function_base; - /// Gets the definition of an entity. /// \returns A [ts::optional_ref]() to the entity that is the definition. /// If the entity is a definition or not derived from [cppast::cpp_forward_declarable]() (only valid diff --git a/include/cppast/cpp_function.hpp b/include/cppast/cpp_function.hpp index 71672de..250c9e2 100644 --- a/include/cppast/cpp_function.hpp +++ b/include/cppast/cpp_function.hpp @@ -42,7 +42,7 @@ private: }; /// The kinds of function bodies of a [cppast::cpp_function_base](). -enum cpp_function_body_kind +enum cpp_function_body_kind : int { cpp_function_declaration, //< Just a declaration. cpp_function_definition, //< Regular definition. diff --git a/include/cppast/cpp_storage_class_specifiers.hpp b/include/cppast/cpp_storage_class_specifiers.hpp index 510e101..8f9a775 100644 --- a/include/cppast/cpp_storage_class_specifiers.hpp +++ b/include/cppast/cpp_storage_class_specifiers.hpp @@ -5,6 +5,8 @@ #ifndef CPPAST_CPP_STORAGE_CLASS_SPECIFIERS_HPP_INCLUDED #define CPPAST_CPP_STORAGE_CLASS_SPECIFIERS_HPP_INCLUDED +#include + namespace cppast { /// C++ storage class specifiers. diff --git a/include/cppast/cpp_template_parameter.hpp b/include/cppast/cpp_template_parameter.hpp index 9427c1d..99c6a8f 100644 --- a/include/cppast/cpp_template_parameter.hpp +++ b/include/cppast/cpp_template_parameter.hpp @@ -158,8 +158,6 @@ namespace detail }; } // namespace detail -class cpp_template; - /// A reference to a [cppast::cpp_template]() or a [cppast::cpp_template_template_parameter](). using cpp_template_ref = basic_cpp_entity_ref; diff --git a/include/cppast/cpp_token.hpp b/include/cppast/cpp_token.hpp index 27016bf..f0a1d29 100644 --- a/include/cppast/cpp_token.hpp +++ b/include/cppast/cpp_token.hpp @@ -10,6 +10,8 @@ #include +#include + namespace cppast { /// The kinds of C++ tokens. diff --git a/include/cppast/cpp_type.hpp b/include/cppast/cpp_type.hpp index 76fbc5f..5accc42 100644 --- a/include/cppast/cpp_type.hpp +++ b/include/cppast/cpp_type.hpp @@ -120,7 +120,7 @@ private: }; /// The C++ builtin types. -enum cpp_builtin_type_kind +enum cpp_builtin_type_kind : int { cpp_void, //< `void` @@ -246,9 +246,6 @@ private: } }; -class cpp_template_parameter_type; -class cpp_template_instantiation_type; - /// A [cppast::cpp_type]() that depends on another type. class cpp_dependent_type final : public cpp_type { @@ -291,7 +288,7 @@ private: }; /// The kinds of C++ cv qualifiers. -enum cpp_cv +enum cpp_cv : int { cpp_cv_none, cpp_cv_const, @@ -387,7 +384,7 @@ private: }; /// The kinds of C++ references. -enum cpp_reference +enum cpp_reference : int { cpp_ref_none, cpp_ref_lvalue, diff --git a/include/cppast/cppast_fwd.hpp b/include/cppast/cppast_fwd.hpp new file mode 100644 index 0000000..d8a40d8 --- /dev/null +++ b/include/cppast/cppast_fwd.hpp @@ -0,0 +1,127 @@ +// Copyright (C) 2021 Julian RĂ¼th +// This file is subject to the license terms in the LICENSE file +// found in the top-level directory of this distribution. + +#ifndef CPPAST_FORWARD_HPP_INCLUDED +#define CPPAST_FORWARD_HPP_INCLUDED + +namespace cppast +{ + +class code_generator; +class compile_config; +class cpp_access_specifier; +class cpp_alias_template; +class cpp_array_type; +class cpp_attribute; +class cpp_auto_type; +class cpp_base_class; +class cpp_bitfield; +class cpp_builtin_type; +class cpp_class; +class cpp_class_template; +class cpp_class_template_specialization; +class cpp_constructor; +class cpp_conversion_op; +class cpp_cv_qualified_type; +class cpp_decltype_auto_type; +class cpp_decltype_type; +class cpp_dependent_type; +class cpp_destructor; +class cpp_entity; +class cpp_entity_index; +class cpp_enum; +class cpp_enum_value; +class cpp_expression; +class cpp_file; +class cpp_forward_declarable; +class cpp_friend; +class cpp_function; +class cpp_function_base; +class cpp_function_parameter; +class cpp_function_template; +class cpp_function_template_specialization; +class cpp_function_type; +class cpp_include_directive; +class cpp_language_linkage; +class cpp_literal_expression; +class cpp_macro_definition; +class cpp_macro_parameter; +class cpp_member_function; +class cpp_member_function_base; +class cpp_member_function_type; +class cpp_member_object_type; +class cpp_member_variable_base; +class cpp_namespace; +class cpp_non_type_template_parameter; +class cpp_pointer_type; +class cpp_reference_type; +class cpp_scope_name; +class cpp_static_assert; +class cpp_template; +class cpp_template_argument; +class cpp_template_instantiation_type; +class cpp_template_parameter; +class cpp_template_parameter_type; +class cpp_template_specialization; +class cpp_template_template_parameter; +class cpp_template_type_parameter; +class cpp_token_string; +class cpp_type; +class cpp_type_alias; +class cpp_unexposed_entity; +class cpp_unexposed_expression; +class cpp_unexposed_type; +class cpp_user_defined_type; +class cpp_using_declaration; +class cpp_using_directive; +class cpp_variable; +class cpp_variable_base; +class cpp_variable_template; +class diagnostic_logger; +class libclang_compilation_database; +class libclang_compile_config; +class libclang_error; +class libclang_parser; +class parser; +class string_view; + +enum class compile_flag; +enum class cpp_attribute_kind; +enum class cpp_class_kind; +enum class cpp_entity_kind; +enum class cpp_expression_kind; +enum class cpp_include_kind; +enum class cpp_standard; +enum class cpp_template_keyword; +enum class cpp_token_kind; +enum class cpp_type_kind; +enum class cpp_virtual_flags; +enum class formatting_flags; +enum class severity; +enum class visit_filter; + +enum cpp_access_specifier_kind : int; +enum cpp_builtin_type_kind : int; +enum cpp_cv : int; +enum cpp_function_body_kind : int; +enum cpp_reference : int; +enum cpp_storage_class_specifiers : int; +enum visitor_result : bool; + +struct cpp_doc_comment; +struct cpp_entity_id; +struct cpp_token; +struct diagnostic; +struct newl_t; +struct source_location; +struct visitor_info; +struct whitespace_t; + +template class cpp_entity_container; +template class simple_file_parser; +template class basic_cpp_entity_ref; + +} // namespace cppast + +#endif // CPPAST_FORWARD_HPP_INCLUDED diff --git a/include/cppast/detail/intrusive_list.hpp b/include/cppast/detail/intrusive_list.hpp index 935e8e0..7ddad7d 100644 --- a/include/cppast/detail/intrusive_list.hpp +++ b/include/cppast/detail/intrusive_list.hpp @@ -11,10 +11,10 @@ #include #include +#include namespace cppast { -class cpp_file; namespace detail { diff --git a/include/cppast/diagnostic.hpp b/include/cppast/diagnostic.hpp index 889c8b2..f85933b 100644 --- a/include/cppast/diagnostic.hpp +++ b/include/cppast/diagnostic.hpp @@ -10,6 +10,8 @@ #include +#include + namespace cppast { /// Describes a physical source location attached to a [cppast::diagnostic](). diff --git a/include/cppast/libclang_parser.hpp b/include/cppast/libclang_parser.hpp index e8dad1e..134e7c7 100644 --- a/include/cppast/libclang_parser.hpp +++ b/include/cppast/libclang_parser.hpp @@ -11,9 +11,6 @@ namespace cppast { -class libclang_compile_config; -class libclang_compilation_database; - namespace detail { struct libclang_compile_config_access diff --git a/include/cppast/parser.hpp b/include/cppast/parser.hpp index 3d5279b..e6c2ed6 100644 --- a/include/cppast/parser.hpp +++ b/include/cppast/parser.hpp @@ -15,7 +15,6 @@ namespace cppast { -class cpp_entity_index; /// Base class for a parser. /// diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b9370a6..5062670 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -44,6 +44,7 @@ set(header ../include/cppast/cpp_variable_template.hpp ../include/cppast/diagnostic.hpp ../include/cppast/diagnostic_logger.hpp + ../include/cppast/cppast_fwd.hpp ../include/cppast/libclang_parser.hpp ../include/cppast/parser.hpp ../include/cppast/visitor.hpp) diff --git a/src/libclang/libclang_parser.cpp b/src/libclang/libclang_parser.cpp index 3d252f5..8179a63 100644 --- a/src/libclang/libclang_parser.cpp +++ b/src/libclang/libclang_parser.cpp @@ -494,6 +494,7 @@ namespace std::vector get_arguments(const libclang_compile_config& config) { std::vector args + // TODO: Why? and Why? = {"-x", "c++", "-I."}; // force C++ and enable current directory for include search for (auto& flag : detail::libclang_compile_config_access::flags(config)) args.push_back(flag.c_str()); diff --git a/src/libclang/type_parser.cpp b/src/libclang/type_parser.cpp index 05a6369..327d27b 100644 --- a/src/libclang/type_parser.cpp +++ b/src/libclang/type_parser.cpp @@ -99,6 +99,25 @@ bool need_to_remove_scope(const CXCursor& cur, const CXType& type) std::string get_type_spelling(const CXCursor& cur, const CXType& type) { + // TODO: There are two interesting things we should keep track of here: + // * The canonical name of a type (easy, clang has a shortcut for that) + // * The type definition of this type, say for: + // std::vector this should be the type (we + // should ignore specialization here!) vector whose parent is a + // namespace in this case but could again be a concrete type as well (to + // handle nested type specializations correctly.) + /* + auto ns_name = [](const CXCursor& cur) { + auto parent = clang_getCursorSemanticParent(cur); + if (clang_getCursorKind(parent) == CXCursor_TranslationUnit) + return ""; + if (clang_getCursorKind(parent) == CXCursor_Namespace) + return spelling of cursor ... + }; + */ + + // auto canonical = detail::cxstring(clang_getTypeSpelling(clang_getCanonicalType(type))).std_str(); + // auto declaration = detail::cxstring(clang_getCursorSpelling(clang_getTypeDeclaration(type))).std_str(); auto spelling = detail::cxstring(clang_getTypeSpelling(type)).std_str(); if (need_to_remove_scope(cur, type)) {