cppparser: support arbitrary constant expression in bitfields

This commit is contained in:
rdb 2020-06-14 11:31:58 +02:00
parent 2880525c1e
commit 97e6a314b1
4 changed files with 15 additions and 5 deletions

View File

@ -1891,7 +1891,7 @@ instance_identifier_and_maybe_trailing_return_type:
}
$$ = $1;
}
| instance_identifier ':' INTEGER
| instance_identifier ':' const_expr
{
// Bitfield definition.
$1->_bit_width = $3;

View File

@ -74,7 +74,17 @@ CPPInstance(CPPType *type, CPPInstanceIdentifier *ii, int storage_class,
ii->_ident = nullptr;
_storage_class = storage_class;
_initializer = nullptr;
_bit_width = ii->_bit_width;
if (ii->_bit_width != nullptr) {
CPPExpression::Result result = ii->_bit_width->evaluate();
if (result._type != CPPExpression::RT_error) {
_bit_width = ii->_bit_width->evaluate().as_integer();
} else {
_bit_width = -1;
}
} else {
_bit_width = -1;
}
CPPParameterList *params = ii->get_initializer();
if (params != nullptr) {

View File

@ -82,7 +82,7 @@ initializer_type(CPPParameterList *params) {
CPPInstanceIdentifier::
CPPInstanceIdentifier(CPPIdentifier *ident) :
_ident(ident),
_bit_width(-1),
_bit_width(nullptr),
_packed(false) {
}

View File

@ -86,8 +86,8 @@ public:
typedef std::vector<Modifier> Modifiers;
Modifiers _modifiers;
// If not -1, indicates a bitfield
int _bit_width;
// If not null, indicates a bitfield
CPPExpression *_bit_width;
// Indicates a parameter pack
bool _packed;