Commit 6cc32b0e authored by Marc-André Lureau's avatar Marc-André Lureau Committed by Markus Armbruster
Browse files

qapi: add 'if' to enum members



QAPISchemaMember gains .ifcond for enum members: inherited classes,
such as QAPISchemaObjectTypeMember, will thus have an ifcond member
after this (those different types will also use the .ifcond to store
the condition and generate conditional code in the following patches).

The generated code remains unconditional for now. Later patches
generate the conditionals.

Signed-off-by: default avatarMarc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: default avatarMarkus Armbruster <armbru@redhat.com>
Message-Id: <20181213123724.4866-10-marcandre.lureau@redhat.com>
Signed-off-by: default avatarMarkus Armbruster <armbru@redhat.com>
parent ea738b21
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -752,6 +752,15 @@ gets its generated code guarded like this:
 #endif /* defined(HAVE_BAR) */
 #endif /* defined(CONFIG_FOO) */

An enum value can be replaced by a dictionary with a 'name' and a 'if'
key.

Example: a conditional 'bar' enum member.

{ 'enum': 'IfEnum', 'data':
  [ 'foo',
    { 'name' : 'bar', 'if': 'defined(IFCOND)' } ] }

Please note that you are responsible to ensure that the C code will
compile with an arbitrary combination of conditions, since the
generators are unable to check it at this point.
+5 −3
Original line number Diff line number Diff line
@@ -871,7 +871,8 @@ def check_enum(expr, info):

    for member in members:
        source = "dictionary member of enum '%s'" % name
        check_known_keys(info, source, member, ['name'], [])
        check_known_keys(info, source, member, ['name'], ['if'])
        check_if(member, info)
        check_name(info, "Member of enum '%s'" % name, member['name'],
                   enum_member=True)

@@ -1345,9 +1346,10 @@ class QAPISchemaObjectType(QAPISchemaType):
class QAPISchemaMember(object):
    role = 'member'

    def __init__(self, name):
    def __init__(self, name, ifcond=None):
        assert isinstance(name, str)
        self.name = name
        self.ifcond = listify_cond(ifcond)
        self.owner = None

    def set_owner(self, name):
@@ -1656,7 +1658,7 @@ class QAPISchema(object):
                                            qtype_values, 'QTYPE'))

    def _make_enum_members(self, values):
        return [QAPISchemaMember(v['name']) for v in values]
        return [QAPISchemaMember(v['name'], v.get('if')) for v in values]

    def _make_implicit_enum_type(self, name, info, ifcond, values):
        # See also QAPISchemaObjectTypeMember._pretty_owner()
+1 −0
Original line number Diff line number Diff line
@@ -384,6 +384,7 @@ qapi-schema += enum-bad-name.json
qapi-schema += enum-bad-prefix.json
qapi-schema += enum-clash-member.json
qapi-schema += enum-dict-member-unknown.json
qapi-schema += enum-if-invalid.json
qapi-schema += enum-int-member.json
qapi-schema += enum-member-case.json
qapi-schema += enum-missing-data.json
+1 −1
Original line number Diff line number Diff line
tests/qapi-schema/enum-dict-member-unknown.json:2: Unknown key 'bad-key' in dictionary member of enum 'MyEnum'
Valid keys are 'name'.
Valid keys are 'if', 'name'.
+1 −0
Original line number Diff line number Diff line
tests/qapi-schema/enum-if-invalid.json:2: 'if' condition must be a string or a list of strings
Loading