| 1 |
.. highlight:: rst |
| 2 |
|
| 3 |
============ |
| 4 |
The C Domain |
| 5 |
============ |
| 6 |
|
| 7 |
.. versionadded:: 1.0 |
| 8 |
|
| 9 |
The C domain (name **c**) is suited for documentation of C API. |
| 10 |
|
| 11 |
.. rst:directive:: .. c:member:: declaration |
| 12 |
.. c:var:: declaration |
| 13 |
|
| 14 |
Describes a C struct member or variable. Example signature:: |
| 15 |
|
| 16 |
.. c:member:: PyObject *PyTypeObject.tp_bases |
| 17 |
|
| 18 |
The difference between the two directives is only cosmetic. |
| 19 |
|
| 20 |
.. rst:directive:: .. c:function:: function prototype |
| 21 |
|
| 22 |
Describes a C function. The signature should be given as in C, e.g.:: |
| 23 |
|
| 24 |
.. c:function:: PyObject *PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
| 25 |
|
| 26 |
Note that you don't have to backslash-escape asterisks in the signature, as |
| 27 |
it is not parsed by the reStructuredText inliner. |
| 28 |
|
| 29 |
In the description of a function you can use the following info fields |
| 30 |
(see also :ref:`info-field-lists`). |
| 31 |
|
| 32 |
* ``param``, ``parameter``, ``arg``, ``argument``, |
| 33 |
Description of a parameter. |
| 34 |
* ``type``: Type of a parameter, |
| 35 |
written as if passed to the :rst:role:`c:expr` role. |
| 36 |
* ``returns``, ``return``: Description of the return value. |
| 37 |
* ``rtype``: Return type, |
| 38 |
written as if passed to the :rst:role:`c:expr` role. |
| 39 |
* ``retval``, ``retvals``: An alternative to ``returns`` for describing |
| 40 |
the result of the function. |
| 41 |
|
| 42 |
.. versionadded:: 4.3 |
| 43 |
The ``retval`` field type. |
| 44 |
|
| 45 |
For example:: |
| 46 |
|
| 47 |
.. c:function:: PyObject *PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
| 48 |
|
| 49 |
:param type: description of the first parameter. |
| 50 |
:param nitems: description of the second parameter. |
| 51 |
:returns: a result. |
| 52 |
:retval NULL: under some conditions. |
| 53 |
:retval NULL: under some other conditions as well. |
| 54 |
|
| 55 |
which renders as |
| 56 |
|
| 57 |
.. c:function:: PyObject *PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
| 58 |
|
| 59 |
.. |
| 60 |
** for some editors (e.g., vim) to stop bold-highlighting the source |
| 61 |
|
| 62 |
:no-contents-entry: |
| 63 |
:no-index-entry: |
| 64 |
:param type: description of the first parameter. |
| 65 |
:param nitems: description of the second parameter. |
| 66 |
:returns: a result. |
| 67 |
:retval NULL: under some conditions. |
| 68 |
:retval NULL: under some other conditions as well. |
| 69 |
|
| 70 |
.. rst:directive:option:: single-line-parameter-list |
| 71 |
:type: no value |
| 72 |
|
| 73 |
Ensures that the function's parameters will be emitted on a single logical |
| 74 |
line, overriding :confval:`c_maximum_signature_line_length` and |
| 75 |
:confval:`maximum_signature_line_length`. |
| 76 |
|
| 77 |
.. versionadded:: 7.1 |
| 78 |
|
| 79 |
|
| 80 |
.. rst:directive:: .. c:macro:: name |
| 81 |
.. c:macro:: name(arg list) |
| 82 |
|
| 83 |
Describes a C macro, i.e., a C-language ``#define``, without the replacement |
| 84 |
text. |
| 85 |
|
| 86 |
In the description of a macro you can use the same info fields as for the |
| 87 |
:rst:dir:`c:function` directive. |
| 88 |
|
| 89 |
.. versionadded:: 3.0 |
| 90 |
The function style variant. |
| 91 |
|
| 92 |
.. rst:directive:option:: single-line-parameter-list |
| 93 |
:type: no value |
| 94 |
|
| 95 |
Ensures that the macro's parameters will be emitted on a single logical |
| 96 |
line, overriding :confval:`c_maximum_signature_line_length` and |
| 97 |
:confval:`maximum_signature_line_length`. |
| 98 |
|
| 99 |
.. versionadded:: 7.1 |
| 100 |
|
| 101 |
.. rst:directive:: .. c:struct:: name |
| 102 |
|
| 103 |
Describes a C struct. |
| 104 |
|
| 105 |
.. versionadded:: 3.0 |
| 106 |
|
| 107 |
.. rst:directive:: .. c:union:: name |
| 108 |
|
| 109 |
Describes a C union. |
| 110 |
|
| 111 |
.. versionadded:: 3.0 |
| 112 |
|
| 113 |
.. rst:directive:: .. c:enum:: name |
| 114 |
|
| 115 |
Describes a C enum. |
| 116 |
|
| 117 |
.. versionadded:: 3.0 |
| 118 |
|
| 119 |
.. rst:directive:: .. c:enumerator:: name |
| 120 |
|
| 121 |
Describes a C enumerator. |
| 122 |
|
| 123 |
.. versionadded:: 3.0 |
| 124 |
|
| 125 |
.. rst:directive:: .. c:type:: typedef-like declaration |
| 126 |
.. c:type:: name |
| 127 |
|
| 128 |
Describes a C type, either as a typedef, or the alias for an unspecified |
| 129 |
type. |
| 130 |
|
| 131 |
.. _c-xref-roles: |
| 132 |
|
| 133 |
Cross-referencing C constructs |
| 134 |
------------------------------ |
| 135 |
|
| 136 |
The following roles create cross-references to C-language constructs if they |
| 137 |
are defined in the documentation: |
| 138 |
|
| 139 |
.. rst:role:: c:member |
| 140 |
c:data |
| 141 |
c:var |
| 142 |
c:func |
| 143 |
c:macro |
| 144 |
c:struct |
| 145 |
c:union |
| 146 |
c:enum |
| 147 |
c:enumerator |
| 148 |
c:type |
| 149 |
|
| 150 |
Reference a C declaration, as defined above. |
| 151 |
Note that :rst:role:`c:member`, :rst:role:`c:data`, and |
| 152 |
:rst:role:`c:var` are equivalent. |
| 153 |
|
| 154 |
.. versionadded:: 3.0 |
| 155 |
The var, struct, union, enum, and enumerator roles. |
| 156 |
|
| 157 |
|
| 158 |
Anonymous Entities |
| 159 |
------------------ |
| 160 |
|
| 161 |
C supports anonymous structs, enums, and unions. |
| 162 |
For the sake of documentation they must be given some name that starts with |
| 163 |
``@``, e.g., ``@42`` or ``@data``. |
| 164 |
These names can also be used in cross-references, |
| 165 |
though nested symbols will be found even when omitted. |
| 166 |
The ``@...`` name will always be rendered as **[anonymous]** (possibly as a |
| 167 |
link). |
| 168 |
|
| 169 |
Example:: |
| 170 |
|
| 171 |
.. c:struct:: Data |
| 172 |
|
| 173 |
.. c:union:: @data |
| 174 |
|
| 175 |
.. c:var:: int a |
| 176 |
|
| 177 |
.. c:var:: double b |
| 178 |
|
| 179 |
Explicit ref: :c:var:`[email protected]`. Short-hand ref: :c:var:`Data.a`. |
| 180 |
|
| 181 |
This will be rendered as: |
| 182 |
|
| 183 |
.. c:struct:: Data |
| 184 |
:no-contents-entry: |
| 185 |
:no-index-entry: |
| 186 |
|
| 187 |
.. c:union:: @data |
| 188 |
:no-contents-entry: |
| 189 |
:no-index-entry: |
| 190 |
|
| 191 |
.. c:var:: int a |
| 192 |
:no-contents-entry: |
| 193 |
:no-index-entry: |
| 194 |
|
| 195 |
.. c:var:: double b |
| 196 |
:no-contents-entry: |
| 197 |
:no-index-entry: |
| 198 |
|
| 199 |
Explicit ref: :c:var:`[email protected]`. Short-hand ref: :c:var:`Data.a`. |
| 200 |
|
| 201 |
.. versionadded:: 3.0 |
| 202 |
|
| 203 |
|
| 204 |
Aliasing Declarations |
| 205 |
--------------------- |
| 206 |
|
| 207 |
.. c:namespace-push:: @alias |
| 208 |
|
| 209 |
Sometimes it may be helpful list declarations elsewhere than their main |
| 210 |
documentation, e.g., when creating a synopsis of an interface. |
| 211 |
The following directive can be used for this purpose. |
| 212 |
|
| 213 |
.. rst:directive:: .. c:alias:: name |
| 214 |
|
| 215 |
Insert one or more alias declarations. Each entity can be specified |
| 216 |
as they can in the :rst:role:`c:any` role. |
| 217 |
|
| 218 |
For example:: |
| 219 |
|
| 220 |
.. c:var:: int data |
| 221 |
.. c:function:: int f(double k) |
| 222 |
|
| 223 |
.. c:alias:: data |
| 224 |
f |
| 225 |
|
| 226 |
becomes |
| 227 |
|
| 228 |
.. c:var:: int data |
| 229 |
:no-contents-entry: |
| 230 |
:no-index-entry: |
| 231 |
|
| 232 |
.. c:function:: int f(double k) |
| 233 |
:no-contents-entry: |
| 234 |
:no-index-entry: |
| 235 |
|
| 236 |
.. c:alias:: data |
| 237 |
f |
| 238 |
|
| 239 |
.. versionadded:: 3.2 |
| 240 |
|
| 241 |
|
| 242 |
.. rubric:: Options |
| 243 |
|
| 244 |
.. rst:directive:option:: maxdepth: int |
| 245 |
|
| 246 |
Insert nested declarations as well, up to the total depth given. |
| 247 |
Use 0 for infinite depth and 1 for just the mentioned declaration. |
| 248 |
Defaults to 1. |
| 249 |
|
| 250 |
.. versionadded:: 3.3 |
| 251 |
|
| 252 |
.. rst:directive:option:: noroot |
| 253 |
|
| 254 |
Skip the mentioned declarations and only render nested declarations. |
| 255 |
Requires ``maxdepth`` either 0 or at least 2. |
| 256 |
|
| 257 |
.. versionadded:: 3.5 |
| 258 |
|
| 259 |
|
| 260 |
.. c:namespace-pop:: |
| 261 |
|
| 262 |
|
| 263 |
Inline Expressions and Types |
| 264 |
---------------------------- |
| 265 |
|
| 266 |
.. rst:role:: c:expr |
| 267 |
c:texpr |
| 268 |
|
| 269 |
Insert a C expression or type either as inline code (``cpp:expr``) |
| 270 |
or inline text (``cpp:texpr``). For example:: |
| 271 |
|
| 272 |
.. c:var:: int a = 42 |
| 273 |
|
| 274 |
.. c:function:: int f(int i) |
| 275 |
|
| 276 |
An expression: :c:expr:`a * f(a)` (or as text: :c:texpr:`a * f(a)`). |
| 277 |
|
| 278 |
A type: :c:expr:`const Data*` |
| 279 |
(or as text :c:texpr:`const Data*`). |
| 280 |
|
| 281 |
will be rendered as follows: |
| 282 |
|
| 283 |
.. c:var:: int a = 42 |
| 284 |
:no-contents-entry: |
| 285 |
:no-index-entry: |
| 286 |
|
| 287 |
.. c:function:: int f(int i) |
| 288 |
:no-contents-entry: |
| 289 |
:no-index-entry: |
| 290 |
|
| 291 |
An expression: :c:expr:`a * f(a)` (or as text: :c:texpr:`a * f(a)`). |
| 292 |
|
| 293 |
A type: :c:expr:`const Data*` |
| 294 |
(or as text :c:texpr:`const Data*`). |
| 295 |
|
| 296 |
.. versionadded:: 3.0 |
| 297 |
|
| 298 |
|
| 299 |
Namespacing |
| 300 |
----------- |
| 301 |
|
| 302 |
.. versionadded:: 3.1 |
| 303 |
|
| 304 |
The C language it self does not support namespacing, but it can sometimes be |
| 305 |
useful to emulate it in documentation, e.g., to show alternate declarations. |
| 306 |
The feature may also be used to document members of structs/unions/enums |
| 307 |
separate from their parent declaration. |
| 308 |
|
| 309 |
The current scope can be changed using three namespace directives. They manage |
| 310 |
a stack declarations where ``c:namespace`` resets the stack and changes a given |
| 311 |
scope. |
| 312 |
|
| 313 |
The ``c:namespace-push`` directive changes the scope to a given inner scope |
| 314 |
of the current one. |
| 315 |
|
| 316 |
The ``c:namespace-pop`` directive undoes the most recent |
| 317 |
``c:namespace-push`` directive. |
| 318 |
|
| 319 |
.. rst:directive:: .. c:namespace:: scope specification |
| 320 |
|
| 321 |
Changes the current scope for the subsequent objects to the given scope, and |
| 322 |
resets the namespace directive stack. Note that nested scopes can be |
| 323 |
specified by separating with a dot, e.g.:: |
| 324 |
|
| 325 |
.. c:namespace:: Namespace1.Namespace2.SomeStruct.AnInnerStruct |
| 326 |
|
| 327 |
All subsequent objects will be defined as if their name were declared with |
| 328 |
the scope prepended. The subsequent cross-references will be searched for |
| 329 |
starting in the current scope. |
| 330 |
|
| 331 |
Using ``NULL`` or ``0`` as the scope will change to global scope. |
| 332 |
|
| 333 |
.. rst:directive:: .. c:namespace-push:: scope specification |
| 334 |
|
| 335 |
Change the scope relatively to the current scope. For example, after:: |
| 336 |
|
| 337 |
.. c:namespace:: A.B |
| 338 |
|
| 339 |
.. c:namespace-push:: C.D |
| 340 |
|
| 341 |
the current scope will be ``A.B.C.D``. |
| 342 |
|
| 343 |
.. rst:directive:: .. c:namespace-pop:: |
| 344 |
|
| 345 |
Undo the previous ``c:namespace-push`` directive (*not* just pop a scope). |
| 346 |
For example, after:: |
| 347 |
|
| 348 |
.. c:namespace:: A.B |
| 349 |
|
| 350 |
.. c:namespace-push:: C.D |
| 351 |
|
| 352 |
.. c:namespace-pop:: |
| 353 |
|
| 354 |
the current scope will be ``A.B`` (*not* ``A.B.C``). |
| 355 |
|
| 356 |
If no previous ``c:namespace-push`` directive has been used, but only a |
| 357 |
``c:namespace`` directive, then the current scope will be reset to global |
| 358 |
scope. That is, ``.. c:namespace:: A.B`` is equivalent to:: |
| 359 |
|
| 360 |
.. c:namespace:: NULL |
| 361 |
|
| 362 |
.. c:namespace-push:: A.B |
| 363 |
|
| 364 |
Configuration Variables |
| 365 |
----------------------- |
| 366 |
|
| 367 |
See :ref:`c-config`. |