All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: mdroth@linux.vnet.ibm.com, marcandre.lureau@redhat.com,
	eblake@redhat.com
Subject: [Qemu-devel] [PATCH v2 17/29] qapi: Record 'include' directives in intermediate representation
Date: Sun, 11 Feb 2018 10:35:55 +0100	[thread overview]
Message-ID: <20180211093607.27351-18-armbru@redhat.com> (raw)
In-Reply-To: <20180211093607.27351-1-armbru@redhat.com>

The include directive permits modular QAPI schemata, but the generated
code is monolithic all the same.  To permit generating modular code,
the front end needs to pass more information on inclusions to the back
ends.  The commit before last added the necessary information to the
parse tree.  This commit adds it to the intermediate representation
and its QAPISchemaVisitor.  A later commit will use this to to
generate modular code.

New entity QAPISchemaInclude represents inclusions.  Call new visitor
method visit_include() for it, so visitors can see the sub-modules a
module includes.

Note that unlike other entities, QAPISchemaInclude has no name, and is
therefore not added to entity_dict.

New QAPISchemaEntity attribute @module names the entity's source file.
Call new visitor method visit_module() when it changes during a visit,
so visitors can keep track of the module being visited.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 scripts/qapi/common.py                   | 44 ++++++++++++++++++++++++++++----
 tests/qapi-schema/comments.out           |  1 +
 tests/qapi-schema/doc-bad-section.out    |  1 +
 tests/qapi-schema/doc-good.out           |  1 +
 tests/qapi-schema/event-case.out         |  1 +
 tests/qapi-schema/ident-with-escape.out  |  1 +
 tests/qapi-schema/include-relpath.out    |  5 ++++
 tests/qapi-schema/include-repetition.out | 10 ++++++++
 tests/qapi-schema/include-simple.out     |  3 +++
 tests/qapi-schema/indented-expr.out      |  1 +
 tests/qapi-schema/qapi-schema-test.out   |  1 +
 tests/qapi-schema/test-qapi.py           |  7 +++++
 12 files changed, 71 insertions(+), 5 deletions(-)

diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
index b531ab519f..29d98ca934 100644
--- a/scripts/qapi/common.py
+++ b/scripts/qapi/common.py
@@ -985,8 +985,9 @@ def check_exprs(exprs):
 
 class QAPISchemaEntity(object):
     def __init__(self, name, info, doc):
-        assert isinstance(name, str)
+        assert name is None or isinstance(name, str)
         self.name = name
+        self.module = None
         # For explicitly defined entities, info points to the (explicit)
         # definition.  For builtins (and their arrays), info is None.
         # For implicitly defined entities, info points to a place that
@@ -1015,10 +1016,16 @@ class QAPISchemaVisitor(object):
     def visit_end(self):
         pass
 
+    def visit_module(self, fname):
+        pass
+
     def visit_needed(self, entity):
         # Default to visiting everything
         return True
 
+    def visit_include(self, fname, info):
+        pass
+
     def visit_builtin_type(self, name, info, json_type):
         pass
 
@@ -1045,6 +1052,16 @@ class QAPISchemaVisitor(object):
         pass
 
 
+class QAPISchemaInclude(QAPISchemaEntity):
+
+    def __init__(self, fname, info):
+        QAPISchemaEntity.__init__(self, None, info, None)
+        self.fname = fname
+
+    def visit(self, visitor):
+        visitor.visit_include(self.fname, self.info)
+
+
 class QAPISchemaType(QAPISchemaEntity):
     # Return the C type for common use.
     # For the types we commonly box, this is a pointer type.
@@ -1472,6 +1489,7 @@ class QAPISchemaEvent(QAPISchemaEntity):
 
 class QAPISchema(object):
     def __init__(self, fname):
+        self._fname = fname
         parser = QAPISchemaParser(open(fname, 'r'))
         exprs = check_exprs(parser.exprs)
         self.docs = parser.docs
@@ -1479,16 +1497,19 @@ class QAPISchema(object):
         self._entity_dict = {}
         self._predefining = True
         self._def_predefineds()
-        self._predefining = False
         self._def_exprs(exprs)
         self.check()
 
     def _def_entity(self, ent):
         # Only the predefined types are allowed to not have info
         assert ent.info or self._predefining
-        assert ent.name not in self._entity_dict
+        assert ent.name is None or ent.name not in self._entity_dict
         self._entity_list.append(ent)
-        self._entity_dict[ent.name] = ent
+        if ent.name is not None:
+            self._entity_dict[ent.name] = ent
+        if ent.info:
+            ent.module = os.path.relpath(ent.info['file'],
+                                         os.path.dirname(self._fname))
 
     def lookup_entity(self, name, typ=None):
         ent = self._entity_dict.get(name)
@@ -1499,6 +1520,15 @@ class QAPISchema(object):
     def lookup_type(self, name):
         return self.lookup_entity(name, QAPISchemaType)
 
+    def _def_include(self, expr, info, doc):
+        include = expr['include']
+        assert doc is None
+        main_info = info
+        while main_info['parent']:
+            main_info = main_info['parent']
+        fname = os.path.relpath(include, os.path.dirname(main_info['file']))
+        self._def_entity(QAPISchemaInclude(fname, info))
+
     def _def_builtin_type(self, name, json_type, c_type):
         self._def_entity(QAPISchemaBuiltinType(name, json_type, c_type))
         # TODO As long as we have QAPI_TYPES_BUILTIN to share multiple
@@ -1681,7 +1711,7 @@ class QAPISchema(object):
             elif 'event' in expr:
                 self._def_event(expr, info, doc)
             elif 'include' in expr:
-                pass
+                self._def_include(expr, info, doc)
             else:
                 assert False
 
@@ -1691,8 +1721,12 @@ class QAPISchema(object):
 
     def visit(self, visitor):
         visitor.visit_begin(self)
+        module = None
         for entity in self._entity_list:
             if visitor.visit_needed(entity):
+                if entity.module != module:
+                    module = entity.module
+                    visitor.visit_module(module)
                 entity.visit(visitor)
         visitor.visit_end()
 
diff --git a/tests/qapi-schema/comments.out b/tests/qapi-schema/comments.out
index 0261ddf202..8d2f1ce8a2 100644
--- a/tests/qapi-schema/comments.out
+++ b/tests/qapi-schema/comments.out
@@ -1,4 +1,5 @@
 object q_empty
 enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
     prefix QTYPE
+module comments.json
 enum Status ['good', 'bad', 'ugly']
diff --git a/tests/qapi-schema/doc-bad-section.out b/tests/qapi-schema/doc-bad-section.out
index 23bf8c71ab..cd28721568 100644
--- a/tests/qapi-schema/doc-bad-section.out
+++ b/tests/qapi-schema/doc-bad-section.out
@@ -1,6 +1,7 @@
 object q_empty
 enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
     prefix QTYPE
+module doc-bad-section.json
 enum Enum ['one', 'two']
 doc symbol=Enum
     body=
diff --git a/tests/qapi-schema/doc-good.out b/tests/qapi-schema/doc-good.out
index 0c07301f07..430b5a87db 100644
--- a/tests/qapi-schema/doc-good.out
+++ b/tests/qapi-schema/doc-good.out
@@ -1,6 +1,7 @@
 object q_empty
 enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
     prefix QTYPE
+module doc-good.json
 enum Enum ['one', 'two']
 object Base
     member base1: Enum optional=False
diff --git a/tests/qapi-schema/event-case.out b/tests/qapi-schema/event-case.out
index 110571b793..88c0964917 100644
--- a/tests/qapi-schema/event-case.out
+++ b/tests/qapi-schema/event-case.out
@@ -1,5 +1,6 @@
 object q_empty
 enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
     prefix QTYPE
+module event-case.json
 event oops None
    boxed=False
diff --git a/tests/qapi-schema/ident-with-escape.out b/tests/qapi-schema/ident-with-escape.out
index 8336aa7629..ee3b34e623 100644
--- a/tests/qapi-schema/ident-with-escape.out
+++ b/tests/qapi-schema/ident-with-escape.out
@@ -1,6 +1,7 @@
 object q_empty
 enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
     prefix QTYPE
+module ident-with-escape.json
 object q_obj_fooA-arg
     member bar1: str optional=False
 command fooA q_obj_fooA-arg -> None
diff --git a/tests/qapi-schema/include-relpath.out b/tests/qapi-schema/include-relpath.out
index 0261ddf202..ebbabd7a18 100644
--- a/tests/qapi-schema/include-relpath.out
+++ b/tests/qapi-schema/include-relpath.out
@@ -1,4 +1,9 @@
 object q_empty
 enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
     prefix QTYPE
+module include-relpath.json
+include include/relpath.json
+module include/relpath.json
+include include-relpath-sub.json
+module include-relpath-sub.json
 enum Status ['good', 'bad', 'ugly']
diff --git a/tests/qapi-schema/include-repetition.out b/tests/qapi-schema/include-repetition.out
index 0261ddf202..7235e055bc 100644
--- a/tests/qapi-schema/include-repetition.out
+++ b/tests/qapi-schema/include-repetition.out
@@ -1,4 +1,14 @@
 object q_empty
 enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
     prefix QTYPE
+module include-repetition.json
+include comments.json
+module comments.json
 enum Status ['good', 'bad', 'ugly']
+module include-repetition.json
+include include-repetition-sub.json
+module include-repetition-sub.json
+include comments.json
+include comments.json
+module include-repetition.json
+include comments.json
diff --git a/tests/qapi-schema/include-simple.out b/tests/qapi-schema/include-simple.out
index 0261ddf202..006f723eeb 100644
--- a/tests/qapi-schema/include-simple.out
+++ b/tests/qapi-schema/include-simple.out
@@ -1,4 +1,7 @@
 object q_empty
 enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
     prefix QTYPE
+module include-simple.json
+include include-simple-sub.json
+module include-simple-sub.json
 enum Status ['good', 'bad', 'ugly']
diff --git a/tests/qapi-schema/indented-expr.out b/tests/qapi-schema/indented-expr.out
index 34de8be426..a79935e8c3 100644
--- a/tests/qapi-schema/indented-expr.out
+++ b/tests/qapi-schema/indented-expr.out
@@ -1,6 +1,7 @@
 object q_empty
 enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
     prefix QTYPE
+module indented-expr.json
 command eins None -> None
    gen=True success_response=True boxed=False
 command zwei None -> None
diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qapi-schema-test.out
index 50706b0136..012e7fc06a 100644
--- a/tests/qapi-schema/qapi-schema-test.out
+++ b/tests/qapi-schema/qapi-schema-test.out
@@ -1,6 +1,7 @@
 object q_empty
 enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
     prefix QTYPE
+module qapi-schema-test.json
 object TestStruct
     member integer: int optional=False
     member boolean: bool optional=False
diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py
index 4da14b43af..67e417e298 100644
--- a/tests/qapi-schema/test-qapi.py
+++ b/tests/qapi-schema/test-qapi.py
@@ -16,6 +16,13 @@ from qapi.common import QAPIError, QAPISchema, QAPISchemaVisitor
 
 
 class QAPISchemaTestVisitor(QAPISchemaVisitor):
+
+    def visit_module(self, name):
+        print('module %s' % name)
+
+    def visit_include(self, name, info):
+        print('include %s' % name)
+
     def visit_enum_type(self, name, info, values, prefix):
         print('enum %s %s' % (name, values))
         if prefix:
-- 
2.13.6

  parent reply	other threads:[~2018-02-11  9:36 UTC|newest]

Thread overview: 110+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-11  9:35 [Qemu-devel] [PATCH v2 00/29] Modularize generated QAPI code Markus Armbruster
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 01/29] Include qapi/qmp/qerror.h exactly where needed Markus Armbruster
2018-02-12 16:55   ` Eric Blake
2018-02-13 15:24   ` Marc-Andre Lureau
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 02/29] qapi: Streamline boilerplate comment generation Markus Armbruster
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 03/29] qapi: Generate up-to-date copyright notice Markus Armbruster
2018-02-12 19:40   ` Eric Blake
2018-02-16 22:53   ` Michael Roth
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 04/29] qapi: Rename variable holding the QAPISchemaGenFOOVisitor Markus Armbruster
2018-02-12 16:59   ` Eric Blake
2018-02-13 15:24   ` Marc-Andre Lureau
2018-02-16 22:56   ` Michael Roth
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 05/29] qapi: New classes QAPIGenC, QAPIGenH, QAPIGenDoc Markus Armbruster
2018-02-17  0:59   ` Michael Roth
2018-02-23 17:18     ` Markus Armbruster
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 06/29] qapi: Reduce use of global variables in generators some Markus Armbruster
2018-02-17  1:22   ` Michael Roth
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 07/29] qapi: Turn generators into modules Markus Armbruster
2018-02-17  1:29   ` Michael Roth
2018-02-27 15:53   ` Eric Blake
2018-02-27 16:01     ` Daniel P. Berrangé
2018-04-13 12:49       ` Markus Armbruster
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 08/29] qapi-gen: New common driver for code and doc generators Markus Armbruster
2018-02-12 19:42   ` Eric Blake
2018-02-23 17:20     ` Markus Armbruster
2018-02-26 20:02       ` Eric Blake
2018-02-18 22:16   ` Michael Roth
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 09/29] qapi-gen: Convert from getopt to argparse Markus Armbruster
2018-02-12 19:44   ` Eric Blake
2018-02-13 15:24   ` Marc-Andre Lureau
2018-02-18 22:31   ` Michael Roth
2018-02-23 17:21     ` Markus Armbruster
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 10/29] qapi: Touch generated files only when they change Markus Armbruster
2018-02-12 19:48   ` Eric Blake
2018-02-13 15:22     ` Marc-Andre Lureau
2018-02-23 17:22       ` Markus Armbruster
2018-02-18 22:43   ` Michael Roth
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 11/29] qapi: Improve include file name reporting in error messages Markus Armbruster
2018-02-18 23:11   ` Michael Roth
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 12/29] qapi/common: Eliminate QAPISchema.exprs Markus Armbruster
2018-02-18 23:13   ` Michael Roth
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 13/29] qapi: Lift error reporting from QAPISchema.__init__() to callers Markus Armbruster
2018-02-12 19:52   ` Eric Blake
2018-02-18 23:17   ` Michael Roth
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 14/29] qapi: Concentrate QAPISchemaParser.exprs updates in .__init__() Markus Armbruster
2018-02-12 19:56   ` Eric Blake
2018-02-18 23:32   ` Michael Roth
2018-02-23 17:29     ` Markus Armbruster
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 15/29] qapi: Record 'include' directives in parse tree Markus Armbruster
2018-02-12 20:03   ` Eric Blake
2018-02-18 23:57   ` Michael Roth
2018-02-23 17:31     ` Markus Armbruster
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 16/29] qapi: Generate in source order Markus Armbruster
2018-02-12 20:06   ` Eric Blake
2018-02-19  0:01   ` Michael Roth
2018-02-11  9:35 ` Markus Armbruster [this message]
2018-02-12 20:34   ` [Qemu-devel] [PATCH v2 17/29] qapi: Record 'include' directives in intermediate representation Eric Blake
2018-02-23 17:33     ` Markus Armbruster
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 18/29] qapi: Rename generated qmp-marshal.c to qmp-commands.c Markus Armbruster
2018-02-12 20:56   ` Eric Blake
2018-02-13 15:25   ` Marc-Andre Lureau
2018-02-19  0:21   ` Michael Roth
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 19/29] qapi: Make code-generating visitors use QAPIGen more Markus Armbruster
2018-02-12 21:06   ` Eric Blake
2018-02-13 15:25   ` Marc-Andre Lureau
2018-02-19 15:52   ` Michael Roth
2018-02-23 17:35     ` Markus Armbruster
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 20/29] qapi/types qapi/visit: Generate built-in stuff into separate files Markus Armbruster
2018-02-12 21:44   ` Eric Blake
2018-02-23 17:36     ` Markus Armbruster
2018-02-13 15:25   ` Marc-Andre Lureau
2018-02-19 19:30   ` Michael Roth
2018-02-11  9:35 ` [Qemu-devel] [PATCH v2 21/29] qapi/common: Fix guardname() for funny filenames Markus Armbruster
2018-02-19 18:18   ` Michael Roth
2018-02-11  9:36 ` [Qemu-devel] [PATCH v2 22/29] qapi: Generate separate .h, .c for each module Markus Armbruster
2018-02-12 22:06   ` Eric Blake
2018-02-23 17:41     ` Markus Armbruster
2018-02-26 22:39       ` Eric Blake
2018-02-11  9:36 ` [Qemu-devel] [PATCH v2 23/29] Include less of the generated modular QAPI headers Markus Armbruster
2018-02-12 22:13   ` Eric Blake
2018-02-23 17:47     ` Markus Armbruster
2018-02-13 15:54   ` Marc-Andre Lureau
2018-02-11  9:36 ` [Qemu-devel] [PATCH v2 24/29] qapi: Empty out qapi-schema.json Markus Armbruster
2018-02-12 22:26   ` Eric Blake
2018-02-23 17:50     ` Markus Armbruster
2018-02-11  9:36 ` [Qemu-devel] [PATCH v2 25/29] docs/devel/writing-qmp-commands: Update for modular QAPI Markus Armbruster
2018-02-12 22:27   ` Eric Blake
2018-02-13 16:00   ` Marc-Andre Lureau
2018-02-19 19:33   ` Michael Roth
2018-02-11  9:36 ` [Qemu-devel] [PATCH v2 26/29] docs: Correct outdated information on QAPI Markus Armbruster
2018-02-12 22:29   ` Eric Blake
2018-02-13 16:03   ` Marc-Andre Lureau
2018-02-19 19:35   ` Michael Roth
2018-02-11  9:36 ` [Qemu-devel] [PATCH v2 27/29] qapi: Move qapi-schema.json to qapi/, rename generated files Markus Armbruster
2018-02-12 22:36   ` Eric Blake
2018-02-23 17:51     ` Markus Armbruster
2018-02-13 16:08   ` Marc-Andre Lureau
2018-02-19 19:37   ` Michael Roth
2018-02-11  9:36 ` [Qemu-devel] [PATCH v2 28/29] Fix up dangling references to qmp-commands.* in comment and doc Markus Armbruster
2018-02-12 22:47   ` Eric Blake
2018-02-13 16:12   ` Marc-Andre Lureau
2018-02-19 19:38   ` Michael Roth
2018-02-11  9:36 ` [Qemu-devel] [PATCH v2 29/29] qapi: Don't create useless directory qapi-generated Markus Armbruster
2018-02-12 22:48   ` Eric Blake
2018-02-13 16:15   ` Marc-Andre Lureau
2018-02-19 19:38   ` Michael Roth
2018-02-11 10:16 ` [Qemu-devel] [PATCH v2 00/29] Modularize generated QAPI code no-reply
2018-02-26 22:57 ` [Qemu-devel] [PATCH v2 23.5/29] watchdog: Consolidate QAPI into single file Eric Blake
2018-02-26 23:10   ` Eric Blake
2018-02-27 14:19 ` [Qemu-devel] [PATCH v2 00/29] Modularize generated QAPI code Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180211093607.27351-18-armbru@redhat.com \
    --to=armbru@redhat.com \
    --cc=eblake@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.