2015-01-15 Richard Sandiford <richard.sandiford@arm.com>
[official-gcc.git] / gcc / jit / docs / cp / topics / types.rst
blobaa0640e6111d59a89c2b5d13bba3fa8effd772ac
1 .. Copyright (C) 2014-2015 Free Software Foundation, Inc.
2    Originally contributed by David Malcolm <dmalcolm@redhat.com>
4    This is free software: you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see
16    <http://www.gnu.org/licenses/>.
18 .. default-domain:: cpp
20 Types
21 =====
23 .. class:: gccjit::type
25    gccjit::type represents a type within the library.  It is a subclass
26    of :class:`gccjit::object`.
28 Types can be created in several ways:
30 * fundamental types can be accessed using
31   :func:`gccjit::context::get_type`:
33   .. code-block:: c++
35       gccjit::type int_type = ctxt.get_type (GCC_JIT_TYPE_INT);
37   or using the :func:`gccjit::context::get_int_type<T>` template:
39   .. code-block:: c++
41       gccjit::type t = ctxt.get_int_type <unsigned short> ();
43   See :c:func:`gcc_jit_context_get_type` for the available types.
45 * derived types can be accessed by using functions such as
46   :func:`gccjit::type::get_pointer` and :func:`gccjit::type::get_const`:
48   .. code-block:: c++
50     gccjit::type const_int_star = int_type.get_const ().get_pointer ();
51     gccjit::type int_const_star = int_type.get_pointer ().get_const ();
53 * by creating structures (see below).
55 Standard types
56 --------------
58 .. function:: gccjit::type gccjit::context::get_type (enum gcc_jit_types)
60    Access a specific type.  This is a thin wrapper around
61    :c:func:`gcc_jit_context_get_type`; the parameter has the same meaning.
63 .. function:: gccjit::type \
64               gccjit::context::get_int_type (size_t num_bytes, int is_signed)
66    Access the integer type of the given size.
68 .. function:: gccjit::type \
69               gccjit::context::get_int_type <T> ()
71    Access the given integer type.  For example, you could map the
72    ``unsigned short`` type into a gccjit::type via:
74    .. code-block:: c++
76       gccjit::type t = ctxt.get_int_type <unsigned short> ();
78 Pointers, `const`, and `volatile`
79 ---------------------------------
81 .. function::  gccjit::type gccjit::type::get_pointer ()
83    Given type "T", get type "T*".
85 .. FIXME: get_const doesn't seem to exist
87 .. function::  gccjit::type gccjit::type::get_const ()
89    Given type "T", get type "const T".
91 .. function::  gccjit::type gccjit::type::get_volatile ()
93    Given type "T", get type "volatile T".
95 .. function::  gccjit::type \
96                gccjit::context::new_array_type (gccjit::type element_type, \
97                                                 int num_elements, \
98                                                 gccjit::location loc)
100    Given type "T", get type "T[N]" (for a constant N).
101    Param "loc" is optional.
104 Structures and unions
105 ---------------------
107 .. class:: gccjit::struct_
109 A compound type analagous to a C `struct`.
111 :class:`gccjit::struct_` is a subclass of :class:`gccjit::type` (and thus
112 of :class:`gccjit::object` in turn).
114 .. class:: gccjit::field
116 A field within a :class:`gccjit::struct_`.
118 :class:`gccjit::field` is a subclass of :class:`gccjit::object`.
120 You can model C `struct` types by creating :class:`gccjit::struct_` and
121 :class:`gccjit::field` instances, in either order:
123 * by creating the fields, then the structure.  For example, to model:
125   .. code-block:: c
127     struct coord {double x; double y; };
129   you could call:
131   .. code-block:: c++
133     gccjit::field field_x = ctxt.new_field (double_type, "x");
134     gccjit::field field_y = ctxt.new_field (double_type, "y");
135     std::vector fields;
136     fields.push_back (field_x);
137     fields.push_back (field_y);
138     gccjit::struct_ coord = ctxt.new_struct_type ("coord", fields);
140 * by creating the structure, then populating it with fields, typically
141   to allow modelling self-referential structs such as:
143   .. code-block:: c
145     struct node { int m_hash; struct node *m_next; };
147   like this:
149   .. code-block:: c++
151     gccjit::struct_ node = ctxt.new_opaque_struct_type ("node");
152     gccjit::type node_ptr = node.get_pointer ();
153     gccjit::field field_hash = ctxt.new_field (int_type, "m_hash");
154     gccjit::field field_next = ctxt.new_field (node_ptr, "m_next");
155     std::vector fields;
156     fields.push_back (field_hash);
157     fields.push_back (field_next);
158     node.set_fields (fields);
160 .. FIXME: the above API doesn't seem to exist yet
162 .. function:: gccjit::field \
163               gccjit::context::new_field (gccjit::type type,\
164                                           const char *name, \
165                                           gccjit::location loc)
167    Construct a new field, with the given type and name.
169 .. function:: gccjit::struct_ \
170    gccjit::context::new_struct_type (const std::string &name,\
171                                      std::vector<field> &fields,\
172                                      gccjit::location loc)
174      Construct a new struct type, with the given name and fields.
176 .. function:: gccjit::struct_ \
177               gccjit::context::new_opaque_struct (const std::string &name, \
178                                                   gccjit::location loc)
180      Construct a new struct type, with the given name, but without
181      specifying the fields.   The fields can be omitted (in which case the
182      size of the struct is not known), or later specified using
183      :c:func:`gcc_jit_struct_set_fields`.