Skip to content

Commit cdde4df

Browse files
committed
Merge pull request #8694 from JuliaLang/cjh/link-doc
WIP: Cross-reference stdlib from manual
2 parents 7fdc860 + 1698a9d commit cdde4df

7 files changed

+401
-355
lines changed

doc/manual/calling-c-and-fortran-code.rst

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.. _man-calling-c-and-fortran-code:
22

3+
.. currentmodule:: Base
4+
35
****************************
46
Calling C and Fortran Code
57
****************************
@@ -11,7 +13,7 @@ and efficient to call C and Fortran functions. Julia has a "no
1113
boilerplate" philosophy: functions can be called directly from Julia
1214
without any "glue" code, code generation, or compilation — even from the
1315
interactive prompt. This is accomplished just by making an appropriate call
14-
with ``ccall`` syntax, which looks like an ordinary function call.
16+
with :func:`ccall` syntax, which looks like an ordinary function call.
1517

1618
The code to be called must be available as a shared library. Most C and
1719
Fortran libraries ship compiled as shared libraries already, but if you
@@ -38,10 +40,10 @@ the current process. This form can be used to call C library functions,
3840
functions in the Julia runtime, or functions in an application linked to
3941
Julia.
4042

41-
Finally, you can use ``ccall`` to actually generate a call to the
42-
library function. Arguments to ``ccall`` are as follows:
43+
Finally, you can use :func:`ccall` to actually generate a call to the
44+
library function. Arguments to :func:`ccall` are as follows:
4345

44-
1. (:function, "library") pair (must be a constant, but see below).
46+
1. ``(:function, "library")`` pair (must be a constant, but see below).
4547
2. Return type, which may be any bits type, including ``Int32``,
4648
``Int64``, ``Float64``, or ``Ptr{T}`` for any type parameter ``T``,
4749
indicating a pointer to values of type ``T``, or ``Ptr{Void}`` for
@@ -86,7 +88,7 @@ rather than ``(Ptr{Uint8})``. This is because ``(Ptr{Uint8})`` is just
8688
(Ptr{Uint8},)
8789

8890
In practice, especially when providing reusable functionality, one
89-
generally wraps ``ccall`` uses in Julia functions that set up arguments
91+
generally wraps :func:`ccall` uses in Julia functions that set up arguments
9092
and then check for errors in whatever manner the C or Fortran function
9193
indicates them, propagating to the Julia caller as exceptions. This is
9294
especially important since C and Fortran APIs are notoriously
@@ -164,9 +166,9 @@ a real address operator, it may be used with any syntax, such as
164166
``&0`` or ``&f(x)``.
165167

166168
Note that no C header files are used anywhere in the process. Currently,
167-
it is not possible to pass structs and other non-primitive types from
169+
it is not possible to pass ``struct``\ s and other non-primitive types from
168170
Julia to C libraries. However, C functions that generate and use opaque
169-
struct types by passing pointers to them can return such values
171+
``struct`` types by passing pointers to them can return such values
170172
to Julia as ``Ptr{Void}``, which can then be passed to other C functions
171173
as ``Ptr{Void}``. Memory allocation and deallocation of such objects
172174
must be handled by calls to the appropriate cleanup routines in the
@@ -175,7 +177,7 @@ libraries being used, just like in any C program.
175177
Mapping C Types to Julia
176178
------------------------
177179

178-
Julia automatically inserts calls to the ``convert`` function to convert
180+
Julia automatically inserts calls to the :func:`convert` function to convert
179181
each argument to the specified type. For example, the following call::
180182

181183
ccall( (:foo, "libfoo"), Void, (Int32, Float64),
@@ -198,7 +200,7 @@ array matches ``T``, and the address of the first element is passed.
198200
This is done in order to avoid copying arrays unnecessarily.
199201

200202
Therefore, if an ``Array`` contains data in the wrong format, it will
201-
have to be explicitly converted using a call such as ``int32(a)``.
203+
have to be explicitly converted using a call such as :func:`int32(A) <int32>`.
202204

203205
To pass an array ``A`` as a pointer of a different type *without*
204206
converting the data beforehand (for example, to pass a ``Float64`` array
@@ -212,7 +214,9 @@ Type correspondences
212214

213215
On all systems we currently support, basic C/C++ value types may be
214216
translated to Julia types as follows. Every C type also has a corresponding
215-
Julia type with the same name, prefixed by C. This can help for writing portable code (and remembering that an int in C is not the same as an Int in Julia).
217+
Julia type with the same name, prefixed by C. This can help for writing
218+
portable code (and remembering that an ``int`` in C is not the same as an
219+
``Int`` in Julia).
216220

217221
**System-independent:**
218222

@@ -300,11 +304,11 @@ can be called via the following Julia code::
300304

301305
For ``wchar_t*`` arguments, the Julia type should be ``Ptr{Wchar_t}``,
302306
and data can be converted to/from ordinary Julia strings by the
303-
``wstring(s)`` function (equivalent to either ``utf16(s)`` or ``utf32(s)``
304-
depending upon the width of ``Cwchar_t``. Note also that ASCII, UTF-8,
305-
UTF-16, and UTF-32 string data in Julia is internally NUL-terminated, so
306-
it can be passed to C functions expecting NUL-terminated data without making
307-
a copy.
307+
:func:`wstring(s) <wstring>` function (equivalent to either :func:`utf16(s)
308+
<utf16>` or :func:`utf32(s) <utf32>` depending upon the width of ``Cwchar_t``.
309+
Note also that ASCII, UTF-8, UTF-16, and UTF-32 string data in Julia is
310+
internally NUL-terminated, so it can be passed to C functions expecting
311+
NUL-terminated data without making a copy.
308312

309313
Accessing Data through a Pointer
310314
--------------------------------
@@ -313,9 +317,10 @@ to terminate abruptly or corrupt arbitrary process memory due to a bad pointer
313317
or type declaration.
314318

315319
Given a ``Ptr{T}``, the contents of type ``T`` can generally be copied from
316-
the referenced memory into a Julia object using ``unsafe_load(ptr, [index])``. The
317-
index argument is optional (default is 1), and performs 1-based indexing. This
318-
function is intentionally similar to the behavior of ``getindex()`` and ``setindex!()``
320+
the referenced memory into a Julia object using :func:`unsafe_load(ptr,
321+
[index]) <unsafe_load>`. The index argument is optional (default is 1), and
322+
performs 1-based indexing. This function is intentionally similar to the
323+
behavior of :func:`getindex` and :func:`setindex!`
319324
(e.g. ``[]`` access syntax).
320325

321326
The return value will be a new object initialized
@@ -330,21 +335,23 @@ count, but the new reference does) to ensure the memory is not prematurely freed
330335
Note that if the object was not originally allocated by Julia, the new object
331336
will never be finalized by Julia's garbage collector. If the ``Ptr`` itself
332337
is actually a ``jl_value_t*``, it can be converted back to a Julia object
333-
reference by ``unsafe_pointer_to_objref(ptr)``. (Julia values ``v``
334-
can be converted to ``jl_value_t*`` pointers, as ``Ptr{Void}``, by calling
335-
``pointer_from_objref(v)``.)
338+
reference by :func:`unsafe_pointer_to_objref(ptr) <unsafe_pointer_to_objref>`.
339+
(Julia values ``v`` can be converted to ``jl_value_t*`` pointers, as
340+
``Ptr{Void}``, by calling :func:`pointer_from_objref(v)
341+
<pointer_from_objref>`.)
336342

337-
The reverse operation (writing data to a Ptr{T}), can be performed using
338-
``unsafe_store!(ptr, value, [index])``. Currently, this is only supported
339-
for bitstypes or other pointer-free (``isbits``) immutable types.
343+
The reverse operation (writing data to a ``Ptr{T}``), can be performed using
344+
:func:`unsafe_store!(ptr, value, [index]) <unsafe_store!>`. Currently, this is
345+
only supported for bitstypes or other pointer-free (:func:`isbits <isbits>`)
346+
immutable types.
340347

341348
Any operation that throws an error is probably currently unimplemented
342349
and should be posted as a bug so that it can be resolved.
343350

344351
If the pointer of interest is a plain-data array (bitstype or immutable), the
345-
function ``pointer_to_array(ptr,dims,[own])`` may be more useful. The final
352+
function :func:`pointer_to_array(ptr,dims,[own]) <pointer_to_array>` may be more useful. The final
346353
parameter should be true if Julia should "take ownership" of the underlying
347-
buffer and call ``free(ptr)`` when the returned ``Array`` object is finalized.
354+
buffer and call :func:`free(ptr) <free>` when the returned ``Array`` object is finalized.
348355
If the ``own`` parameter is omitted or false, the caller must ensure the
349356
buffer remains in existence until all access is complete.
350357

@@ -359,8 +366,8 @@ Passing Pointers for Modifying Inputs
359366

360367
Because C doesn't support multiple return values, often C functions will take
361368
pointers to data that the function will modify. To accomplish this within a
362-
``ccall`` you need to encapsulate the value inside an array of the appropriate
363-
type. When you pass the array as an argument with a ``Ptr`` type, julia will
369+
:func:`ccall` you need to encapsulate the value inside an array of the appropriate
370+
type. When you pass the array as an argument with a ``Ptr`` type, Julia will
364371
automatically pass a C pointer to the encapsulated data::
365372

366373
width = Cint[0]
@@ -372,21 +379,21 @@ is passed to LAPACK by reference, and on return, includes the success code.
372379

373380
Garbage Collection Safety
374381
-------------------------
375-
When passing data to a ccall, it is best to avoid using the ``pointer()``
382+
When passing data to :func:`ccall`, it is best to avoid using the :func:`pointer`
376383
function. Instead define a convert method and pass the variables directly to
377-
the ccall. ccall automatically arranges that all of its arguments will be
384+
the :func:`ccall`. :func:`ccall` automatically arranges that all of its arguments will be
378385
preserved from garbage collection until the call returns. If a C API will
379-
store a reference to memory allocated by Julia, after the ccall returns, you
386+
store a reference to memory allocated by Julia, after the :func:`ccall` returns, you
380387
must arrange that the object remains visible to the garbage collector. The
381388
suggested way to handle this is to make a global variable of type
382389
``Array{Any,1}`` to hold these values, until C interface notifies you that
383390
it is finished with them.
384391

385392
Whenever you have created a pointer to Julia data, you must ensure the original data
386393
exists until you are done with using the pointer. Many methods in Julia such as
387-
``unsafe_load()`` and ``bytestring()`` make copies of data instead of taking ownership
394+
:func:`unsafe_load` and :func:`bytestring` make copies of data instead of taking ownership
388395
of the buffer, so that it is safe to free (or alter) the original data without
389-
affecting Julia. A notable exception is ``pointer_to_array()`` which, for performance
396+
affecting Julia. A notable exception is :func:`pointer_to_array` which, for performance
390397
reasons, shares (or can be told to take ownership of) the underlying buffer.
391398

392399
The garbage collector does not guarantee any order of finalization. That is, if ``a``
@@ -401,35 +408,36 @@ Non-constant Function Specifications
401408

402409
A ``(name, library)`` function specification must be a constant expression.
403410
However, it is possible to use computed values as function names by staging
404-
through ``eval`` as follows::
411+
through :func:`eval` as follows::
405412

406413
@eval ccall(($(string("a","b")),"lib"), ...
407414

408-
This expression constructs a name using ``string``, then substitutes this
409-
name into a new ``ccall`` expression, which is then evaluated. Keep in mind that
410-
``eval`` only operates at the top level, so within this expression local
415+
This expression constructs a name using :func:`string`, then substitutes this
416+
name into a new :func:`ccall` expression, which is then evaluated. Keep in mind that
417+
:func:`eval` only operates at the top level, so within this expression local
411418
variables will not be available (unless their values are substituted with
412-
``$``). For this reason, ``eval`` is typically only used to form top-level
419+
``$``). For this reason, :func:`eval` is typically only used to form top-level
413420
definitions, for example when wrapping libraries that contain many
414421
similar functions.
415422

416423
Indirect Calls
417424
--------------
418425

419-
The first argument to ``ccall`` can also be an expression evaluated at
426+
The first argument to :func:`ccall` can also be an expression evaluated at
420427
run time. In this case, the expression must evaluate to a ``Ptr``,
421428
which will be used as the address of the native function to call. This
422-
behavior occurs when the first ``ccall`` argument contains references
429+
behavior occurs when the first :func:`ccall` argument contains references
423430
to non-constants, such as local variables or function arguments.
424431

425432
Calling Convention
426433
------------------
427434

428-
The second argument to ``ccall`` can optionally be a calling convention
435+
The second argument to :func:`ccall` can optionally be a calling convention
429436
specifier (immediately preceding return type). Without any specifier,
430437
the platform-default C calling convention is used. Other supported
431438
conventions are: ``stdcall``, ``cdecl``, ``fastcall``, and ``thiscall``.
432-
For example (from base/libc.jl)::
439+
For example (from
440+
`libc.jl <https://github.com/JuliaLang/julia/blob/master/base/libc.jl>`_::
433441

434442
hn = Array(Uint8, 256)
435443
err=ccall(:gethostname, stdcall, Int32, (Ptr{Uint8}, Uint32), hn, length(hn))
@@ -442,15 +450,15 @@ Accessing Global Variables
442450
--------------------------
443451

444452
Global variables exported by native libraries can be accessed by name using the
445-
``cglobal`` function. The arguments to ``cglobal`` are a symbol specification
446-
identical to that used by ``ccall``, and a type describing the value stored in
453+
:func:`cglobal` function. The arguments to :func:`cglobal` are a symbol specification
454+
identical to that used by :func:`ccall`, and a type describing the value stored in
447455
the variable::
448456

449457
julia> cglobal((:errno,:libc), Int32)
450458
Ptr{Int32} @0x00007f418d0816b8
451459

452460
The result is a pointer giving the address of the value. The value can be
453-
manipulated through this pointer using ``unsafe_load`` and ``unsafe_store``.
461+
manipulated through this pointer using :func:`unsafe_load` and :func:`unsafe_store`.
454462

455463
Passing Julia Callback Functions to C
456464
-------------------------------------
@@ -478,14 +486,14 @@ some arbitrary type T::
478486
end
479487

480488
Notice that we have to be careful about the return type: ``qsort`` expects a function
481-
returning a C ``int``, so we must be sure to return ``Cint`` via a call to ``convert``.
489+
returning a C ``int``, so we must be sure to return ``Cint`` via a call to :func:`convert`.
482490

483491
In order to pass this function to C, we obtain its address using the function
484-
``cfunction``::
492+
:func:`cfunction`::
485493

486494
const mycompare_c = cfunction(mycompare, Cint, (Ptr{Cdouble}, Ptr{Cdouble}))
487495

488-
``cfunction`` accepts three arguments: the Julia function (``mycompare``), the return
496+
:func:`cfunction` accepts three arguments: the Julia function (``mycompare``), the return
489497
type (``Cint``), and a tuple of the argument types, in this case to sort an array of
490498
``Cdouble`` (Float64) elements.
491499

@@ -515,7 +523,7 @@ triggered, which you'll probably just ignore) to ``SingleAsyncWork``::
515523

516524
cb = Base.SingleAsyncWork(data -> my_real_callback(args))
517525

518-
The callback you pass to C should only execute a ``ccall`` to
526+
The callback you pass to C should only execute a :func:`ccall` to
519527
``:uv_async_send``, passing ``cb.handle`` as the argument.
520528

521529
More About Callbacks
@@ -534,11 +542,12 @@ Handling Platform Variations
534542
----------------------------
535543

536544
When dealing with platform libraries, it is often necessary to provide special cases
537-
for various platforms. The variable ``OS_NAME`` can be used to write these special
545+
for various platforms. The variable :data:`OS_NAME` can be used to write these special
538546
cases. Additionally, there are several macros intended to make this easier:
539-
``@windows``, ``@unix``, ``@linux``, and ``@osx``. Note that linux and osx are mutually
540-
exclusive subsets of unix. Their usage takes the form of a ternary conditional
541-
operator, as demonstrated in the following examples.
547+
:func:`@windows`, :func:`@unix`, :func:`@linux`, and :func:`@osx`. Note that
548+
:func:`@linux` and :func:`@osx` are mutually exclusive subsets of
549+
:func:`@unix`. Their usage takes the form of a ternary conditional operator,
550+
as demonstrated in the following examples.
542551

543552
Simple blocks::
544553

doc/manual/complex-and-rational-numbers.rst

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.. _man-complex-and-rational-numbers:
22

3+
.. currentmodule:: Base
4+
35
******************************
46
Complex and Rational Numbers
57
******************************
@@ -122,10 +124,10 @@ Standard functions to manipulate complex values are provided:
122124
julia> angle(1 + 2im)
123125
1.1071487177940904
124126

125-
As usual, the absolute value (``abs``) of a complex number is its
126-
distance from zero. The ``abs2`` function gives the square of the
127+
As usual, the absolute value (:func:`abs`) of a complex number is its
128+
distance from zero. The :func:`abs2` function gives the square of the
127129
absolute value, and is of particular use for complex numbers where it
128-
avoids taking a square root. The ``angle`` function returns the phase
130+
avoids taking a square root. The :func:`angle` function returns the phase
129131
angle in radians (also known as the *argument* or *arg* function). The
130132
full gamut of other :ref:`man-elementary-functions` is also defined
131133
for complex numbers:
@@ -149,7 +151,7 @@ for complex numbers:
149151

150152
Note that mathematical functions typically return real values when applied
151153
to real numbers and complex values when applied to complex numbers.
152-
For example, ``sqrt`` behaves differently when applied to ``-1``
154+
For example, :func:`sqrt` behaves differently when applied to ``-1``
153155
versus ``-1 + 0im`` even though ``-1 == -1 + 0im``:
154156

155157
.. doctest::
@@ -172,7 +174,7 @@ multiplication must be explicitly written out:
172174
julia> a = 1; b = 2; a + b*im
173175
1 + 2im
174176

175-
However, this is *not* recommended; Use the ``complex`` function instead to
177+
However, this is *not* recommended; Use the :func:`complex` function instead to
176178
construct a complex value directly from its real and imaginary parts.:
177179

178180
.. doctest::
@@ -182,7 +184,7 @@ construct a complex value directly from its real and imaginary parts.:
182184

183185
This construction avoids the multiplication and addition operations.
184186

185-
``Inf`` and ``NaN`` propagate through complex numbers in the real
187+
:data:`Inf` and :data:`NaN` propagate through complex numbers in the real
186188
and imaginary parts of a complex number as described in the
187189
:ref:`man-special-floats` section:
188190

@@ -200,7 +202,7 @@ Rational Numbers
200202
----------------
201203

202204
Julia has a rational number type to represent exact ratios of integers.
203-
Rationals are constructed using the ``//`` operator:
205+
Rationals are constructed using the :func:`// <//>` operator:
204206

205207
.. doctest::
206208

@@ -227,7 +229,7 @@ are reduced to lowest terms such that the denominator is non-negative:
227229
This normalized form for a ratio of integers is unique, so equality of
228230
rational values can be tested by checking for equality of the numerator
229231
and denominator. The standardized numerator and denominator of a
230-
rational value can be extracted using the ``num`` and ``den`` functions:
232+
rational value can be extracted using the :func:`num` and :func:`den` functions:
231233

232234
.. doctest::
233235

@@ -296,7 +298,7 @@ Constructing infinite rational values is acceptable:
296298
julia> typeof(ans)
297299
Rational{Int64} (constructor with 1 method)
298300

299-
Trying to construct a ``NaN`` rational value, however, is not:
301+
Trying to construct a :data:`NaN` rational value, however, is not:
300302

301303
.. doctest::
302304

0 commit comments

Comments
 (0)