// The Construct // // Copyright (C) The Construct Developers, Authors & Contributors // Copyright (C) 2016-2022 Jason Volk // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. #pragma once #define HAVE_IRCD_SPIRIT_QI_RULE_H #if defined(__clang__) /// Custom reimplementation/interposition of the qi::rule template by /// specializing the `const char *` iterator, which is common to our parsers. /// /// The goal here is to support our custom lightweight function object (see /// function.h). This implementation is optimized for our grammar's patterns, /// which generally involve constant/immutable exprs and static global rules. /// /// Dynamic memory has been removed which would otherwise impede optimization /// at global scope: /// /// - For the rule's name, which was formerly std::string. The name is now a /// `const char *` which should point to a literal. /// /// - To support the custom function object, a fixed array is contained /// directly within this class. The parser_binder is placement new'ed there. /// template struct boost::spirit::qi::rule :proto::extends < typename proto::terminal>>::type ,rule > ,parser < rule > { using Iterator = const char *; using iterator_type = Iterator; using this_type = rule; using reference_ = reference; using terminal = typename proto::terminal::type; using terminal_type = typename proto::terminal::type; using parser_type = parser; using base_type = proto::extends; using template_params = mpl::vector; using locals_type = typename spirit::detail::extract_locals::type; using skipper_type = typename spirit::detail::extract_component::type; using encoding_type = typename spirit::detail::extract_encoding::type; using sig_type = typename spirit::detail::extract_sig::type; using attr_type = typename spirit::detail::attr_from_sig::type; using attr_reference_type = attr_type &; using parameter_types = typename spirit::detail::params_from_sig::type; using parameter_types_size = typename fusion::result_of::size::type; using context_type = context, locals_type>; using function_prototype = bool (Iterator &, const Iterator &, context_type &, const skipper_type &); using function_type = function; using encoding_modifier_type = typename mpl::if_ < is_same, unused_type, tag::char_code >::type; template struct attribute { using type = attr_type; }; static constexpr const size_t ¶ms_size { parameter_types_size::value }; private: BOOST_STATIC_ASSERT_MSG ( !is_reference::value, "Reference qualifier on Qi rule attribute is meaningless" ); template static decltype(auto) define(Expr&& expr, mpl::false_) { BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Expr); } template [[warn_unused_result]] static decltype(auto) define(Expr&& expr, mpl::true_) { return detail::bind_parser ( compile(std::forward(expr), encoding_modifier_type()) ); } template static decltype(auto) create(Expr&& expr) { return define ( std::forward(expr), traits::matches() ); } /// Slightly exceeds the worst-case for currently in use compiled /// expressions. It is possible to build an expression which exceeds /// this value, and in that case, feel free to increase this size. static constexpr const size_t &buf_sz { 2048 - 32 // offsetof buf }; function_type func; const char *_name; size_t _size; [[clang::loader_uninitialized]] uint8_t buf alignas(32) [buf_sz]; public: using parameterized_subject_type = const rule; const rule &get_parameterized_subject() const { return *this; } reference_ alias() const noexcept { return reference_(*this); } std::string_view name() const { return _name; } template info what(Context&) const noexcept { return info(std::string(name())); } template bool parse(Iterator &, const Iterator &, C &, const S &, A &) const; template bool parse(Iterator &, const Iterator &, C &, const S &, A &, const P &) const; // bring in the operator() overloads #include /// Expression compiler; primary construction. template rule(const Expr &expr, const char *const name = "") noexcept; /// Renaming constructor. This supports a special case when this rule is /// constructed from a single other rule, but a name is still provided as /// the second argument; prevents forced indirection and bad codegen. template explicit rule(const rule &o, const char *const name) noexcept; /// Copy construction is supported through the rename-ctor, just using the /// name from the rhs. rule(const rule &o) :rule(o, o._name) {} rule() = delete; rule(rule &&o) = delete; rule& operator=(rule &&) = delete; rule& operator=(const rule &) = delete; ~rule() noexcept = default; }; template template inline boost::spirit::qi::rule::rule(const Expr &expr, const char *const name) noexcept :base_type(terminal::make(reference_(*this))) ,func(new (buf) decltype(create(expr)) (create(expr))) ,_name(name) ,_size(sizeof(decltype(create(expr)))) { /// If this assertion has tripped first ensure you are not copying /// an instance of this rule, otherwise see the comment on buf_sz. static_assert ( sizeof(decltype(create(expr))) <= buf_sz, "Instance buffer is insufficient for the binder object." ); } template template inline boost::spirit::qi::rule::rule(const rule &o, const char *const name) noexcept :base_type(terminal::make(reference_(*this))) ,func(o.func, buf) ,_name(name) ,_size(o._size) { assert(_size <= buf_sz); memcpy(buf, o.buf, _size); } template template inline bool boost::spirit::qi::rule::parse(Iterator &first, const Iterator &last, Context &, const Skipper &skipper, Attribute &attr_param) const { BOOST_STATIC_ASSERT_MSG ( (is_same::value || !is_same::value), "The rule was instantiated with a skipper type but you have not pass any. " "Did you use `parse` instead of `phrase_parse`?" ); BOOST_STATIC_ASSERT_MSG ( (is_convertible::value), "The passed skipper is not compatible/convertible to one " "that the rule was instantiated with" ); if constexpr(is_same::value) qi::skip_over(first, last, skipper); using transform = traits::transform_attribute; typename transform::type attr_(transform::pre(attr_param)); context_type context(attr_); assume(bool(func)); const bool ret { func(first, last, context, skipper) }; if(ret) transform::post(attr_param, attr_); else transform::fail(attr_param); return ret; } template template inline bool boost::spirit::qi::rule::parse(Iterator &first, const Iterator &last, Context &caller_context, const Skipper &skipper, Attribute &attr_param, const Params ¶ms) const { BOOST_STATIC_ASSERT_MSG ( (is_same::value || !is_same::value), "The rule was instantiated with a skipper type but you have not pass any. " "Did you use `parse` instead of `phrase_parse`?" ); BOOST_STATIC_ASSERT_MSG ( (is_convertible::value), "The passed skipper is not compatible/convertible to one " "that the rule was instantiated with" ); if constexpr(is_same::value) qi::skip_over(first, last, skipper); using transform = traits::transform_attribute; typename transform::type attr_(transform::pre(attr_param)); context_type context(attr_, params, caller_context); assume(bool(func)); const bool ret { func(first, last, context, skipper) }; if(ret) transform::post(attr_param, attr_); else transform::fail(attr_param); return ret; } #endif defined(__clang__)