xref: /openbmc/qemu/scripts/qapi/source.py (revision 3404e57410b80734e6961b6574d6683d9c3d9c14)
1#
2# QAPI frontend source file info
3#
4# Copyright (c) 2019 Red Hat Inc.
5#
6# Authors:
7#  Markus Armbruster <armbru@redhat.com>
8#
9# This work is licensed under the terms of the GNU GPL, version 2.
10# See the COPYING file in the top-level directory.
11
12import copy
13from typing import List, Optional, TypeVar
14
15
16class QAPISchemaPragma:
17    # Replace with @dataclass in Python 3.7+
18    # pylint: disable=too-few-public-methods
19
20    def __init__(self) -> None:
21        # Are documentation comments required?
22        self.doc_required = False
23        # Commands whose names may use '_'
24        self.command_name_exceptions: List[str] = []
25        # Commands allowed to return a non-dictionary
26        self.command_returns_exceptions: List[str] = []
27        # Types whose member names may violate case conventions
28        self.member_name_exceptions: List[str] = []
29
30
31class QAPISourceInfo:
32    T = TypeVar('T', bound='QAPISourceInfo')
33
34    def __init__(self, fname: str, line: int,
35                 parent: Optional['QAPISourceInfo']):
36        self.fname = fname
37        self.line = line
38        self.parent = parent
39        self.pragma: QAPISchemaPragma = (
40            parent.pragma if parent else QAPISchemaPragma()
41        )
42        self.defn_meta: Optional[str] = None
43        self.defn_name: Optional[str] = None
44
45    def set_defn(self, meta: str, name: str) -> None:
46        self.defn_meta = meta
47        self.defn_name = name
48
49    def next_line(self: T) -> T:
50        info = copy.copy(self)
51        info.line += 1
52        return info
53
54    def loc(self) -> str:
55        ret = self.fname
56        if self.line is not None:
57            ret += ':%d' % self.line
58        return ret
59
60    def in_defn(self) -> str:
61        if self.defn_name:
62            return "%s: In %s '%s':\n" % (self.fname,
63                                          self.defn_meta, self.defn_name)
64        return ''
65
66    def include_path(self) -> str:
67        ret = ''
68        parent = self.parent
69        while parent:
70            ret = 'In file included from %s:\n' % parent.loc() + ret
71            parent = parent.parent
72        return ret
73
74    def __str__(self) -> str:
75        return self.include_path() + self.in_defn() + self.loc()
76