qapi_domain.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. """
  2. QAPI domain extension.
  3. """
  4. # The best laid plans of mice and men, ...
  5. # pylint: disable=too-many-lines
  6. from __future__ import annotations
  7. import re
  8. from typing import (
  9. TYPE_CHECKING,
  10. List,
  11. NamedTuple,
  12. Tuple,
  13. cast,
  14. )
  15. from docutils import nodes
  16. from docutils.parsers.rst import directives
  17. from compat import (
  18. CompatField,
  19. CompatGroupedField,
  20. CompatTypedField,
  21. KeywordNode,
  22. ParserFix,
  23. Signature,
  24. SpaceNode,
  25. )
  26. from sphinx import addnodes
  27. from sphinx.directives import ObjectDescription
  28. from sphinx.domains import (
  29. Domain,
  30. Index,
  31. IndexEntry,
  32. ObjType,
  33. )
  34. from sphinx.locale import _, __
  35. from sphinx.roles import XRefRole
  36. from sphinx.util import logging
  37. from sphinx.util.docutils import SphinxDirective
  38. from sphinx.util.nodes import make_id, make_refnode
  39. if TYPE_CHECKING:
  40. from typing import (
  41. AbstractSet,
  42. Any,
  43. Dict,
  44. Iterable,
  45. Optional,
  46. Union,
  47. )
  48. from docutils.nodes import Element, Node
  49. from sphinx.addnodes import desc_signature, pending_xref
  50. from sphinx.application import Sphinx
  51. from sphinx.builders import Builder
  52. from sphinx.environment import BuildEnvironment
  53. from sphinx.util.typing import OptionSpec
  54. logger = logging.getLogger(__name__)
  55. def _unpack_field(
  56. field: nodes.Node,
  57. ) -> Tuple[nodes.field_name, nodes.field_body]:
  58. """
  59. docutils helper: unpack a field node in a type-safe manner.
  60. """
  61. assert isinstance(field, nodes.field)
  62. assert len(field.children) == 2
  63. assert isinstance(field.children[0], nodes.field_name)
  64. assert isinstance(field.children[1], nodes.field_body)
  65. return (field.children[0], field.children[1])
  66. class ObjectEntry(NamedTuple):
  67. docname: str
  68. node_id: str
  69. objtype: str
  70. aliased: bool
  71. class QAPIXRefRole(XRefRole):
  72. def process_link(
  73. self,
  74. env: BuildEnvironment,
  75. refnode: Element,
  76. has_explicit_title: bool,
  77. title: str,
  78. target: str,
  79. ) -> tuple[str, str]:
  80. refnode["qapi:namespace"] = env.ref_context.get("qapi:namespace")
  81. refnode["qapi:module"] = env.ref_context.get("qapi:module")
  82. # Cross-references that begin with a tilde adjust the title to
  83. # only show the reference without a leading module, even if one
  84. # was provided. This is a Sphinx-standard syntax; give it
  85. # priority over QAPI-specific type markup below.
  86. hide_module = False
  87. if target.startswith("~"):
  88. hide_module = True
  89. target = target[1:]
  90. # Type names that end with "?" are considered optional
  91. # arguments and should be documented as such, but it's not
  92. # part of the xref itself.
  93. if target.endswith("?"):
  94. refnode["qapi:optional"] = True
  95. target = target[:-1]
  96. # Type names wrapped in brackets denote lists. strip the
  97. # brackets and remember to add them back later.
  98. if target.startswith("[") and target.endswith("]"):
  99. refnode["qapi:array"] = True
  100. target = target[1:-1]
  101. if has_explicit_title:
  102. # Don't mess with the title at all if it was explicitly set.
  103. # Explicit title syntax for references is e.g.
  104. # :qapi:type:`target <explicit title>`
  105. # and this explicit title overrides everything else here.
  106. return title, target
  107. title = target
  108. if hide_module:
  109. title = target.split(".")[-1]
  110. return title, target
  111. def result_nodes(
  112. self,
  113. document: nodes.document,
  114. env: BuildEnvironment,
  115. node: Element,
  116. is_ref: bool,
  117. ) -> Tuple[List[nodes.Node], List[nodes.system_message]]:
  118. # node here is the pending_xref node (or whatever nodeclass was
  119. # configured at XRefRole class instantiation time).
  120. results: List[nodes.Node] = [node]
  121. if node.get("qapi:array"):
  122. results.insert(0, nodes.literal("[", "["))
  123. results.append(nodes.literal("]", "]"))
  124. if node.get("qapi:optional"):
  125. results.append(nodes.Text(", "))
  126. results.append(nodes.emphasis("?", "optional"))
  127. return results, []
  128. class QAPIDescription(ParserFix):
  129. """
  130. Generic QAPI description.
  131. This is meant to be an abstract class, not instantiated
  132. directly. This class handles the abstract details of indexing, the
  133. TOC, and reference targets for QAPI descriptions.
  134. """
  135. def handle_signature(self, sig: str, signode: desc_signature) -> Signature:
  136. # Do nothing. The return value here is the "name" of the entity
  137. # being documented; for QAPI, this is the same as the
  138. # "signature", which is just a name.
  139. # Normally this method must also populate signode with nodes to
  140. # render the signature; here we do nothing instead - the
  141. # subclasses will handle this.
  142. return sig
  143. def get_index_text(self, name: Signature) -> Tuple[str, str]:
  144. """Return the text for the index entry of the object."""
  145. # NB: this is used for the global index, not the QAPI index.
  146. return ("single", f"{name} (QMP {self.objtype})")
  147. def _get_context(self) -> Tuple[str, str]:
  148. namespace = self.options.get(
  149. "namespace", self.env.ref_context.get("qapi:namespace", "")
  150. )
  151. modname = self.options.get(
  152. "module", self.env.ref_context.get("qapi:module", "")
  153. )
  154. return namespace, modname
  155. def _get_fqn(self, name: Signature) -> str:
  156. namespace, modname = self._get_context()
  157. # If we're documenting a module, don't include the module as
  158. # part of the FQN; we ARE the module!
  159. if self.objtype == "module":
  160. modname = ""
  161. if modname:
  162. name = f"{modname}.{name}"
  163. if namespace:
  164. name = f"{namespace}:{name}"
  165. return name
  166. def add_target_and_index(
  167. self, name: Signature, sig: str, signode: desc_signature
  168. ) -> None:
  169. # name is the return value of handle_signature.
  170. # sig is the original, raw text argument to handle_signature.
  171. # For QAPI, these are identical, currently.
  172. assert self.objtype
  173. if not (fullname := signode.get("fullname", "")):
  174. fullname = self._get_fqn(name)
  175. node_id = make_id(
  176. self.env, self.state.document, self.objtype, fullname
  177. )
  178. signode["ids"].append(node_id)
  179. self.state.document.note_explicit_target(signode)
  180. domain = cast(QAPIDomain, self.env.get_domain("qapi"))
  181. domain.note_object(fullname, self.objtype, node_id, location=signode)
  182. if "no-index-entry" not in self.options:
  183. arity, indextext = self.get_index_text(name)
  184. assert self.indexnode is not None
  185. if indextext:
  186. self.indexnode["entries"].append(
  187. (arity, indextext, node_id, "", None)
  188. )
  189. @staticmethod
  190. def split_fqn(name: str) -> Tuple[str, str, str]:
  191. if ":" in name:
  192. ns, name = name.split(":")
  193. else:
  194. ns = ""
  195. if "." in name:
  196. module, name = name.split(".")
  197. else:
  198. module = ""
  199. return (ns, module, name)
  200. def _object_hierarchy_parts(
  201. self, sig_node: desc_signature
  202. ) -> Tuple[str, ...]:
  203. if "fullname" not in sig_node:
  204. return ()
  205. return self.split_fqn(sig_node["fullname"])
  206. def _toc_entry_name(self, sig_node: desc_signature) -> str:
  207. # This controls the name in the TOC and on the sidebar.
  208. # This is the return type of _object_hierarchy_parts().
  209. toc_parts = cast(Tuple[str, ...], sig_node.get("_toc_parts", ()))
  210. if not toc_parts:
  211. return ""
  212. config = self.env.app.config
  213. namespace, modname, name = toc_parts
  214. if config.toc_object_entries_show_parents == "domain":
  215. ret = name
  216. if modname and modname != self.env.ref_context.get(
  217. "qapi:module", ""
  218. ):
  219. ret = f"{modname}.{name}"
  220. if namespace and namespace != self.env.ref_context.get(
  221. "qapi:namespace", ""
  222. ):
  223. ret = f"{namespace}:{ret}"
  224. return ret
  225. if config.toc_object_entries_show_parents == "hide":
  226. return name
  227. if config.toc_object_entries_show_parents == "all":
  228. return sig_node.get("fullname", name)
  229. return ""
  230. class QAPIObject(QAPIDescription):
  231. """
  232. Description of a generic QAPI object.
  233. It's not used directly, but is instead subclassed by specific directives.
  234. """
  235. # Inherit some standard options from Sphinx's ObjectDescription
  236. option_spec: OptionSpec = ( # type:ignore[misc]
  237. ObjectDescription.option_spec.copy()
  238. )
  239. option_spec.update(
  240. {
  241. # Context overrides:
  242. "namespace": directives.unchanged,
  243. "module": directives.unchanged,
  244. # These are QAPI originals:
  245. "since": directives.unchanged,
  246. "ifcond": directives.unchanged,
  247. "deprecated": directives.flag,
  248. "unstable": directives.flag,
  249. }
  250. )
  251. doc_field_types = [
  252. # :feat name: descr
  253. CompatGroupedField(
  254. "feature",
  255. label=_("Features"),
  256. names=("feat",),
  257. can_collapse=False,
  258. ),
  259. ]
  260. def get_signature_prefix(self) -> List[nodes.Node]:
  261. """Return a prefix to put before the object name in the signature."""
  262. assert self.objtype
  263. return [
  264. KeywordNode("", self.objtype.title()),
  265. SpaceNode(" "),
  266. ]
  267. def get_signature_suffix(self) -> List[nodes.Node]:
  268. """Return a suffix to put after the object name in the signature."""
  269. ret: List[nodes.Node] = []
  270. if "since" in self.options:
  271. ret += [
  272. SpaceNode(" "),
  273. addnodes.desc_sig_element(
  274. "", f"(Since: {self.options['since']})"
  275. ),
  276. ]
  277. return ret
  278. def handle_signature(self, sig: str, signode: desc_signature) -> Signature:
  279. """
  280. Transform a QAPI definition name into RST nodes.
  281. This method was originally intended for handling function
  282. signatures. In the QAPI domain, however, we only pass the
  283. definition name as the directive argument and handle everything
  284. else in the content body with field lists.
  285. As such, the only argument here is "sig", which is just the QAPI
  286. definition name.
  287. """
  288. # No module or domain info allowed in the signature!
  289. assert ":" not in sig
  290. assert "." not in sig
  291. namespace, modname = self._get_context()
  292. signode["fullname"] = self._get_fqn(sig)
  293. signode["namespace"] = namespace
  294. signode["module"] = modname
  295. sig_prefix = self.get_signature_prefix()
  296. if sig_prefix:
  297. signode += addnodes.desc_annotation(
  298. str(sig_prefix), "", *sig_prefix
  299. )
  300. signode += addnodes.desc_name(sig, sig)
  301. signode += self.get_signature_suffix()
  302. return sig
  303. def _add_infopips(self, contentnode: addnodes.desc_content) -> None:
  304. # Add various eye-catches and things that go below the signature
  305. # bar, but precede the user-defined content.
  306. infopips = nodes.container()
  307. infopips.attributes["classes"].append("qapi-infopips")
  308. def _add_pip(
  309. source: str, content: Union[str, List[nodes.Node]], classname: str
  310. ) -> None:
  311. node = nodes.container(source)
  312. if isinstance(content, str):
  313. node.append(nodes.Text(content))
  314. else:
  315. node.extend(content)
  316. node.attributes["classes"].extend(["qapi-infopip", classname])
  317. infopips.append(node)
  318. if "deprecated" in self.options:
  319. _add_pip(
  320. ":deprecated:",
  321. f"This {self.objtype} is deprecated.",
  322. "qapi-deprecated",
  323. )
  324. if "unstable" in self.options:
  325. _add_pip(
  326. ":unstable:",
  327. f"This {self.objtype} is unstable/experimental.",
  328. "qapi-unstable",
  329. )
  330. if self.options.get("ifcond", ""):
  331. ifcond = self.options["ifcond"]
  332. _add_pip(
  333. f":ifcond: {ifcond}",
  334. [
  335. nodes.emphasis("", "Availability"),
  336. nodes.Text(": "),
  337. nodes.literal(ifcond, ifcond),
  338. ],
  339. "qapi-ifcond",
  340. )
  341. if infopips.children:
  342. contentnode.insert(0, infopips)
  343. def _validate_field(self, field: nodes.field) -> None:
  344. """Validate field lists in this QAPI Object Description."""
  345. name, _ = _unpack_field(field)
  346. allowed_fields = set(self.env.app.config.qapi_allowed_fields)
  347. field_label = name.astext()
  348. if field_label in allowed_fields:
  349. # Explicitly allowed field list name, OK.
  350. return
  351. try:
  352. # split into field type and argument (if provided)
  353. # e.g. `:arg type name: descr` is
  354. # field_type = "arg", field_arg = "type name".
  355. field_type, field_arg = field_label.split(None, 1)
  356. except ValueError:
  357. # No arguments provided
  358. field_type = field_label
  359. field_arg = ""
  360. typemap = self.get_field_type_map()
  361. if field_type in typemap:
  362. # This is a special docfield, yet-to-be-processed. Catch
  363. # correct names, but incorrect arguments. This mismatch WILL
  364. # cause Sphinx to render this field incorrectly (without a
  365. # warning), which is never what we want.
  366. typedesc = typemap[field_type][0]
  367. if typedesc.has_arg != bool(field_arg):
  368. msg = f"docfield field list type {field_type!r} "
  369. if typedesc.has_arg:
  370. msg += "requires an argument."
  371. else:
  372. msg += "takes no arguments."
  373. logger.warning(msg, location=field)
  374. else:
  375. # This is unrecognized entirely. It's valid rST to use
  376. # arbitrary fields, but let's ensure the documentation
  377. # writer has done this intentionally.
  378. valid = ", ".join(sorted(set(typemap) | allowed_fields))
  379. msg = (
  380. f"Unrecognized field list name {field_label!r}.\n"
  381. f"Valid fields for qapi:{self.objtype} are: {valid}\n"
  382. "\n"
  383. "If this usage is intentional, please add it to "
  384. "'qapi_allowed_fields' in docs/conf.py."
  385. )
  386. logger.warning(msg, location=field)
  387. def transform_content(self, content_node: addnodes.desc_content) -> None:
  388. # This hook runs after before_content and the nested parse, but
  389. # before the DocFieldTransformer is executed.
  390. super().transform_content(content_node)
  391. self._add_infopips(content_node)
  392. # Validate field lists.
  393. for child in content_node:
  394. if isinstance(child, nodes.field_list):
  395. for field in child.children:
  396. assert isinstance(field, nodes.field)
  397. self._validate_field(field)
  398. class SpecialTypedField(CompatTypedField):
  399. def make_field(self, *args: Any, **kwargs: Any) -> nodes.field:
  400. ret = super().make_field(*args, **kwargs)
  401. # Look for the characteristic " -- " text node that Sphinx
  402. # inserts for each TypedField entry ...
  403. for node in ret.traverse(lambda n: str(n) == " -- "):
  404. par = node.parent
  405. if par.children[0].astext() != "q_dummy":
  406. continue
  407. # If the first node's text is q_dummy, this is a dummy
  408. # field we want to strip down to just its contents.
  409. del par.children[:-1]
  410. return ret
  411. class QAPICommand(QAPIObject):
  412. """Description of a QAPI Command."""
  413. doc_field_types = QAPIObject.doc_field_types.copy()
  414. doc_field_types.extend(
  415. [
  416. # :arg TypeName ArgName: descr
  417. SpecialTypedField(
  418. "argument",
  419. label=_("Arguments"),
  420. names=("arg",),
  421. typerolename="type",
  422. can_collapse=False,
  423. ),
  424. # :error: descr
  425. CompatField(
  426. "error",
  427. label=_("Errors"),
  428. names=("error", "errors"),
  429. has_arg=False,
  430. ),
  431. # :return TypeName: descr
  432. CompatGroupedField(
  433. "returnvalue",
  434. label=_("Return"),
  435. rolename="type",
  436. names=("return",),
  437. can_collapse=True,
  438. ),
  439. ]
  440. )
  441. class QAPIEnum(QAPIObject):
  442. """Description of a QAPI Enum."""
  443. doc_field_types = QAPIObject.doc_field_types.copy()
  444. doc_field_types.extend(
  445. [
  446. # :value name: descr
  447. CompatGroupedField(
  448. "value",
  449. label=_("Values"),
  450. names=("value",),
  451. can_collapse=False,
  452. )
  453. ]
  454. )
  455. class QAPIAlternate(QAPIObject):
  456. """Description of a QAPI Alternate."""
  457. doc_field_types = QAPIObject.doc_field_types.copy()
  458. doc_field_types.extend(
  459. [
  460. # :alt type name: descr
  461. CompatTypedField(
  462. "alternative",
  463. label=_("Alternatives"),
  464. names=("alt",),
  465. typerolename="type",
  466. can_collapse=False,
  467. ),
  468. ]
  469. )
  470. class QAPIObjectWithMembers(QAPIObject):
  471. """Base class for Events/Structs/Unions"""
  472. doc_field_types = QAPIObject.doc_field_types.copy()
  473. doc_field_types.extend(
  474. [
  475. # :member type name: descr
  476. SpecialTypedField(
  477. "member",
  478. label=_("Members"),
  479. names=("memb",),
  480. typerolename="type",
  481. can_collapse=False,
  482. ),
  483. ]
  484. )
  485. class QAPIEvent(QAPIObjectWithMembers):
  486. # pylint: disable=too-many-ancestors
  487. """Description of a QAPI Event."""
  488. class QAPIJSONObject(QAPIObjectWithMembers):
  489. # pylint: disable=too-many-ancestors
  490. """Description of a QAPI Object: structs and unions."""
  491. class QAPIModule(QAPIDescription):
  492. """
  493. Directive to mark description of a new module.
  494. This directive doesn't generate any special formatting, and is just
  495. a pass-through for the content body. Named section titles are
  496. allowed in the content body.
  497. Use this directive to create entries for the QAPI module in the
  498. global index and the QAPI index; as well as to associate subsequent
  499. definitions with the module they are defined in for purposes of
  500. search and QAPI index organization.
  501. :arg: The name of the module.
  502. :opt no-index: Don't add cross-reference targets or index entries.
  503. :opt no-typesetting: Don't render the content body (but preserve any
  504. cross-reference target IDs in the squelched output.)
  505. Example::
  506. .. qapi:module:: block-core
  507. :no-index:
  508. :no-typesetting:
  509. Lorem ipsum, dolor sit amet ...
  510. """
  511. def run(self) -> List[Node]:
  512. modname = self.arguments[0].strip()
  513. self.env.ref_context["qapi:module"] = modname
  514. ret = super().run()
  515. # ObjectDescription always creates a visible signature bar. We
  516. # want module items to be "invisible", however.
  517. # Extract the content body of the directive:
  518. assert isinstance(ret[-1], addnodes.desc)
  519. desc_node = ret.pop(-1)
  520. assert isinstance(desc_node.children[1], addnodes.desc_content)
  521. ret.extend(desc_node.children[1].children)
  522. # Re-home node_ids so anchor refs still work:
  523. node_ids: List[str]
  524. if node_ids := [
  525. node_id
  526. for el in desc_node.children[0].traverse(nodes.Element)
  527. for node_id in cast(List[str], el.get("ids", ()))
  528. ]:
  529. target_node = nodes.target(ids=node_ids)
  530. ret.insert(1, target_node)
  531. return ret
  532. class QAPINamespace(SphinxDirective):
  533. has_content = False
  534. required_arguments = 1
  535. def run(self) -> List[Node]:
  536. namespace = self.arguments[0].strip()
  537. self.env.ref_context["qapi:namespace"] = namespace
  538. return []
  539. class QAPIIndex(Index):
  540. """
  541. Index subclass to provide the QAPI definition index.
  542. """
  543. # pylint: disable=too-few-public-methods
  544. name = "index"
  545. localname = _("QAPI Index")
  546. shortname = _("QAPI Index")
  547. def generate(
  548. self,
  549. docnames: Optional[Iterable[str]] = None,
  550. ) -> Tuple[List[Tuple[str, List[IndexEntry]]], bool]:
  551. assert isinstance(self.domain, QAPIDomain)
  552. content: Dict[str, List[IndexEntry]] = {}
  553. collapse = False
  554. # list of all object (name, ObjectEntry) pairs, sorted by name
  555. # (ignoring the module)
  556. objects = sorted(
  557. self.domain.objects.items(),
  558. key=lambda x: x[0].split(".")[-1].lower(),
  559. )
  560. for objname, obj in objects:
  561. if docnames and obj.docname not in docnames:
  562. continue
  563. # Strip the module name out:
  564. objname = objname.split(".")[-1]
  565. # Add an alphabetical entry:
  566. entries = content.setdefault(objname[0].upper(), [])
  567. entries.append(
  568. IndexEntry(
  569. objname, 0, obj.docname, obj.node_id, obj.objtype, "", ""
  570. )
  571. )
  572. # Add a categorical entry:
  573. category = obj.objtype.title() + "s"
  574. entries = content.setdefault(category, [])
  575. entries.append(
  576. IndexEntry(objname, 0, obj.docname, obj.node_id, "", "", "")
  577. )
  578. # alphabetically sort categories; type names first, ABC entries last.
  579. sorted_content = sorted(
  580. content.items(),
  581. key=lambda x: (len(x[0]) == 1, x[0]),
  582. )
  583. return sorted_content, collapse
  584. class QAPIDomain(Domain):
  585. """QAPI language domain."""
  586. name = "qapi"
  587. label = "QAPI"
  588. # This table associates cross-reference object types (key) with an
  589. # ObjType instance, which defines the valid cross-reference roles
  590. # for each object type.
  591. #
  592. # e.g., the :qapi:type: cross-reference role can refer to enum,
  593. # struct, union, or alternate objects; but :qapi:obj: can refer to
  594. # anything. Each object also gets its own targeted cross-reference role.
  595. object_types: Dict[str, ObjType] = {
  596. "module": ObjType(_("module"), "mod", "any"),
  597. "command": ObjType(_("command"), "cmd", "any"),
  598. "event": ObjType(_("event"), "event", "any"),
  599. "enum": ObjType(_("enum"), "enum", "type", "any"),
  600. "object": ObjType(_("object"), "obj", "type", "any"),
  601. "alternate": ObjType(_("alternate"), "alt", "type", "any"),
  602. }
  603. # Each of these provides a rST directive,
  604. # e.g. .. qapi:module:: block-core
  605. directives = {
  606. "namespace": QAPINamespace,
  607. "module": QAPIModule,
  608. "command": QAPICommand,
  609. "event": QAPIEvent,
  610. "enum": QAPIEnum,
  611. "object": QAPIJSONObject,
  612. "alternate": QAPIAlternate,
  613. }
  614. # These are all cross-reference roles; e.g.
  615. # :qapi:cmd:`query-block`. The keys correlate to the names used in
  616. # the object_types table values above.
  617. roles = {
  618. "mod": QAPIXRefRole(),
  619. "cmd": QAPIXRefRole(),
  620. "event": QAPIXRefRole(),
  621. "enum": QAPIXRefRole(),
  622. "obj": QAPIXRefRole(), # specifically structs and unions.
  623. "alt": QAPIXRefRole(),
  624. # reference any data type (excludes modules, commands, events)
  625. "type": QAPIXRefRole(),
  626. "any": QAPIXRefRole(), # reference *any* type of QAPI object.
  627. }
  628. # Moved into the data property at runtime;
  629. # this is the internal index of reference-able objects.
  630. initial_data: Dict[str, Dict[str, Tuple[Any]]] = {
  631. "objects": {}, # fullname -> ObjectEntry
  632. }
  633. # Index pages to generate; each entry is an Index class.
  634. indices = [
  635. QAPIIndex,
  636. ]
  637. @property
  638. def objects(self) -> Dict[str, ObjectEntry]:
  639. ret = self.data.setdefault("objects", {})
  640. return ret # type: ignore[no-any-return]
  641. def note_object(
  642. self,
  643. name: str,
  644. objtype: str,
  645. node_id: str,
  646. aliased: bool = False,
  647. location: Any = None,
  648. ) -> None:
  649. """Note a QAPI object for cross reference."""
  650. if name in self.objects:
  651. other = self.objects[name]
  652. if other.aliased and aliased is False:
  653. # The original definition found. Override it!
  654. pass
  655. elif other.aliased is False and aliased:
  656. # The original definition is already registered.
  657. return
  658. else:
  659. # duplicated
  660. logger.warning(
  661. __(
  662. "duplicate object description of %s, "
  663. "other instance in %s, use :no-index: for one of them"
  664. ),
  665. name,
  666. other.docname,
  667. location=location,
  668. )
  669. self.objects[name] = ObjectEntry(
  670. self.env.docname, node_id, objtype, aliased
  671. )
  672. def clear_doc(self, docname: str) -> None:
  673. for fullname, obj in list(self.objects.items()):
  674. if obj.docname == docname:
  675. del self.objects[fullname]
  676. def merge_domaindata(
  677. self, docnames: AbstractSet[str], otherdata: Dict[str, Any]
  678. ) -> None:
  679. for fullname, obj in otherdata["objects"].items():
  680. if obj.docname in docnames:
  681. # Sphinx's own python domain doesn't appear to bother to
  682. # check for collisions. Assert they don't happen and
  683. # we'll fix it if/when the case arises.
  684. assert fullname not in self.objects, (
  685. "bug - collision on merge?"
  686. f" {fullname=} {obj=} {self.objects[fullname]=}"
  687. )
  688. self.objects[fullname] = obj
  689. def find_obj(
  690. self, namespace: str, modname: str, name: str, typ: Optional[str]
  691. ) -> List[Tuple[str, ObjectEntry]]:
  692. """
  693. Find a QAPI object for "name", maybe using contextual information.
  694. Returns a list of (name, object entry) tuples.
  695. :param namespace: The current namespace context (if any!) under
  696. which we are searching.
  697. :param modname: The current module context (if any!) under
  698. which we are searching.
  699. :param name: The name of the x-ref to resolve; may or may not
  700. include leading context.
  701. :param type: The role name of the x-ref we're resolving, if
  702. provided. This is absent for "any" role lookups.
  703. """
  704. if not name:
  705. return []
  706. # ##
  707. # what to search for
  708. # ##
  709. parts = list(QAPIDescription.split_fqn(name))
  710. explicit = tuple(bool(x) for x in parts)
  711. # Fill in the blanks where possible:
  712. if namespace and not parts[0]:
  713. parts[0] = namespace
  714. if modname and not parts[1]:
  715. parts[1] = modname
  716. implicit_fqn = ""
  717. if all(parts):
  718. implicit_fqn = f"{parts[0]}:{parts[1]}.{parts[2]}"
  719. if typ is None:
  720. # :any: lookup, search everything:
  721. objtypes: List[str] = list(self.object_types)
  722. else:
  723. # type is specified and will be a role (e.g. obj, mod, cmd)
  724. # convert this to eligible object types (e.g. command, module)
  725. # using the QAPIDomain.object_types table.
  726. objtypes = self.objtypes_for_role(typ, [])
  727. # ##
  728. # search!
  729. # ##
  730. def _search(needle: str) -> List[str]:
  731. if (
  732. needle
  733. and needle in self.objects
  734. and self.objects[needle].objtype in objtypes
  735. ):
  736. return [needle]
  737. return []
  738. if found := _search(name):
  739. # Exact match!
  740. pass
  741. elif found := _search(implicit_fqn):
  742. # Exact match using contextual information to fill in the gaps.
  743. pass
  744. else:
  745. # No exact hits, perform applicable fuzzy searches.
  746. searches = []
  747. esc = tuple(re.escape(s) for s in parts)
  748. # Try searching for ns:*.name or ns:name
  749. if explicit[0] and not explicit[1]:
  750. searches.append(f"^{esc[0]}:([^\\.]+\\.)?{esc[2]}$")
  751. # Try searching for *:module.name or module.name
  752. if explicit[1] and not explicit[0]:
  753. searches.append(f"(^|:){esc[1]}\\.{esc[2]}$")
  754. # Try searching for context-ns:*.name or context-ns:name
  755. if parts[0] and not (explicit[0] or explicit[1]):
  756. searches.append(f"^{esc[0]}:([^\\.]+\\.)?{esc[2]}$")
  757. # Try searching for *:context-mod.name or context-mod.name
  758. if parts[1] and not (explicit[0] or explicit[1]):
  759. searches.append(f"(^|:){esc[1]}\\.{esc[2]}$")
  760. # Try searching for *:name, *.name, or name
  761. if not (explicit[0] or explicit[1]):
  762. searches.append(f"(^|:|\\.){esc[2]}$")
  763. for search in searches:
  764. if found := [
  765. oname
  766. for oname in self.objects
  767. if re.search(search, oname)
  768. and self.objects[oname].objtype in objtypes
  769. ]:
  770. break
  771. matches = [(oname, self.objects[oname]) for oname in found]
  772. if len(matches) > 1:
  773. matches = [m for m in matches if not m[1].aliased]
  774. return matches
  775. def resolve_xref(
  776. self,
  777. env: BuildEnvironment,
  778. fromdocname: str,
  779. builder: Builder,
  780. typ: str,
  781. target: str,
  782. node: pending_xref,
  783. contnode: Element,
  784. ) -> nodes.reference | None:
  785. namespace = node.get("qapi:namespace")
  786. modname = node.get("qapi:module")
  787. matches = self.find_obj(namespace, modname, target, typ)
  788. if not matches:
  789. # Normally, we could pass warn_dangling=True to QAPIXRefRole(),
  790. # but that will trigger on references to these built-in types,
  791. # which we'd like to ignore instead.
  792. # Take care of that warning here instead, so long as the
  793. # reference isn't to one of our built-in core types.
  794. if target not in (
  795. "string",
  796. "number",
  797. "int",
  798. "boolean",
  799. "null",
  800. "value",
  801. "q_empty",
  802. ):
  803. logger.warning(
  804. __("qapi:%s reference target not found: %r"),
  805. typ,
  806. target,
  807. type="ref",
  808. subtype="qapi",
  809. location=node,
  810. )
  811. return None
  812. if len(matches) > 1:
  813. logger.warning(
  814. __("more than one target found for cross-reference %r: %s"),
  815. target,
  816. ", ".join(match[0] for match in matches),
  817. type="ref",
  818. subtype="qapi",
  819. location=node,
  820. )
  821. name, obj = matches[0]
  822. return make_refnode(
  823. builder, fromdocname, obj.docname, obj.node_id, contnode, name
  824. )
  825. def resolve_any_xref(
  826. self,
  827. env: BuildEnvironment,
  828. fromdocname: str,
  829. builder: Builder,
  830. target: str,
  831. node: pending_xref,
  832. contnode: Element,
  833. ) -> List[Tuple[str, nodes.reference]]:
  834. results: List[Tuple[str, nodes.reference]] = []
  835. matches = self.find_obj(
  836. node.get("qapi:namespace"), node.get("qapi:module"), target, None
  837. )
  838. for name, obj in matches:
  839. rolename = self.role_for_objtype(obj.objtype)
  840. assert rolename is not None
  841. role = f"qapi:{rolename}"
  842. refnode = make_refnode(
  843. builder, fromdocname, obj.docname, obj.node_id, contnode, name
  844. )
  845. results.append((role, refnode))
  846. return results
  847. def setup(app: Sphinx) -> Dict[str, Any]:
  848. app.setup_extension("sphinx.directives")
  849. app.add_config_value(
  850. "qapi_allowed_fields",
  851. set(),
  852. "env", # Setting impacts parsing phase
  853. types=set,
  854. )
  855. app.add_domain(QAPIDomain)
  856. return {
  857. "version": "1.0",
  858. "env_version": 1,
  859. "parallel_read_safe": True,
  860. "parallel_write_safe": True,
  861. }