Compare commits

..

14 Commits
1.2.0 ... main

Author SHA1 Message Date
a3b3c43a76 Added very basic support for constinit and consteval. 2024-01-05 14:49:26 +01:00
98b68265cc Fixed PointerToMember format functions. 2024-01-05 14:37:46 +01:00
efa7f5eaad Fixed operator*(). 2024-01-05 14:36:06 +01:00
83a0fb805d Added basic parsing of pointer-to-member types. 2024-01-05 14:24:47 +01:00
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
12 changed files with 846 additions and 87 deletions

View File

@ -86,6 +86,8 @@ class PlyLexer:
"concept", "concept",
"const", "const",
"constexpr", "constexpr",
"consteval",
"constinit",
"const_cast", "const_cast",
"continue", "continue",
"decltype", "decltype",

View File

@ -25,6 +25,7 @@ from .types import (
Concept, Concept,
DecltypeSpecifier, DecltypeSpecifier,
DecoratedType, DecoratedType,
DeductionGuide,
EnumDecl, EnumDecl,
Enumerator, Enumerator,
Field, Field,
@ -42,6 +43,7 @@ from .types import (
Parameter, Parameter,
PQName, PQName,
Pointer, Pointer,
PointerToMember,
Reference, Reference,
TemplateArgument, TemplateArgument,
TemplateDecl, TemplateDecl,
@ -992,7 +994,9 @@ class CxxParser:
self.visitor.on_using_namespace(state, names) 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_declaration: "using" ["typename"] ["::"] nested_name_specifier unqualified_id ";"
| "using" "::" unqualified_id ";" | "using" "::" unqualified_id ";"
@ -1004,12 +1008,15 @@ class CxxParser:
typename, _ = self._parse_pqname( typename, _ = self._parse_pqname(
tok, fn_ok=True, compound_ok=True, fund_ok=True 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) self.visitor.on_using_declaration(self.state, decl)
def _parse_using_typealias( 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: ) -> None:
""" """
alias_declaration: "using" IDENTIFIER "=" type_id ";" alias_declaration: "using" IDENTIFIER "=" type_id ";"
@ -1023,7 +1030,7 @@ class CxxParser:
dtype = self._parse_cv_ptr(parsed_type) 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) self.visitor.on_using_alias(self.state, alias)
@ -1052,9 +1059,9 @@ class CxxParser:
raise CxxParseError( raise CxxParseError(
"unexpected using-declaration when parsing alias-declaration", tok "unexpected using-declaration when parsing alias-declaration", tok
) )
self._parse_using_declaration(tok) self._parse_using_declaration(tok, doxygen)
else: else:
self._parse_using_typealias(tok, template) self._parse_using_typealias(tok, template, doxygen)
# All using things end with a semicolon # All using things end with a semicolon
self._next_token_must_be(";") self._next_token_must_be(";")
@ -1592,6 +1599,7 @@ class CxxParser:
fn_ok: bool = False, fn_ok: bool = False,
compound_ok: bool = False, compound_ok: bool = False,
fund_ok: bool = False, fund_ok: bool = False,
ptr_to_member_ok: bool = False,
) -> typing.Tuple[PQName, typing.Optional[str]]: ) -> typing.Tuple[PQName, typing.Optional[str]]:
""" """
Parses a possibly qualified function name or a type name, returns when Parses a possibly qualified function name or a type name, returns when
@ -1719,7 +1727,12 @@ class CxxParser:
if not self.lex.token_if("DBL_COLON"): if not self.lex.token_if("DBL_COLON"):
break break
tok = self._next_token_must_be("NAME", "operator", "template", "decltype") tok = self._next_token_must_be("NAME", "operator", "template", "decltype", "*")
if tok.value == '*':
if not ptr_to_member_ok:
raise self._parse_error(tok)
return name, 'PTR_TO_MEMBER'
pqname = PQName(segments, classkey, has_typename) pqname = PQName(segments, classkey, has_typename)
@ -1786,10 +1799,31 @@ class CxxParser:
toks = self._consume_balanced_tokens(tok) toks = self._consume_balanced_tokens(tok)
self.lex.return_tokens(toks[1:-1]) self.lex.return_tokens(toks[1:-1])
# optional name # optional name
tok = self.lex.token_if("NAME", "final") tok = self.lex.token_if("NAME", "final", "DBL_COLON")
if tok: if tok:
param_name = tok.value pqname, op = self._parse_pqname(tok, fn_ok=True, ptr_to_member_ok=True)
while op == 'PTR_TO_MEMBER':
dtype = PointerToMember(base_type=Type(typename=pqname), ptr_to=dtype, const=dtype.const, volatile=dtype.volatile)
# dtype = self._parse_cv_ptr(dtype)
tok = self.lex.token_if("NAME", "final", "DBL_COLON")
if tok:
pqname, op = self._parse_pqname(tok, fn_ok=True, ptr_to_member_ok=True)
else:
pqname = None
op = None
if pqname:
if len(pqname.segments) != 1:
raise self._parse_error(None)
param_name = pqname.segments[0].name
if self.lex.token_if("("):
if isinstance(dtype, PointerToMember):
params, vararg, at_params = self._parse_parameters(False)
dtype.ptr_to = FunctionType(return_type=dtype.ptr_to, parameters=params, vararg=vararg)
else:
assert(False) # TODO
# optional array parameter # optional array parameter
tok = self.lex.token_if("[") tok = self.lex.token_if("[")
@ -1863,10 +1897,9 @@ class CxxParser:
_auto_return_typename = PQName([AutoSpecifier()]) _auto_return_typename = PQName([AutoSpecifier()])
def _parse_trailing_return_type( def _parse_trailing_return_type(
self, fn: typing.Union[Function, FunctionType] self, return_type: typing.Optional[DecoratedType]
) -> None: ) -> DecoratedType:
# entry is "->" # entry is "->"
return_type = fn.return_type
if not ( if not (
isinstance(return_type, Type) isinstance(return_type, Type)
and not return_type.const and not return_type.const
@ -1885,8 +1918,7 @@ class CxxParser:
dtype = self._parse_cv_ptr(parsed_type) dtype = self._parse_cv_ptr(parsed_type)
fn.has_trailing_return = True return dtype
fn.return_type = dtype
def _parse_fn_end(self, fn: Function) -> None: def _parse_fn_end(self, fn: Function) -> None:
""" """
@ -1907,18 +1939,19 @@ class CxxParser:
else: else:
rtok = self.lex.token_if("requires") rtok = self.lex.token_if("requires")
if rtok: if rtok:
fn_template = fn.template # requires on a function must always be accompanied by a template
if fn_template is None: if fn.template is None:
raise self._parse_error(rtok) raise self._parse_error(rtok)
elif isinstance(fn_template, list): fn.raw_requires = self._parse_requires(rtok)
fn_template = fn_template[0]
fn_template.raw_requires_post = 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("{"): if self.lex.token_if("{"):
self._discard_contents("{", "}") self._discard_contents("{", "}")
fn.has_body = True fn.has_body = True
elif self.lex.token_if("ARROW"):
self._parse_trailing_return_type(fn)
def _parse_method_end(self, method: Method) -> None: def _parse_method_end(self, method: Method) -> None:
""" """
@ -1962,7 +1995,12 @@ class CxxParser:
elif tok_value in ("&", "&&"): elif tok_value in ("&", "&&"):
method.ref_qualifier = tok_value method.ref_qualifier = tok_value
elif 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 break
elif tok_value == "throw": elif tok_value == "throw":
tok = self._next_token_must_be("(") tok = self._next_token_must_be("(")
@ -1974,12 +2012,7 @@ class CxxParser:
toks = self._consume_balanced_tokens(otok)[1:-1] toks = self._consume_balanced_tokens(otok)[1:-1]
method.noexcept = self._create_value(toks) method.noexcept = self._create_value(toks)
elif tok_value == "requires": elif tok_value == "requires":
method_template = method.template method.raw_requires = self._parse_requires(tok)
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)
else: else:
self.lex.return_token(tok) self.lex.return_token(tok)
break break
@ -1998,6 +2031,7 @@ class CxxParser:
is_friend: bool, is_friend: bool,
is_typedef: bool, is_typedef: bool,
msvc_convention: typing.Optional[LexToken], msvc_convention: typing.Optional[LexToken],
is_guide: bool = False,
) -> bool: ) -> bool:
""" """
Assumes the caller has already consumed the return type and name, this consumes the Assumes the caller has already consumed the return type and name, this consumes the
@ -2074,7 +2108,21 @@ class CxxParser:
self.visitor.on_method_impl(state, method) self.visitor.on_method_impl(state, method)
return method.has_body or method.has_trailing_return 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: else:
assert return_type is not None assert return_type is not None
fn = Function( fn = Function(
@ -2100,6 +2148,8 @@ class CxxParser:
if fn.constexpr: if fn.constexpr:
raise CxxParseError("typedef function may not be constexpr") raise CxxParseError("typedef function may not be constexpr")
if fn.consteval:
raise CxxParseError("typedef function may not be consteval")
if fn.extern: if fn.extern:
raise CxxParseError("typedef function may not be extern") raise CxxParseError("typedef function may not be extern")
if fn.static: if fn.static:
@ -2208,7 +2258,9 @@ class CxxParser:
assert not isinstance(dtype, FunctionType) assert not isinstance(dtype, FunctionType)
dtype = dtype_fn = FunctionType(dtype, fn_params, vararg) dtype = dtype_fn = FunctionType(dtype, fn_params, vararg)
if self.lex.token_if("ARROW"): 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: else:
msvc_convention = None msvc_convention = None
@ -2268,7 +2320,7 @@ class CxxParser:
return dtype return dtype
# Applies to variables and return values # Applies to variables and return values
_type_kwd_both = {"const", "constexpr", "extern", "inline", "static"} _type_kwd_both = {"const", "consteval", "constexpr", "constinit", "extern", "inline", "static"}
# Only found on methods # Only found on methods
_type_kwd_meth = {"explicit", "virtual"} _type_kwd_meth = {"explicit", "virtual"}
@ -2389,6 +2441,7 @@ class CxxParser:
destructor = False destructor = False
op = None op = None
msvc_convention = None msvc_convention = None
is_guide = False
# If we have a leading (, that's either an obnoxious grouping # If we have a leading (, that's either an obnoxious grouping
# paren or it's a constructor # paren or it's a constructor
@ -2439,6 +2492,13 @@ class CxxParser:
# grouping paren like "void (name(int x));" # grouping paren like "void (name(int x));"
toks = self._consume_balanced_tokens(tok) toks = self._consume_balanced_tokens(tok)
# 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? # .. not sure what it's grouping, so put it back?
self.lex.return_tokens(toks[1:-1]) self.lex.return_tokens(toks[1:-1])
@ -2447,7 +2507,17 @@ class CxxParser:
tok = self.lex.token_if_in_set(self._pqname_start_tokens) tok = self.lex.token_if_in_set(self._pqname_start_tokens)
if tok: if tok:
pqname, op = self._parse_pqname(tok, fn_ok=True) pqname, op = self._parse_pqname(tok, fn_ok=True, ptr_to_member_ok=True)
while op == 'PTR_TO_MEMBER':
dtype = PointerToMember(base_type=Type(typename=pqname), ptr_to=dtype, const=dtype.const, volatile=dtype.volatile)
# dtype = self._parse_cv_ptr(dtype)
tok = self.lex.token_if_in_set(self._pqname_start_tokens)
if tok:
pqname, op = self._parse_pqname(tok, fn_ok=True, ptr_to_member_ok=True)
else:
pqname = None
op = None
# TODO: "type fn(x);" is ambiguous here. Because this is a header # TODO: "type fn(x);" is ambiguous here. Because this is a header
# parser, we assume it's a function, not a variable declaration # parser, we assume it's a function, not a variable declaration
@ -2458,6 +2528,10 @@ class CxxParser:
if not pqname: if not pqname:
raise self._parse_error(None) raise self._parse_error(None)
if isinstance(dtype, PointerToMember):
params, vararg, at_params = self._parse_parameters(False)
dtype.ptr_to = FunctionType(return_type=dtype.ptr_to, parameters=params, vararg=vararg)
else:
return self._parse_function( return self._parse_function(
mods, mods,
dtype, dtype,
@ -2471,6 +2545,7 @@ class CxxParser:
is_friend, is_friend,
is_typedef, is_typedef,
msvc_convention, msvc_convention,
is_guide,
) )
elif msvc_convention: elif msvc_convention:
raise self._parse_error(msvc_convention) raise self._parse_error(msvc_convention)

View File

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

View File

@ -306,7 +306,7 @@ class Array:
""" """
#: The type that this is an array of #: The type that this is an array of
array_of: typing.Union["Array", "Pointer", Type] array_of: typing.Union["Array", "Pointer", "PointerToMember", Type]
#: Size of the array #: Size of the array
#: #:
@ -332,7 +332,7 @@ class Pointer:
""" """
#: Thing that this points to #: Thing that this points to
ptr_to: typing.Union[Array, FunctionType, "Pointer", Type] ptr_to: typing.Union[Array, FunctionType, "Pointer", "PointerToMember", Type]
const: bool = False const: bool = False
volatile: bool = False volatile: bool = False
@ -356,6 +356,39 @@ class Pointer:
else: else:
return f"{ptr_to.format()}*{c}{v} {name}" return f"{ptr_to.format()}*{c}{v} {name}"
@dataclass
class PointerToMember:
"""
Pointer to a class member. (``Class::* int``)
"""
#: Thing that this points to
base_type: Type
ptr_to: typing.Union[Array, FunctionType, "Pointer", "PointerToMember", Type]
const: bool = False
volatile: bool = False
def format(self) -> str:
c = " const" if self.const else ""
v = " volatile" if self.volatile else ""
ptr_to = self.ptr_to
if isinstance(ptr_to, (Array, FunctionType)):
return ptr_to.format_decl(f"({self.base_type.format()}::*{c}{v})")
else:
return f"{ptr_to.format()} {self.base_type.format()}::*{c}{v}"
def format_decl(self, name: str):
"""Format as a named declaration"""
c = " const" if self.const else ""
v = " volatile" if self.volatile else ""
ptr_to = self.ptr_to
if isinstance(ptr_to, (Array, FunctionType)):
return ptr_to.format_decl(f"({self.base_type.format()}::*{c}{v} {name})")
else:
return f"{ptr_to.format()} {self.base_type.format()}::*{c}{v} {name}"
@dataclass @dataclass
class Reference: class Reference:
@ -363,7 +396,7 @@ class Reference:
A lvalue (``&``) reference A lvalue (``&``) reference
""" """
ref_to: typing.Union[Array, FunctionType, Pointer, Type] ref_to: typing.Union[Array, FunctionType, Pointer, PointerToMember, Type]
def format(self) -> str: def format(self) -> str:
ref_to = self.ref_to ref_to = self.ref_to
@ -388,7 +421,7 @@ class MoveReference:
An rvalue (``&&``) reference An rvalue (``&&``) reference
""" """
moveref_to: typing.Union[Array, FunctionType, Pointer, Type] moveref_to: typing.Union[Array, FunctionType, Pointer, PointerToMember, Type]
def format(self) -> str: def format(self) -> str:
return f"{self.moveref_to.format()}&&" return f"{self.moveref_to.format()}&&"
@ -402,7 +435,7 @@ class MoveReference:
#: #:
#: .. note:: There can only be one of FunctionType or Type in a DecoratedType #: .. note:: There can only be one of FunctionType or Type in a DecoratedType
#: chain #: chain
DecoratedType = typing.Union[Array, Pointer, MoveReference, Reference, Type] DecoratedType = typing.Union[Array, Pointer, PointerToMember, MoveReference, Reference, Type]
@dataclass @dataclass
@ -526,9 +559,6 @@ class TemplateDecl:
#: template <typename T> requires ... #: template <typename T> requires ...
raw_requires_pre: typing.Optional[Value] = None 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 #: If no template, this is None. This is a TemplateDecl if this there is a single
#: declaration: #: declaration:
@ -691,6 +721,7 @@ class Function:
doxygen: typing.Optional[str] = None doxygen: typing.Optional[str] = None
constexpr: bool = False constexpr: bool = False
consteval: bool = False
extern: typing.Union[bool, str] = False extern: typing.Union[bool, str] = False
static: bool = False static: bool = False
inline: bool = False inline: bool = False
@ -730,6 +761,13 @@ class Function:
#: is the string "conversion" and the full Type is found in return_type #: is the string "conversion" and the full Type is found in return_type
operator: typing.Optional[str] = None 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 @dataclass
class Method(Function): class Method(Function):
@ -820,6 +858,7 @@ class Variable:
value: typing.Optional[Value] = None value: typing.Optional[Value] = None
constexpr: bool = False constexpr: bool = False
constinit: bool = False
extern: typing.Union[bool, str] = False extern: typing.Union[bool, str] = False
static: bool = False static: bool = False
inline: bool = False inline: bool = False
@ -846,8 +885,10 @@ class Field:
bits: typing.Optional[int] = None bits: typing.Optional[int] = None
constexpr: bool = False constexpr: bool = False
constinit: bool = False
mutable: bool = False mutable: bool = False
static: bool = False static: bool = False
inline: bool = False
doxygen: typing.Optional[str] = None doxygen: typing.Optional[str] = None
@ -865,6 +906,9 @@ class UsingDecl:
#: If within a class, the access level for this decl #: If within a class, the access level for this decl
access: typing.Optional[str] = None access: typing.Optional[str] = None
#: Documentation if present
doxygen: typing.Optional[str] = None
@dataclass @dataclass
class UsingAlias: class UsingAlias:
@ -885,3 +929,24 @@ class UsingAlias:
#: If within a class, the access level for this decl #: If within a class, the access level for this decl
access: typing.Optional[str] = None 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 ( from .types import (
Concept, Concept,
DeductionGuide,
EnumDecl, EnumDecl,
Field, Field,
ForwardDecl, ForwardDecl,
@ -236,6 +237,13 @@ class CxxVisitor(Protocol):
``on_variable`` for each instance declared. ``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: class NullVisitor:
""" """
@ -318,5 +326,10 @@ class NullVisitor:
def on_class_end(self, state: ClassBlockState) -> None: def on_class_end(self, state: ClassBlockState) -> None:
return None return None
def on_deduction_guide(
self, state: NonClassBlockState, guide: DeductionGuide
) -> None:
return None
null_visitor = NullVisitor() 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, Concept,
Function, Function,
FundamentalSpecifier, FundamentalSpecifier,
Method,
MoveReference, MoveReference,
NameSpecifier, NameSpecifier,
PQName, PQName,
@ -495,8 +496,9 @@ def test_requires_last_elem() -> None:
) )
], ],
template=TemplateDecl( template=TemplateDecl(
params=[TemplateTypeParam(typekey="typename", name="T")], params=[TemplateTypeParam(typekey="typename", name="T")]
raw_requires_post=Value( ),
raw_requires=Value(
tokens=[ tokens=[
Token(value="Eq"), Token(value="Eq"),
Token(value="<"), Token(value="<"),
@ -504,7 +506,6 @@ def test_requires_last_elem() -> None:
Token(value=">"), Token(value=">"),
] ]
), ),
),
) )
] ]
) )
@ -752,7 +753,8 @@ def test_requires_both() -> None:
Token(value=">"), Token(value=">"),
] ]
), ),
raw_requires_post=Value( ),
raw_requires=Value(
tokens=[ tokens=[
Token(value="Subtractable"), Token(value="Subtractable"),
Token(value="<"), Token(value="<"),
@ -760,7 +762,6 @@ def test_requires_both() -> None:
Token(value=">"), Token(value=">"),
] ]
), ),
),
) )
] ]
) )
@ -791,8 +792,9 @@ def test_requires_paren() -> None:
) )
], ],
template=TemplateDecl( template=TemplateDecl(
params=[TemplateTypeParam(typekey="class", name="T")], params=[TemplateTypeParam(typekey="class", name="T")]
raw_requires_post=Value( ),
raw_requires=Value(
tokens=[ tokens=[
Token(value="("), Token(value="("),
Token(value="is_purrable"), Token(value="is_purrable"),
@ -804,7 +806,72 @@ def test_requires_paren() -> None:
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

@ -0,0 +1,104 @@
def test_constinit_consteval() -> None:
content = """
struct S
{
static constinit int i = 5;
static consteval int func(int i) { return i*i; }
};
template<std::size_t numBits>
consteval auto getUintType()
{
if constexpr (numBits == 8) {
return std::uint8_t{};
}
else if constexpr (numBits == 16) {
return std::uint16_t{};
}
else if constexpr (numBits == 32) {
return std::uint32_t{};
}
else if constexpr (numBits == 64) {
return std::uint64_t{};
}
}
"""
data = parse_string(content, cleandoc=True)
assert data == ParsedData(
namespace=NamespaceScope(
classes=[
ClassScope(
class_decl=ClassDecl(
typename=PQName(
segments=[NameSpecifier(name="S")], classkey="struct"
)
),
fields=[
Field(
access="public",
type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="int")]
)
),
name="i",
value=Value(tokens=[Token(value="5")]),
constinit=True,
static=True,
)
],
methods=[
Method(
return_type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="int")]
)
),
name=PQName(segments=[NameSpecifier(name="func")]),
parameters=[
Parameter(
type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="int")]
)
),
name="i",
)
],
consteval=True,
static=True,
has_body=True,
access="public",
)
],
)
],
functions=[
Function(
return_type=Type(typename=PQName(segments=[AutoSpecifier()])),
name=PQName(segments=[NameSpecifier(name="getUintType")]),
parameters=[],
consteval=True,
has_body=True,
template=TemplateDecl(
params=[
TemplateNonTypeParam(
type=Type(
typename=PQName(
segments=[
NameSpecifier(name="std"),
NameSpecifier(name="size_t"),
]
)
),
name="numBits",
)
]
),
)
],
)
)

View File

@ -26,6 +26,7 @@ from cxxheaderparser.types import (
Type, Type,
Typedef, Typedef,
UsingDecl, UsingDecl,
UsingAlias,
Value, Value,
Variable, 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

@ -0,0 +1,188 @@
def test_pointer_to_member() -> None:
content = """
class Class
{
};
int Class::* intPtr;
int (Class::* intReturnFuncPtr)();
void (Class::* intParamFuncPtr)(int);
void (Class::* varargFuncPtr)(...);
template<typename... TArgs>
int takesFunc(void (*func)(TArgs...));
template<typename TObject, typename... TArgs>
int takesMemberFunc(TObject& object, void (TObject::* func)(TArgs...));
"""
data = parse_string(content, cleandoc=True)
assert data == ParsedData(
namespace=NamespaceScope(
classes=[
ClassScope(
class_decl=ClassDecl(
typename=PQName(
segments=[NameSpecifier(name="Class")], classkey="class"
)
)
)
],
functions=[
Function(
return_type=Type(
typename=PQName(segments=[FundamentalSpecifier(name="int")])
),
name=PQName(segments=[NameSpecifier(name="takesFunc")]),
parameters=[
Parameter(
type=Pointer(
ptr_to=FunctionType(
return_type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="void")]
)
),
parameters=[
Parameter(
type=Type(
typename=PQName(
segments=[
NameSpecifier(name="TArgs")
]
)
),
param_pack=True,
)
],
)
),
name="func",
)
],
template=TemplateDecl(
params=[
TemplateTypeParam(
typekey="typename", name="TArgs", param_pack=True
)
]
),
),
Function(
return_type=Type(
typename=PQName(segments=[FundamentalSpecifier(name="int")])
),
name=PQName(segments=[NameSpecifier(name="takesMemberFunc")]),
parameters=[
Parameter(
type=Reference(
ref_to=Type(
typename=PQName(
segments=[NameSpecifier(name="TObject")]
)
)
),
name="object",
),
Parameter(
type=PointerToMember(
base_type=Type(typename=NameSpecifier(name="TObject")),
ptr_to=FunctionType(
return_type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="void")]
)
),
parameters=[
Parameter(
type=Type(
typename=PQName(
segments=[
NameSpecifier(name="TArgs")
]
)
),
param_pack=True,
)
],
),
),
name="func",
),
],
template=TemplateDecl(
params=[
TemplateTypeParam(typekey="typename", name="TObject"),
TemplateTypeParam(
typekey="typename", name="TArgs", param_pack=True
),
]
),
),
],
variables=[
Variable(
name=PQName(segments=[NameSpecifier(name="intPtr")]),
type=PointerToMember(
base_type=Type(typename=NameSpecifier(name="Class")),
ptr_to=Type(
typename=PQName(segments=[FundamentalSpecifier(name="int")])
),
),
),
Variable(
name=PQName(segments=[NameSpecifier(name="intReturnFuncPtr")]),
type=PointerToMember(
base_type=Type(typename=NameSpecifier(name="Class")),
ptr_to=FunctionType(
return_type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="int")]
)
),
parameters=[],
),
),
),
Variable(
name=PQName(segments=[NameSpecifier(name="intParamFuncPtr")]),
type=PointerToMember(
base_type=Type(typename=NameSpecifier(name="Class")),
ptr_to=FunctionType(
return_type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="void")]
)
),
parameters=[
Parameter(
type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="int")]
)
)
)
],
),
),
),
Variable(
name=PQName(segments=[NameSpecifier(name="varargFuncPtr")]),
type=PointerToMember(
base_type=Type(typename=NameSpecifier(name="Class")),
ptr_to=FunctionType(
return_type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="void")]
)
),
parameters=[],
vararg=True,
),
),
),
],
)
)

View File

@ -5,6 +5,7 @@ from cxxheaderparser.types import (
BaseClass, BaseClass,
ClassDecl, ClassDecl,
DecltypeSpecifier, DecltypeSpecifier,
DeductionGuide,
Field, Field,
ForwardDecl, ForwardDecl,
Function, 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"
)
]
)
)
),
]
),
),
]
)
)
)
],
)
]
)
)