Add basic concept support (#144)

This commit is contained in:
waitingtocompile 2022-09-03 20:22:59 +01:00 committed by GitHub
commit a937efbcbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 396 additions and 16 deletions

View file

@ -0,0 +1,74 @@
// Copyright (C) 2017-2022 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
#ifndef CPPAST_CPP_CONCEPT_HPP_INCLUDED
#define CPPAST_CPP_CONCEPT_HPP_INCLUDED
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_template_parameter.hpp>
#include <cppast/cpp_expression.hpp>
namespace cppast
{
/// A [[cppast::cpp_entity]() modelling a c++ concept declaration
/// \notes while concepts are technically templates,
/// this is not a [cppast::cpp_template](),
/// as concepts act very differently from other templates
class cpp_concept final : public cpp_entity
{
public:
static cpp_entity_kind kind() noexcept;
/// \returns the template parameters as a string
const cpp_token_string& parameters() const noexcept
{
return parameters_;
}
/// \returns the [cppast::cpp_expression]() defining the concept constraint
const cpp_expression& constraint_expression() const noexcept
{
return *expression_;
}
class builder
{
public:
builder(std::string name)
: concept_(new cpp_concept(std::move(name)))
{}
cpp_token_string& set_parameters(cpp_token_string string) noexcept
{
concept_->parameters_ = std::move(string);
return concept_->parameters_;
}
cpp_expression& set_expression(std::unique_ptr<cpp_expression> expression) noexcept
{
concept_->expression_ = std::move(expression);
return *concept_->expression_;
}
std::unique_ptr<cpp_concept> finish(const cpp_entity_index& idx, cpp_entity_id id);
private:
std::unique_ptr<cpp_concept> concept_;
};
private:
cpp_concept(std::string name)
: cpp_entity(std::move(name)), parameters_({})
{}
cpp_entity_kind do_get_entity_kind() const noexcept override;
cpp_token_string parameters_;
std::unique_ptr<cpp_expression> expression_;
};
} // namespace cppast
#endif

View file

@ -57,6 +57,7 @@ enum class cpp_entity_kind
function_template_specialization_t,
class_template_t,
class_template_specialization_t,
concept_t,
static_assert_t,

View file

@ -15,7 +15,7 @@
namespace cppast
{
/// Base class for all entities modelling a C++ template of some kind.
/// Base class for all entities modelling a C++ template of some kind, aside from concepts
///
/// It is a container of a single [cppast::cpp_entity]() that is the entity being templated.
class cpp_template : public cpp_entity, public cpp_entity_container<cpp_template, cpp_entity>

View file

@ -36,7 +36,8 @@ private:
enum class cpp_template_keyword
{
keyword_class,
keyword_typename
keyword_typename,
concept_contraint
};
/// \returns The string associated of the keyword.
@ -52,7 +53,8 @@ public:
/// \notes The `default_type` may be `nullptr` in which case the parameter has no default.
static std::unique_ptr<cpp_template_type_parameter> build(
const cpp_entity_index& idx, cpp_entity_id id, std::string name, cpp_template_keyword kw,
bool variadic, std::unique_ptr<cpp_type> default_type = nullptr);
bool variadic, std::unique_ptr<cpp_type> default_type = nullptr,
type_safe::optional<cpp_token_string> concept_constraint = type_safe::nullopt);
/// \returns A [ts::optional_ref]() to the default type.
type_safe::optional_ref<const cpp_type> default_type() const noexcept
@ -66,17 +68,24 @@ public:
return keyword_;
}
const type_safe::optional<cpp_token_string>& concept_constraint() const noexcept
{
return concept_constraint_;
}
private:
cpp_template_type_parameter(std::string name, cpp_template_keyword kw, bool variadic,
std::unique_ptr<cpp_type> default_type)
std::unique_ptr<cpp_type> default_type,
type_safe::optional<cpp_token_string> concept_constraint)
: cpp_template_parameter(std::move(name), variadic), default_type_(std::move(default_type)),
keyword_(kw)
keyword_(kw), concept_constraint_(concept_constraint)
{}
cpp_entity_kind do_get_entity_kind() const noexcept override;
std::unique_ptr<cpp_type> default_type_;
cpp_template_keyword keyword_;
type_safe::optional<cpp_token_string> concept_constraint_;
};
/// \exclude

View file

@ -20,6 +20,7 @@ class cpp_builtin_type;
class cpp_class;
class cpp_class_template;
class cpp_class_template_specialization;
class cpp_concept;
class cpp_constructor;
class cpp_conversion_op;
class cpp_cv_qualified_type;