10 Commits
1.2.0 ... 1.3.0

Author SHA1 Message Date
Dustin Spicuzza
29b71ab2ed Merge pull request #87 from justinboswell/ctad
Added support for template deduction guides
2023-12-05 17:51:52 -05:00
Justin Boswell
88a7048513 Added support for template deduction guides
* Added DeductionGuide as a language element
2023-12-05 17:49:14 -05:00
Dustin Spicuzza
64c5290318 Merge pull request #89 from robotpy/fn-constraints
Move non-template requires to the function
2023-12-03 01:04:11 -05:00
Dustin Spicuzza
85f93ec09e Move non-template requires to the function
- Methods can have a requires() that refer to the class template without
  an explicit function template
- This is a breaking change, but since the values aren't parsed yet I
  can't imagine anyone is using it
2023-12-02 04:51:00 -05:00
Dustin Spicuzza
04ba4bffae Merge pull request #88 from robotpy/more-using
Retain doxygen comments for using declarations and type aliases
2023-12-02 04:45:11 -05:00
Dustin Spicuzza
73a81d3107 Retain doxygen comments for using declarations and type aliases 2023-12-02 04:24:10 -05:00
Dustin Spicuzza
f1708bf9b8 Merge pull request #85 from robotpy/static-inline
Allow fields to be marked inline
2023-11-19 12:50:09 -05:00
Dustin Spicuzza
cafb594179 Allow fields to be marked inline
- Fixes #84
2023-11-19 12:47:09 -05:00
Dustin Spicuzza
0e732f1d43 Merge pull request #82 from robotpy/trailing-return-type-body
Consume function body if present after trailing return type
2023-11-13 23:27:15 -05:00
Dustin Spicuzza
42bc6b60ad Consume function body if present after trailing return type
- Fixes #81
2023-11-13 23:23:40 -05:00
9 changed files with 447 additions and 62 deletions

View File

@@ -25,6 +25,7 @@ from .types import (
Concept,
DecltypeSpecifier,
DecoratedType,
DeductionGuide,
EnumDecl,
Enumerator,
Field,
@@ -992,7 +993,9 @@ class CxxParser:
self.visitor.on_using_namespace(state, names)
def _parse_using_declaration(self, tok: LexToken) -> None:
def _parse_using_declaration(
self, tok: LexToken, doxygen: typing.Optional[str]
) -> None:
"""
using_declaration: "using" ["typename"] ["::"] nested_name_specifier unqualified_id ";"
| "using" "::" unqualified_id ";"
@@ -1004,12 +1007,15 @@ class CxxParser:
typename, _ = self._parse_pqname(
tok, fn_ok=True, compound_ok=True, fund_ok=True
)
decl = UsingDecl(typename, self._current_access)
decl = UsingDecl(typename, self._current_access, doxygen)
self.visitor.on_using_declaration(self.state, decl)
def _parse_using_typealias(
self, id_tok: LexToken, template: typing.Optional[TemplateDecl]
self,
id_tok: LexToken,
template: typing.Optional[TemplateDecl],
doxygen: typing.Optional[str],
) -> None:
"""
alias_declaration: "using" IDENTIFIER "=" type_id ";"
@@ -1023,7 +1029,7 @@ class CxxParser:
dtype = self._parse_cv_ptr(parsed_type)
alias = UsingAlias(id_tok.value, dtype, template, self._current_access)
alias = UsingAlias(id_tok.value, dtype, template, self._current_access, doxygen)
self.visitor.on_using_alias(self.state, alias)
@@ -1052,9 +1058,9 @@ class CxxParser:
raise CxxParseError(
"unexpected using-declaration when parsing alias-declaration", tok
)
self._parse_using_declaration(tok)
self._parse_using_declaration(tok, doxygen)
else:
self._parse_using_typealias(tok, template)
self._parse_using_typealias(tok, template, doxygen)
# All using things end with a semicolon
self._next_token_must_be(";")
@@ -1863,10 +1869,9 @@ class CxxParser:
_auto_return_typename = PQName([AutoSpecifier()])
def _parse_trailing_return_type(
self, fn: typing.Union[Function, FunctionType]
) -> None:
self, return_type: typing.Optional[DecoratedType]
) -> DecoratedType:
# entry is "->"
return_type = fn.return_type
if not (
isinstance(return_type, Type)
and not return_type.const
@@ -1885,8 +1890,7 @@ class CxxParser:
dtype = self._parse_cv_ptr(parsed_type)
fn.has_trailing_return = True
fn.return_type = dtype
return dtype
def _parse_fn_end(self, fn: Function) -> None:
"""
@@ -1907,18 +1911,19 @@ class CxxParser:
else:
rtok = self.lex.token_if("requires")
if rtok:
fn_template = fn.template
if fn_template is None:
# requires on a function must always be accompanied by a template
if fn.template is None:
raise self._parse_error(rtok)
elif isinstance(fn_template, list):
fn_template = fn_template[0]
fn_template.raw_requires_post = self._parse_requires(rtok)
fn.raw_requires = self._parse_requires(rtok)
if self.lex.token_if("ARROW"):
return_type = self._parse_trailing_return_type(fn.return_type)
fn.has_trailing_return = True
fn.return_type = return_type
if self.lex.token_if("{"):
self._discard_contents("{", "}")
fn.has_body = True
elif self.lex.token_if("ARROW"):
self._parse_trailing_return_type(fn)
def _parse_method_end(self, method: Method) -> None:
"""
@@ -1962,7 +1967,12 @@ class CxxParser:
elif tok_value in ("&", "&&"):
method.ref_qualifier = tok_value
elif tok_value == "->":
self._parse_trailing_return_type(method)
return_type = self._parse_trailing_return_type(method.return_type)
method.has_trailing_return = True
method.return_type = return_type
if self.lex.token_if("{"):
self._discard_contents("{", "}")
method.has_body = True
break
elif tok_value == "throw":
tok = self._next_token_must_be("(")
@@ -1974,12 +1984,7 @@ class CxxParser:
toks = self._consume_balanced_tokens(otok)[1:-1]
method.noexcept = self._create_value(toks)
elif tok_value == "requires":
method_template = method.template
if method_template is None:
raise self._parse_error(tok)
elif isinstance(method_template, list):
method_template = method_template[0]
method_template.raw_requires_post = self._parse_requires(tok)
method.raw_requires = self._parse_requires(tok)
else:
self.lex.return_token(tok)
break
@@ -1998,6 +2003,7 @@ class CxxParser:
is_friend: bool,
is_typedef: bool,
msvc_convention: typing.Optional[LexToken],
is_guide: bool = False,
) -> bool:
"""
Assumes the caller has already consumed the return type and name, this consumes the
@@ -2074,7 +2080,21 @@ class CxxParser:
self.visitor.on_method_impl(state, method)
return method.has_body or method.has_trailing_return
elif is_guide:
assert isinstance(state, (ExternBlockState, NamespaceBlockState))
if not self.lex.token_if("ARROW"):
raise self._parse_error(None, expected="Trailing return type")
return_type = self._parse_trailing_return_type(
Type(PQName([AutoSpecifier()]))
)
guide = DeductionGuide(
return_type,
name=pqname,
parameters=params,
doxygen=doxygen,
)
self.visitor.on_deduction_guide(state, guide)
return False
else:
assert return_type is not None
fn = Function(
@@ -2208,7 +2228,9 @@ class CxxParser:
assert not isinstance(dtype, FunctionType)
dtype = dtype_fn = FunctionType(dtype, fn_params, vararg)
if self.lex.token_if("ARROW"):
self._parse_trailing_return_type(dtype_fn)
return_type = self._parse_trailing_return_type(dtype_fn.return_type)
dtype_fn.has_trailing_return = True
dtype_fn.return_type = return_type
else:
msvc_convention = None
@@ -2389,6 +2411,7 @@ class CxxParser:
destructor = False
op = None
msvc_convention = None
is_guide = False
# If we have a leading (, that's either an obnoxious grouping
# paren or it's a constructor
@@ -2439,8 +2462,15 @@ class CxxParser:
# grouping paren like "void (name(int x));"
toks = self._consume_balanced_tokens(tok)
# .. not sure what it's grouping, so put it back?
self.lex.return_tokens(toks[1:-1])
# check to see if the next token is an arrow, and thus a trailing return
if self.lex.token_peek_if("ARROW"):
self.lex.return_tokens(toks)
# the leading name of the class/ctor has been parsed as a type before the parens
pqname = parsed_type.typename
is_guide = True
else:
# .. not sure what it's grouping, so put it back?
self.lex.return_tokens(toks[1:-1])
if dtype:
msvc_convention = self.lex.token_if_val(*self._msvc_conventions)
@@ -2471,6 +2501,7 @@ class CxxParser:
is_friend,
is_typedef,
msvc_convention,
is_guide,
)
elif msvc_convention:
raise self._parse_error(msvc_convention)

View File

@@ -35,6 +35,7 @@ from dataclasses import dataclass, field
from .types import (
ClassDecl,
Concept,
DeductionGuide,
EnumDecl,
Field,
ForwardDecl,
@@ -123,6 +124,9 @@ class NamespaceScope:
#: Child namespaces
namespaces: typing.Dict[str, "NamespaceScope"] = field(default_factory=dict)
#: Deduction guides
deduction_guides: typing.List[DeductionGuide] = field(default_factory=list)
Block = typing.Union[ClassScope, NamespaceScope]
@@ -317,6 +321,11 @@ class SimpleCxxVisitor:
def on_class_end(self, state: SClassBlockState) -> None:
pass
def on_deduction_guide(
self, state: SNonClassBlockState, guide: DeductionGuide
) -> None:
state.user_data.deduction_guides.append(guide)
def parse_string(
content: str,

View File

@@ -526,9 +526,6 @@ class TemplateDecl:
#: template <typename T> requires ...
raw_requires_pre: typing.Optional[Value] = None
#: template <typename T> int main() requires ...
raw_requires_post: typing.Optional[Value] = None
#: If no template, this is None. This is a TemplateDecl if this there is a single
#: declaration:
@@ -730,6 +727,13 @@ class Function:
#: is the string "conversion" and the full Type is found in return_type
operator: typing.Optional[str] = None
#: A requires constraint following the function declaration. If you need the
#: prior, look at TemplateDecl.raw_requires_pre. At the moment this is just
#: a raw value, if we interpret it in the future this will change.
#:
#: template <typename T> int main() requires ...
raw_requires: typing.Optional[Value] = None
@dataclass
class Method(Function):
@@ -848,6 +852,7 @@ class Field:
constexpr: bool = False
mutable: bool = False
static: bool = False
inline: bool = False
doxygen: typing.Optional[str] = None
@@ -865,6 +870,9 @@ class UsingDecl:
#: If within a class, the access level for this decl
access: typing.Optional[str] = None
#: Documentation if present
doxygen: typing.Optional[str] = None
@dataclass
class UsingAlias:
@@ -885,3 +893,24 @@ class UsingAlias:
#: If within a class, the access level for this decl
access: typing.Optional[str] = None
#: Documentation if present
doxygen: typing.Optional[str] = None
@dataclass
class DeductionGuide:
"""
.. code-block:: c++
template <class T>
MyClass(T) -> MyClass(int);
"""
#: Only constructors and destructors don't have a return type
result_type: typing.Optional[DecoratedType]
name: PQName
parameters: typing.List[Parameter]
doxygen: typing.Optional[str] = None

View File

@@ -9,6 +9,7 @@ else:
from .types import (
Concept,
DeductionGuide,
EnumDecl,
Field,
ForwardDecl,
@@ -236,6 +237,13 @@ class CxxVisitor(Protocol):
``on_variable`` for each instance declared.
"""
def on_deduction_guide(
self, state: NonClassBlockState, guide: DeductionGuide
) -> None:
"""
Called when a deduction guide is encountered
"""
class NullVisitor:
"""
@@ -318,5 +326,10 @@ class NullVisitor:
def on_class_end(self, state: ClassBlockState) -> None:
return None
def on_deduction_guide(
self, state: NonClassBlockState, guide: DeductionGuide
) -> None:
return None
null_visitor = NullVisitor()

View File

@@ -3336,3 +3336,40 @@ def test_constructor_outside_class() -> None:
]
)
)
def test_class_inline_static() -> None:
content = """
struct X {
inline static bool Foo = 1;
};
"""
data = parse_string(content, cleandoc=True)
assert data == ParsedData(
namespace=NamespaceScope(
classes=[
ClassScope(
class_decl=ClassDecl(
typename=PQName(
segments=[NameSpecifier(name="X")], classkey="struct"
)
),
fields=[
Field(
access="public",
type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="bool")]
)
),
name="Foo",
value=Value(tokens=[Token(value="1")]),
static=True,
inline=True,
)
],
)
]
)
)

View File

@@ -6,6 +6,7 @@ from cxxheaderparser.types import (
Concept,
Function,
FundamentalSpecifier,
Method,
MoveReference,
NameSpecifier,
PQName,
@@ -495,15 +496,15 @@ def test_requires_last_elem() -> None:
)
],
template=TemplateDecl(
params=[TemplateTypeParam(typekey="typename", name="T")],
raw_requires_post=Value(
tokens=[
Token(value="Eq"),
Token(value="<"),
Token(value="T"),
Token(value=">"),
]
),
params=[TemplateTypeParam(typekey="typename", name="T")]
),
raw_requires=Value(
tokens=[
Token(value="Eq"),
Token(value="<"),
Token(value="T"),
Token(value=">"),
]
),
)
]
@@ -752,14 +753,14 @@ def test_requires_both() -> None:
Token(value=">"),
]
),
raw_requires_post=Value(
tokens=[
Token(value="Subtractable"),
Token(value="<"),
Token(value="T"),
Token(value=">"),
]
),
),
raw_requires=Value(
tokens=[
Token(value="Subtractable"),
Token(value="<"),
Token(value="T"),
Token(value=">"),
]
),
)
]
@@ -791,21 +792,87 @@ def test_requires_paren() -> None:
)
],
template=TemplateDecl(
params=[TemplateTypeParam(typekey="class", name="T")],
raw_requires_post=Value(
tokens=[
Token(value="("),
Token(value="is_purrable"),
Token(value="<"),
Token(value="T"),
Token(value=">"),
Token(value="("),
Token(value=")"),
Token(value=")"),
]
),
params=[TemplateTypeParam(typekey="class", name="T")]
),
raw_requires=Value(
tokens=[
Token(value="("),
Token(value="is_purrable"),
Token(value="<"),
Token(value="T"),
Token(value=">"),
Token(value="("),
Token(value=")"),
Token(value=")"),
]
),
)
]
)
)
def test_non_template_requires() -> None:
content = """
// clang-format off
template <class T>
struct Payload
{
constexpr Payload(T v)
requires(std::is_pod_v<T>)
: Value(v)
{
}
};
"""
data = parse_string(content, cleandoc=True)
assert data == ParsedData(
namespace=NamespaceScope(
classes=[
ClassScope(
class_decl=ClassDecl(
typename=PQName(
segments=[NameSpecifier(name="Payload")], classkey="struct"
),
template=TemplateDecl(
params=[TemplateTypeParam(typekey="class", name="T")]
),
),
methods=[
Method(
return_type=None,
name=PQName(segments=[NameSpecifier(name="Payload")]),
parameters=[
Parameter(
type=Type(
typename=PQName(
segments=[NameSpecifier(name="T")]
)
),
name="v",
)
],
constexpr=True,
has_body=True,
raw_requires=Value(
tokens=[
Token(value="("),
Token(value="std"),
Token(value="::"),
Token(value="is_pod_v"),
Token(value="<"),
Token(value="T"),
Token(value=">"),
Token(value=")"),
]
),
access="public",
constructor=True,
)
],
)
]
)
)

View File

@@ -26,6 +26,7 @@ from cxxheaderparser.types import (
Type,
Typedef,
UsingDecl,
UsingAlias,
Value,
Variable,
)
@@ -436,3 +437,53 @@ def test_doxygen_attribute() -> None:
]
)
)
def test_doxygen_using_decl() -> None:
content = """
// clang-format off
/// Comment
using ns::ClassName;
"""
data = parse_string(content, cleandoc=True)
assert data == ParsedData(
namespace=NamespaceScope(
using=[
UsingDecl(
typename=PQName(
segments=[
NameSpecifier(name="ns"),
NameSpecifier(name="ClassName"),
]
),
doxygen="/// Comment",
)
]
)
)
def test_doxygen_using_alias() -> None:
content = """
// clang-format off
/// Comment
using alias = sometype;
"""
data = parse_string(content, cleandoc=True)
assert data == ParsedData(
namespace=NamespaceScope(
using_alias=[
UsingAlias(
alias="alias",
type=Type(
typename=PQName(segments=[NameSpecifier(name="sometype")])
),
doxygen="/// Comment",
)
]
)
)

View File

@@ -1194,3 +1194,67 @@ def test_auto_decltype_return() -> None:
]
)
)
def test_fn_trailing_return_with_body() -> None:
content = """
auto test() -> void
{
}
"""
data = parse_string(content, cleandoc=True)
assert data == ParsedData(
namespace=NamespaceScope(
functions=[
Function(
return_type=Type(
typename=PQName(segments=[FundamentalSpecifier(name="void")])
),
name=PQName(segments=[NameSpecifier(name="test")]),
parameters=[],
has_body=True,
has_trailing_return=True,
)
]
)
)
def test_method_trailing_return_with_body() -> None:
content = """
struct X {
auto test() -> void
{
}
};
"""
data = parse_string(content, cleandoc=True)
assert data == ParsedData(
namespace=NamespaceScope(
classes=[
ClassScope(
class_decl=ClassDecl(
typename=PQName(
segments=[NameSpecifier(name="X")], classkey="struct"
)
),
methods=[
Method(
return_type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="void")]
)
),
name=PQName(segments=[NameSpecifier(name="test")]),
parameters=[],
has_body=True,
has_trailing_return=True,
access="public",
)
],
)
]
)
)

View File

@@ -5,6 +5,7 @@ from cxxheaderparser.types import (
BaseClass,
ClassDecl,
DecltypeSpecifier,
DeductionGuide,
Field,
ForwardDecl,
Function,
@@ -2163,3 +2164,86 @@ def test_member_class_template_specialization() -> None:
]
)
)
def test_template_deduction_guide() -> None:
content = """
template <class CharT, class Traits = std::char_traits<CharT>>
Error(std::basic_string_view<CharT, Traits>) -> Error<std::string>;
"""
data = parse_string(content, cleandoc=True)
assert data == ParsedData(
namespace=NamespaceScope(
deduction_guides=[
DeductionGuide(
result_type=Type(
typename=PQName(
segments=[
NameSpecifier(
name="Error",
specialization=TemplateSpecialization(
args=[
TemplateArgument(
arg=Type(
typename=PQName(
segments=[
NameSpecifier(name="std"),
NameSpecifier(
name="string"
),
]
)
)
)
]
),
)
]
)
),
name=PQName(segments=[NameSpecifier(name="Error")]),
parameters=[
Parameter(
type=Type(
typename=PQName(
segments=[
NameSpecifier(name="std"),
NameSpecifier(
name="basic_string_view",
specialization=TemplateSpecialization(
args=[
TemplateArgument(
arg=Type(
typename=PQName(
segments=[
NameSpecifier(
name="CharT"
)
]
)
)
),
TemplateArgument(
arg=Type(
typename=PQName(
segments=[
NameSpecifier(
name="Traits"
)
]
)
)
),
]
),
),
]
)
)
)
],
)
]
)
)