_CCodeGenerator.generate_c_src(): use Jinja 2 templates
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 28 Aug 2020 22:00:56 +0000 (18:00 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 3 Sep 2020 14:07:20 +0000 (10:07 -0400)
commitd6483c83b9c0e3faa8206fa68ba2d1d4f0c304a8
treebe35b253e7ee4285b793273630447e3727037a50
parentdd163f112d229975133aac5881f7d32bcaa15903
_CCodeGenerator.generate_c_src(): use Jinja 2 templates

This patch makes _CCodeGenerator.generate_c_src() use Jinja 2 templates
instead of a generic code generator.

The purpose of switching to Jinja 2 templates is to make such templates
more readable than Python code, to make them reusable, and to assign
different concerns to different templates.

As such a change is hard to make incrementally, this patch changes many
parts of the tree at once. More specifically, the impacted parts are:

`gen.py`:
    * _CCodeGenerator.generate_c_src() now creates _all_ the operations
      (which used to be named "serialization actions"; more about this
      below) for all the root field types and passes this to the
      `barectf.c.j2` template at rendering time.

      _CCodeGenerator.generate_c_src() doesn't use anything from
      `barectf.codegen` or `barectf.templates`.

    * What used to be named `_SerializationActions` is now named
      `_OpsBuilder`.

      An `_OpsBuilder` object does pretty much the same job as what a
      `_SerializationActions` object previously did: when you call its
      append_root_ft() method, it iterates the members of the structure
      field type recursively to append corresponding operations to
      itself. You can then get its current list of operations with its
      `ops` property.

    * An operation is either "align" (`_AlignOp`) or "write"
      (`_WriteOp`).

      Each of those operations can occur in two different contexts:
      serialization or size computation. Therefore, each operation
      contains two `barectf.template._Template` objects: one for the
      serialization function and one for the size computation function.
      You can render both templates using the serialize_str() and
      size_str() methods (to which you can pass more rendering context).

      Although most operations use the same generic templates, the
      serialization template of some "write" operations can be custom.
      This is how special fields are handled, like the packet header's
      magic number or the event header's time.

      In `barectf/templates/c`, template files are named as such:

      +--------------------+------------------------+-------------------+
      | Operation/Function | Serialization          | Size              |
      +====================+========================+===================+
      | Align              | `serialize-align-*.j2` | `size-align-*.j2` |
      | Write              | `serialize-write-*.j2` | `size-write-*.j2` |
      +--------------------+------------------------+-------------------+

    * An operation contains all the names you need to craft a source
      variable name (to be joined with `_`).

      This is why root field type prefixes (`_RootFtPrefixes`) don't
      contain a trailing underscore anymore.

      The topmost name of an operation `o` is `o.top_name`; templates
      mostly use this property for C comments.

`template.py`:
`codegen.py`:
    Both files are removed as they're no longer needed by the project.

`templates/c`:
    `barectf.c.j2`:
        New template which generates the whole `barectf.c` file (given
        the file name prefix is `barectf`).

        This template generates, in this order:

        * The licence header.
        * Utility C macros.
        * Internal data structures.
        * Public barectf context access funtions.
        * Internal functions.
        * Public barectf context initialization function.
        * For each stream type:
          * Public packet opening function.
          * Public packet closing function.
          * Internal event header serialization function.
          * Internal event common context serialization function.
          * For each event type:
            * Internal serialization function.
          * For each event type:
            * Internal size computation function.
          * For each event type:
            * Public tracing function.

    `barectf.c-macros.j2`:
        Macros to be used by the `barectf.c.j2` template:

        * open_close_func_preamble()
        * ft_call_params()

    `common.j2`:
        New trace_func_name() and op_src() macros.

    `align-statements-comment.j2`:
        C comment for any alignment statement.

    `serialize-align-statements.j2`:
        Alignment statements for serialization functions.

    `serialize-write-*statements.j2`:
        Writing statements for serialization functions.

    `serialize-write-statements-comment.j2`:
        C comment for generic writing statements (for serialization
        functions).

    `size-align-statements.j2`:
        Alignment statements for size computation functions.

    `size-write-*-statements.j2`:
        Writing statements for size computation functions.

    Also, to make templates simpler, I standardized the following C
    variable names:

    `ctx`:
        Generic barectf context.

    `sctx`:
        Stream-type-specific barectf context.

    `vctx`:
        `ctx` as a `void` pointer.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
52 files changed:
barectf/codegen.py [deleted file]
barectf/gen.py
barectf/templates.py [deleted file]
barectf/templates/barectf.h.j2 [deleted file]
barectf/templates/bitfield.h.j2 [deleted file]
barectf/templates/c-close-func-proto.j2 [deleted file]
barectf/templates/c-common.j2 [deleted file]
barectf/templates/c-ctx-init-func-proto.j2 [deleted file]
barectf/templates/c-open-func-proto.j2 [deleted file]
barectf/templates/c-trace-func-proto.j2 [deleted file]
barectf/templates/c/align-statements-comment.j2 [new file with mode: 0644]
barectf/templates/c/barectf.c-macros.j2 [new file with mode: 0644]
barectf/templates/c/barectf.c.j2 [new file with mode: 0644]
barectf/templates/c/barectf.h.j2 [new file with mode: 0644]
barectf/templates/c/bitfield.h.j2 [new file with mode: 0644]
barectf/templates/c/close-func-proto.j2 [new file with mode: 0644]
barectf/templates/c/common.j2 [new file with mode: 0644]
barectf/templates/c/ctx-init-func-proto.j2 [new file with mode: 0644]
barectf/templates/c/func-proto-params.j2 [new file with mode: 0644]
barectf/templates/c/open-func-proto.j2 [new file with mode: 0644]
barectf/templates/c/serialize-align-statements.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-bit-array-statements.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-ev-type-id-statements.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-int-statements.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-magic-statements.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-packet-size-statements.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-real-statements.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-saved-int-statements.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-skip-save-statements.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-statements-comment.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-stream-type-id-statements.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-string-statements.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-time-statements.j2 [new file with mode: 0644]
barectf/templates/c/serialize-write-uuid-statements.j2 [new file with mode: 0644]
barectf/templates/c/size-align-statements.j2 [new file with mode: 0644]
barectf/templates/c/size-write-bit-array-statements.j2 [new file with mode: 0644]
barectf/templates/c/size-write-string-statements.j2 [new file with mode: 0644]
barectf/templates/c/trace-func-proto.j2 [new file with mode: 0644]
barectf/templates/metadata-enum-ft.j2 [deleted file]
barectf/templates/metadata-int-ft.j2 [deleted file]
barectf/templates/metadata-real-ft.j2 [deleted file]
barectf/templates/metadata-str-ft.j2 [deleted file]
barectf/templates/metadata-struct-ft.j2 [deleted file]
barectf/templates/metadata.j2 [deleted file]
barectf/templates/metadata/enum-ft.j2 [new file with mode: 0644]
barectf/templates/metadata/int-ft.j2 [new file with mode: 0644]
barectf/templates/metadata/metadata.j2 [new file with mode: 0644]
barectf/templates/metadata/real-ft.j2 [new file with mode: 0644]
barectf/templates/metadata/str-ft.j2 [new file with mode: 0644]
barectf/templates/metadata/struct-ft.j2 [new file with mode: 0644]
barectf/tsdl182gen.py
doc/examples/linux-fs-simple/config.yaml
This page took 0.026947 seconds and 4 git commands to generate.