Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
a3b3c43a76 | |||
98b68265cc |
@ -86,6 +86,8 @@ class PlyLexer:
|
|||||||
"concept",
|
"concept",
|
||||||
"const",
|
"const",
|
||||||
"constexpr",
|
"constexpr",
|
||||||
|
"consteval",
|
||||||
|
"constinit",
|
||||||
"const_cast",
|
"const_cast",
|
||||||
"continue",
|
"continue",
|
||||||
"decltype",
|
"decltype",
|
||||||
|
@ -2148,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:
|
||||||
@ -2318,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"}
|
||||||
|
@ -374,9 +374,9 @@ class PointerToMember:
|
|||||||
v = " volatile" if self.volatile else ""
|
v = " volatile" if self.volatile else ""
|
||||||
ptr_to = self.ptr_to
|
ptr_to = self.ptr_to
|
||||||
if isinstance(ptr_to, (Array, FunctionType)):
|
if isinstance(ptr_to, (Array, FunctionType)):
|
||||||
return ptr_to.format_decl(f"({base_type.format()}::*{c}{v})")
|
return ptr_to.format_decl(f"({self.base_type.format()}::*{c}{v})")
|
||||||
else:
|
else:
|
||||||
return f"{ptr_to.format()} {base_type.format()}::*{c}{v}"
|
return f"{ptr_to.format()} {self.base_type.format()}::*{c}{v}"
|
||||||
|
|
||||||
def format_decl(self, name: str):
|
def format_decl(self, name: str):
|
||||||
"""Format as a named declaration"""
|
"""Format as a named declaration"""
|
||||||
@ -384,9 +384,9 @@ class PointerToMember:
|
|||||||
v = " volatile" if self.volatile else ""
|
v = " volatile" if self.volatile else ""
|
||||||
ptr_to = self.ptr_to
|
ptr_to = self.ptr_to
|
||||||
if isinstance(ptr_to, (Array, FunctionType)):
|
if isinstance(ptr_to, (Array, FunctionType)):
|
||||||
return ptr_to.format_decl(f"({base_type.format()}::*{c}{v} {name})")
|
return ptr_to.format_decl(f"({self.base_type.format()}::*{c}{v} {name})")
|
||||||
else:
|
else:
|
||||||
return f"{ptr_to.format()} {base_type.format()}::*{c}{v} {name}"
|
return f"{ptr_to.format()} {self.base_type.format()}::*{c}{v} {name}"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -721,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
|
||||||
@ -857,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
|
||||||
@ -883,6 +885,7 @@ 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
|
inline: bool = False
|
||||||
|
104
tests/test_constinit_consteval.py
Normal file
104
tests/test_constinit_consteval.py
Normal 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",
|
||||||
|
)
|
||||||
|
]
|
||||||
|
),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user