summaryrefslogtreecommitdiff
path: root/gcc/jit/docs/cp/topics/types.rst
blob: 96c9bf475d74570dc1a2b5bbf1e75fbf3a54be6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
.. Copyright (C) 2014-2016 Free Software Foundation, Inc.
   Originally contributed by David Malcolm <dmalcolm@redhat.com>

   This is free software: you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.

.. default-domain:: cpp

Types
=====

.. class:: gccjit::type

   gccjit::type represents a type within the library.  It is a subclass
   of :class:`gccjit::object`.

Types can be created in several ways:

* fundamental types can be accessed using
  :func:`gccjit::context::get_type`:

  .. code-block:: c++

      gccjit::type int_type = ctxt.get_type (GCC_JIT_TYPE_INT);

  or using the :func:`gccjit::context::get_int_type<T>` template:

  .. code-block:: c++

      gccjit::type t = ctxt.get_int_type <unsigned short> ();

  See :c:func:`gcc_jit_context_get_type` for the available types.

* derived types can be accessed by using functions such as
  :func:`gccjit::type::get_pointer` and :func:`gccjit::type::get_const`:

  .. code-block:: c++

    gccjit::type const_int_star = int_type.get_const ().get_pointer ();
    gccjit::type int_const_star = int_type.get_pointer ().get_const ();

* by creating structures (see below).

Standard types
--------------

.. function:: gccjit::type gccjit::context::get_type (enum gcc_jit_types)

   Access a specific type.  This is a thin wrapper around
   :c:func:`gcc_jit_context_get_type`; the parameter has the same meaning.

.. function:: gccjit::type \
              gccjit::context::get_int_type (size_t num_bytes, int is_signed)

   Access the integer type of the given size.

.. function:: gccjit::type \
              gccjit::context::get_int_type <T> ()

   Access the given integer type.  For example, you could map the
   ``unsigned short`` type into a gccjit::type via:

   .. code-block:: c++

      gccjit::type t = ctxt.get_int_type <unsigned short> ();

Pointers, `const`, and `volatile`
---------------------------------

.. function::  gccjit::type gccjit::type::get_pointer ()

   Given type "T", get type "T*".

.. FIXME: get_const doesn't seem to exist

.. function::  gccjit::type gccjit::type::get_const ()

   Given type "T", get type "const T".

.. function::  gccjit::type gccjit::type::get_volatile ()

   Given type "T", get type "volatile T".

.. function::  gccjit::type \
               gccjit::context::new_array_type (gccjit::type element_type, \
                                                int num_elements, \
			                        gccjit::location loc)

   Given type "T", get type "T[N]" (for a constant N).
   Param "loc" is optional.


Structures and unions
---------------------

.. class:: gccjit::struct_

A compound type analagous to a C `struct`.

:class:`gccjit::struct_` is a subclass of :class:`gccjit::type` (and thus
of :class:`gccjit::object` in turn).

.. class:: gccjit::field

A field within a :class:`gccjit::struct_`.

:class:`gccjit::field` is a subclass of :class:`gccjit::object`.

You can model C `struct` types by creating :class:`gccjit::struct_` and
:class:`gccjit::field` instances, in either order:

* by creating the fields, then the structure.  For example, to model:

  .. code-block:: c

    struct coord {double x; double y; };

  you could call:

  .. code-block:: c++

    gccjit::field field_x = ctxt.new_field (double_type, "x");
    gccjit::field field_y = ctxt.new_field (double_type, "y");
    std::vector fields;
    fields.push_back (field_x);
    fields.push_back (field_y);
    gccjit::struct_ coord = ctxt.new_struct_type ("coord", fields);

* by creating the structure, then populating it with fields, typically
  to allow modelling self-referential structs such as:

  .. code-block:: c

    struct node { int m_hash; struct node *m_next; };

  like this:

  .. code-block:: c++

    gccjit::struct_ node = ctxt.new_opaque_struct_type ("node");
    gccjit::type node_ptr = node.get_pointer ();
    gccjit::field field_hash = ctxt.new_field (int_type, "m_hash");
    gccjit::field field_next = ctxt.new_field (node_ptr, "m_next");
    std::vector fields;
    fields.push_back (field_hash);
    fields.push_back (field_next);
    node.set_fields (fields);

.. FIXME: the above API doesn't seem to exist yet

.. function:: gccjit::field \
              gccjit::context::new_field (gccjit::type type,\
                                          const char *name, \
                                          gccjit::location loc)

   Construct a new field, with the given type and name.

.. function:: gccjit::struct_ \
   gccjit::context::new_struct_type (const std::string &name,\
                                     std::vector<field> &fields,\
                                     gccjit::location loc)

     Construct a new struct type, with the given name and fields.

.. function:: gccjit::struct_ \
              gccjit::context::new_opaque_struct (const std::string &name, \
                                                  gccjit::location loc)

     Construct a new struct type, with the given name, but without
     specifying the fields.   The fields can be omitted (in which case the
     size of the struct is not known), or later specified using
     :c:func:`gcc_jit_struct_set_fields`.