2019-10-18 10:43:44 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# QAPI code generation
|
|
|
|
#
|
2020-10-09 19:15:39 +03:00
|
|
|
# Copyright (c) 2015-2019 Red Hat Inc.
|
2019-10-18 10:43:44 +03:00
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Markus Armbruster <armbru@redhat.com>
|
|
|
|
# Marc-André Lureau <marcandre.lureau@redhat.com>
|
|
|
|
#
|
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2.
|
|
|
|
# See the COPYING file in the top-level directory.
|
|
|
|
|
2020-10-09 19:15:28 +03:00
|
|
|
from contextlib import contextmanager
|
2019-10-18 10:43:44 +03:00
|
|
|
import os
|
|
|
|
import re
|
2020-10-09 19:15:49 +03:00
|
|
|
from typing import (
|
|
|
|
Dict,
|
|
|
|
Iterator,
|
|
|
|
Optional,
|
2021-02-16 05:17:51 +03:00
|
|
|
Sequence,
|
2020-10-09 19:15:49 +03:00
|
|
|
Tuple,
|
|
|
|
)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2020-10-09 19:15:28 +03:00
|
|
|
from .common import (
|
|
|
|
c_fname,
|
2020-10-09 19:15:39 +03:00
|
|
|
c_name,
|
2020-10-09 19:15:28 +03:00
|
|
|
gen_endif,
|
|
|
|
gen_if,
|
|
|
|
guardend,
|
|
|
|
guardstart,
|
|
|
|
mcgen,
|
|
|
|
)
|
2021-02-01 22:37:36 +03:00
|
|
|
from .schema import (
|
|
|
|
QAPISchemaModule,
|
|
|
|
QAPISchemaObjectType,
|
|
|
|
QAPISchemaVisitor,
|
|
|
|
)
|
2020-10-09 19:15:49 +03:00
|
|
|
from .source import QAPISourceInfo
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
2020-03-04 18:59:29 +03:00
|
|
|
class QAPIGen:
|
2021-02-01 22:37:45 +03:00
|
|
|
def __init__(self, fname: str):
|
2019-10-18 10:43:44 +03:00
|
|
|
self.fname = fname
|
|
|
|
self._preamble = ''
|
|
|
|
self._body = ''
|
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def preamble_add(self, text: str) -> None:
|
2019-10-18 10:43:44 +03:00
|
|
|
self._preamble += text
|
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def add(self, text: str) -> None:
|
2019-10-18 10:43:44 +03:00
|
|
|
self._body += text
|
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def get_content(self) -> str:
|
2019-10-18 10:43:44 +03:00
|
|
|
return self._top() + self._preamble + self._body + self._bottom()
|
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def _top(self) -> str:
|
2020-10-09 19:15:53 +03:00
|
|
|
# pylint: disable=no-self-use
|
2019-10-18 10:43:44 +03:00
|
|
|
return ''
|
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def _bottom(self) -> str:
|
2020-10-09 19:15:53 +03:00
|
|
|
# pylint: disable=no-self-use
|
2019-10-18 10:43:44 +03:00
|
|
|
return ''
|
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def write(self, output_dir: str) -> None:
|
2020-02-24 17:30:08 +03:00
|
|
|
# Include paths starting with ../ are used to reuse modules of the main
|
|
|
|
# schema in specialised schemas. Don't overwrite the files that are
|
|
|
|
# already generated for the main schema.
|
|
|
|
if self.fname.startswith('../'):
|
|
|
|
return
|
2019-10-18 10:43:44 +03:00
|
|
|
pathname = os.path.join(output_dir, self.fname)
|
2020-03-04 18:59:32 +03:00
|
|
|
odir = os.path.dirname(pathname)
|
2020-10-09 19:15:52 +03:00
|
|
|
|
2020-03-04 18:59:32 +03:00
|
|
|
if odir:
|
2020-10-09 19:15:52 +03:00
|
|
|
os.makedirs(odir, exist_ok=True)
|
|
|
|
|
|
|
|
# use os.open for O_CREAT to create and read a non-existant file
|
2019-10-18 10:43:44 +03:00
|
|
|
fd = os.open(pathname, os.O_RDWR | os.O_CREAT, 0o666)
|
2020-10-09 19:15:52 +03:00
|
|
|
with os.fdopen(fd, 'r+', encoding='utf-8') as fp:
|
|
|
|
text = self.get_content()
|
|
|
|
oldtext = fp.read(len(text) + 1)
|
|
|
|
if text != oldtext:
|
|
|
|
fp.seek(0)
|
|
|
|
fp.truncate(0)
|
|
|
|
fp.write(text)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
2021-02-16 05:17:51 +03:00
|
|
|
def _wrap_ifcond(ifcond: Sequence[str], before: str, after: str) -> str:
|
2019-10-18 10:43:44 +03:00
|
|
|
if before == after:
|
|
|
|
return after # suppress empty #if ... #endif
|
|
|
|
|
|
|
|
assert after.startswith(before)
|
|
|
|
out = before
|
|
|
|
added = after[len(before):]
|
|
|
|
if added[0] == '\n':
|
|
|
|
out += '\n'
|
|
|
|
added = added[1:]
|
|
|
|
out += gen_if(ifcond)
|
|
|
|
out += added
|
|
|
|
out += gen_endif(ifcond)
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2020-10-09 19:15:39 +03:00
|
|
|
def build_params(arg_type: Optional[QAPISchemaObjectType],
|
|
|
|
boxed: bool,
|
|
|
|
extra: Optional[str] = None) -> str:
|
|
|
|
ret = ''
|
|
|
|
sep = ''
|
|
|
|
if boxed:
|
|
|
|
assert arg_type
|
|
|
|
ret += '%s arg' % arg_type.c_param_type()
|
|
|
|
sep = ', '
|
|
|
|
elif arg_type:
|
|
|
|
assert not arg_type.variants
|
|
|
|
for memb in arg_type.members:
|
|
|
|
ret += sep
|
|
|
|
sep = ', '
|
|
|
|
if memb.optional:
|
|
|
|
ret += 'bool has_%s, ' % c_name(memb.name)
|
|
|
|
ret += '%s %s' % (memb.type.c_param_type(),
|
|
|
|
c_name(memb.name))
|
|
|
|
if extra:
|
|
|
|
ret += sep + extra
|
|
|
|
return ret if ret else 'void'
|
|
|
|
|
|
|
|
|
2019-10-18 10:43:44 +03:00
|
|
|
class QAPIGenCCode(QAPIGen):
|
2021-02-01 22:37:45 +03:00
|
|
|
def __init__(self, fname: str):
|
2020-03-04 18:59:31 +03:00
|
|
|
super().__init__(fname)
|
2021-02-16 05:17:51 +03:00
|
|
|
self._start_if: Optional[Tuple[Sequence[str], str, str]] = None
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2021-02-16 05:17:51 +03:00
|
|
|
def start_if(self, ifcond: Sequence[str]) -> None:
|
2019-10-18 10:43:44 +03:00
|
|
|
assert self._start_if is None
|
|
|
|
self._start_if = (ifcond, self._body, self._preamble)
|
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def end_if(self) -> None:
|
2021-02-01 22:37:35 +03:00
|
|
|
assert self._start_if is not None
|
2019-10-18 10:43:44 +03:00
|
|
|
self._body = _wrap_ifcond(self._start_if[0],
|
|
|
|
self._start_if[1], self._body)
|
|
|
|
self._preamble = _wrap_ifcond(self._start_if[0],
|
|
|
|
self._start_if[2], self._preamble)
|
2021-02-01 22:37:35 +03:00
|
|
|
self._start_if = None
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def get_content(self) -> str:
|
2019-10-18 10:43:44 +03:00
|
|
|
assert self._start_if is None
|
2020-03-04 18:59:31 +03:00
|
|
|
return super().get_content()
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
class QAPIGenC(QAPIGenCCode):
|
2020-10-09 19:15:49 +03:00
|
|
|
def __init__(self, fname: str, blurb: str, pydoc: str):
|
2020-03-04 18:59:31 +03:00
|
|
|
super().__init__(fname)
|
2019-10-18 10:43:44 +03:00
|
|
|
self._blurb = blurb
|
|
|
|
self._copyright = '\n * '.join(re.findall(r'^Copyright .*', pydoc,
|
|
|
|
re.MULTILINE))
|
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def _top(self) -> str:
|
2019-10-18 10:43:44 +03:00
|
|
|
return mcgen('''
|
|
|
|
/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
|
|
|
|
|
|
|
|
/*
|
|
|
|
%(blurb)s
|
|
|
|
*
|
|
|
|
* %(copyright)s
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
''',
|
|
|
|
blurb=self._blurb, copyright=self._copyright)
|
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def _bottom(self) -> str:
|
2019-10-18 10:43:44 +03:00
|
|
|
return mcgen('''
|
|
|
|
|
|
|
|
/* Dummy declaration to prevent empty .o file */
|
|
|
|
char qapi_dummy_%(name)s;
|
|
|
|
''',
|
|
|
|
name=c_fname(self.fname))
|
|
|
|
|
|
|
|
|
|
|
|
class QAPIGenH(QAPIGenC):
|
2020-10-09 19:15:49 +03:00
|
|
|
def _top(self) -> str:
|
2020-03-04 18:59:31 +03:00
|
|
|
return super()._top() + guardstart(self.fname)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def _bottom(self) -> str:
|
2019-10-18 10:43:44 +03:00
|
|
|
return guardend(self.fname)
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2021-02-16 05:17:51 +03:00
|
|
|
def ifcontext(ifcond: Sequence[str], *args: QAPIGenCCode) -> Iterator[None]:
|
2020-10-09 19:15:24 +03:00
|
|
|
"""
|
|
|
|
A with-statement context manager that wraps with `start_if()` / `end_if()`.
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2021-02-16 05:17:51 +03:00
|
|
|
:param ifcond: A sequence of conditionals, passed to `start_if()`.
|
2020-10-09 19:15:24 +03:00
|
|
|
:param args: any number of `QAPIGenCCode`.
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
with ifcontext(ifcond, self._genh, self._genc):
|
|
|
|
modify self._genh and self._genc ...
|
|
|
|
|
|
|
|
Is equivalent to calling::
|
|
|
|
|
|
|
|
self._genh.start_if(ifcond)
|
|
|
|
self._genc.start_if(ifcond)
|
|
|
|
modify self._genh and self._genc ...
|
|
|
|
self._genh.end_if()
|
|
|
|
self._genc.end_if()
|
|
|
|
"""
|
|
|
|
for arg in args:
|
|
|
|
arg.start_if(ifcond)
|
|
|
|
yield
|
|
|
|
for arg in args:
|
|
|
|
arg.end_if()
|
|
|
|
|
|
|
|
|
|
|
|
class QAPISchemaMonolithicCVisitor(QAPISchemaVisitor):
|
2020-10-09 19:15:49 +03:00
|
|
|
def __init__(self,
|
|
|
|
prefix: str,
|
|
|
|
what: str,
|
|
|
|
blurb: str,
|
|
|
|
pydoc: str):
|
2019-10-18 10:43:44 +03:00
|
|
|
self._prefix = prefix
|
|
|
|
self._what = what
|
|
|
|
self._genc = QAPIGenC(self._prefix + self._what + '.c',
|
|
|
|
blurb, pydoc)
|
|
|
|
self._genh = QAPIGenH(self._prefix + self._what + '.h',
|
|
|
|
blurb, pydoc)
|
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def write(self, output_dir: str) -> None:
|
2019-10-18 10:43:44 +03:00
|
|
|
self._genc.write(output_dir)
|
|
|
|
self._genh.write(output_dir)
|
|
|
|
|
|
|
|
|
|
|
|
class QAPISchemaModularCVisitor(QAPISchemaVisitor):
|
2020-10-09 19:15:49 +03:00
|
|
|
def __init__(self,
|
|
|
|
prefix: str,
|
|
|
|
what: str,
|
|
|
|
user_blurb: str,
|
|
|
|
builtin_blurb: Optional[str],
|
|
|
|
pydoc: str):
|
2019-10-18 10:43:44 +03:00
|
|
|
self._prefix = prefix
|
|
|
|
self._what = what
|
2019-11-20 21:25:51 +03:00
|
|
|
self._user_blurb = user_blurb
|
|
|
|
self._builtin_blurb = builtin_blurb
|
2019-10-18 10:43:44 +03:00
|
|
|
self._pydoc = pydoc
|
2021-02-01 22:37:42 +03:00
|
|
|
self._current_module: Optional[str] = None
|
2021-02-01 22:37:39 +03:00
|
|
|
self._module: Dict[str, Tuple[QAPIGenC, QAPIGenH]] = {}
|
2020-10-09 19:15:49 +03:00
|
|
|
self._main_module: Optional[str] = None
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2021-02-01 22:37:42 +03:00
|
|
|
@property
|
|
|
|
def _genc(self) -> QAPIGenC:
|
|
|
|
assert self._current_module is not None
|
|
|
|
return self._module[self._current_module][0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _genh(self) -> QAPIGenH:
|
|
|
|
assert self._current_module is not None
|
|
|
|
return self._module[self._current_module][1]
|
|
|
|
|
2019-10-18 10:43:44 +03:00
|
|
|
@staticmethod
|
2021-02-01 22:37:39 +03:00
|
|
|
def _module_dirname(name: str) -> str:
|
2021-02-01 22:37:36 +03:00
|
|
|
if QAPISchemaModule.is_user_module(name):
|
2019-10-18 10:43:44 +03:00
|
|
|
return os.path.dirname(name)
|
|
|
|
return ''
|
|
|
|
|
2021-02-01 22:37:39 +03:00
|
|
|
def _module_basename(self, what: str, name: str) -> str:
|
2021-02-01 22:37:36 +03:00
|
|
|
ret = '' if QAPISchemaModule.is_builtin_module(name) else self._prefix
|
|
|
|
if QAPISchemaModule.is_user_module(name):
|
2019-10-18 10:43:44 +03:00
|
|
|
basename = os.path.basename(name)
|
|
|
|
ret += what
|
|
|
|
if name != self._main_module:
|
|
|
|
ret += '-' + os.path.splitext(basename)[0]
|
|
|
|
else:
|
2021-02-01 22:37:39 +03:00
|
|
|
assert QAPISchemaModule.is_system_module(name)
|
|
|
|
ret += re.sub(r'-', '-' + name[2:] + '-', what)
|
2019-10-18 10:43:44 +03:00
|
|
|
return ret
|
|
|
|
|
2021-02-01 22:37:39 +03:00
|
|
|
def _module_filename(self, what: str, name: str) -> str:
|
2020-10-09 19:15:51 +03:00
|
|
|
return os.path.join(self._module_dirname(name),
|
2019-10-18 10:43:44 +03:00
|
|
|
self._module_basename(what, name))
|
|
|
|
|
2021-02-01 22:37:39 +03:00
|
|
|
def _add_module(self, name: str, blurb: str) -> None:
|
2021-02-01 22:37:40 +03:00
|
|
|
if QAPISchemaModule.is_user_module(name):
|
|
|
|
if self._main_module is None:
|
|
|
|
self._main_module = name
|
2019-10-18 10:43:44 +03:00
|
|
|
basename = self._module_filename(self._what, name)
|
|
|
|
genc = QAPIGenC(basename + '.c', blurb, self._pydoc)
|
|
|
|
genh = QAPIGenH(basename + '.h', blurb, self._pydoc)
|
|
|
|
self._module[name] = (genc, genh)
|
2021-02-01 22:37:42 +03:00
|
|
|
self._current_module = name
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2021-02-01 22:37:43 +03:00
|
|
|
@contextmanager
|
|
|
|
def _temp_module(self, name: str) -> Iterator[None]:
|
|
|
|
old_module = self._current_module
|
|
|
|
self._current_module = name
|
|
|
|
yield
|
|
|
|
self._current_module = old_module
|
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def write(self, output_dir: str, opt_builtins: bool = False) -> None:
|
2019-10-18 10:43:44 +03:00
|
|
|
for name in self._module:
|
2021-02-01 22:37:36 +03:00
|
|
|
if QAPISchemaModule.is_builtin_module(name) and not opt_builtins:
|
2019-10-18 10:43:44 +03:00
|
|
|
continue
|
|
|
|
(genc, genh) = self._module[name]
|
|
|
|
genc.write(output_dir)
|
|
|
|
genh.write(output_dir)
|
|
|
|
|
2021-02-01 22:37:37 +03:00
|
|
|
def _begin_builtin_module(self) -> None:
|
2020-03-04 18:59:32 +03:00
|
|
|
pass
|
|
|
|
|
2020-10-09 19:15:49 +03:00
|
|
|
def _begin_user_module(self, name: str) -> None:
|
2019-10-18 10:43:44 +03:00
|
|
|
pass
|
|
|
|
|
2021-02-01 22:37:39 +03:00
|
|
|
def visit_module(self, name: str) -> None:
|
2021-02-01 22:37:37 +03:00
|
|
|
if QAPISchemaModule.is_builtin_module(name):
|
2019-11-20 21:25:51 +03:00
|
|
|
if self._builtin_blurb:
|
2021-02-01 22:37:40 +03:00
|
|
|
self._add_module(name, self._builtin_blurb)
|
2021-02-01 22:37:37 +03:00
|
|
|
self._begin_builtin_module()
|
2019-11-20 21:25:51 +03:00
|
|
|
else:
|
|
|
|
# The built-in module has not been created. No code may
|
|
|
|
# be generated.
|
2021-02-01 22:37:42 +03:00
|
|
|
self._current_module = None
|
2019-10-18 10:43:44 +03:00
|
|
|
else:
|
2021-02-01 22:37:37 +03:00
|
|
|
assert QAPISchemaModule.is_user_module(name)
|
2021-02-01 22:37:40 +03:00
|
|
|
self._add_module(name, self._user_blurb)
|
2019-10-18 10:43:44 +03:00
|
|
|
self._begin_user_module(name)
|
|
|
|
|
2021-02-01 22:37:46 +03:00
|
|
|
def visit_include(self, name: str, info: Optional[QAPISourceInfo]) -> None:
|
2019-10-18 10:43:44 +03:00
|
|
|
relname = os.path.relpath(self._module_filename(self._what, name),
|
|
|
|
os.path.dirname(self._genh.fname))
|
|
|
|
self._genh.preamble_add(mcgen('''
|
|
|
|
#include "%(relname)s.h"
|
|
|
|
''',
|
|
|
|
relname=relname))
|