annotate gcc/jit/docs/cp/topics/types.rst @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1 .. Copyright (C) 2014-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
2 Originally contributed by David Malcolm <dmalcolm@redhat.com>
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This is free software: you can redistribute it and/or modify it
kono
parents:
diff changeset
5 under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
6 the Free Software Foundation, either version 3 of the License, or
kono
parents:
diff changeset
7 (at your option) any later version.
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 This program is distributed in the hope that it will be useful, but
kono
parents:
diff changeset
10 WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
kono
parents:
diff changeset
12 General Public License for more details.
kono
parents:
diff changeset
13
kono
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
15 along with this program. If not, see
kono
parents:
diff changeset
16 <http://www.gnu.org/licenses/>.
kono
parents:
diff changeset
17
kono
parents:
diff changeset
18 .. default-domain:: cpp
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 Types
kono
parents:
diff changeset
21 =====
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 .. class:: gccjit::type
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 gccjit::type represents a type within the library. It is a subclass
kono
parents:
diff changeset
26 of :class:`gccjit::object`.
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 Types can be created in several ways:
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 * fundamental types can be accessed using
kono
parents:
diff changeset
31 :func:`gccjit::context::get_type`:
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 .. code-block:: c++
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 gccjit::type int_type = ctxt.get_type (GCC_JIT_TYPE_INT);
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 or using the :func:`gccjit::context::get_int_type<T>` template:
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 .. code-block:: c++
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 gccjit::type t = ctxt.get_int_type <unsigned short> ();
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 See :c:func:`gcc_jit_context_get_type` for the available types.
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 * derived types can be accessed by using functions such as
kono
parents:
diff changeset
46 :func:`gccjit::type::get_pointer` and :func:`gccjit::type::get_const`:
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 .. code-block:: c++
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 gccjit::type const_int_star = int_type.get_const ().get_pointer ();
kono
parents:
diff changeset
51 gccjit::type int_const_star = int_type.get_pointer ().get_const ();
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 * by creating structures (see below).
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 Standard types
kono
parents:
diff changeset
56 --------------
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 .. function:: gccjit::type gccjit::context::get_type (enum gcc_jit_types)
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 Access a specific type. This is a thin wrapper around
kono
parents:
diff changeset
61 :c:func:`gcc_jit_context_get_type`; the parameter has the same meaning.
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 .. function:: gccjit::type \
kono
parents:
diff changeset
64 gccjit::context::get_int_type (size_t num_bytes, int is_signed)
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 Access the integer type of the given size.
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 .. function:: gccjit::type \
kono
parents:
diff changeset
69 gccjit::context::get_int_type <T> ()
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 Access the given integer type. For example, you could map the
kono
parents:
diff changeset
72 ``unsigned short`` type into a gccjit::type via:
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 .. code-block:: c++
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 gccjit::type t = ctxt.get_int_type <unsigned short> ();
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 Pointers, `const`, and `volatile`
kono
parents:
diff changeset
79 ---------------------------------
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 .. function:: gccjit::type gccjit::type::get_pointer ()
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 Given type "T", get type "T*".
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 .. function:: gccjit::type gccjit::type::get_const ()
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 Given type "T", get type "const T".
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 .. function:: gccjit::type gccjit::type::get_volatile ()
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 Given type "T", get type "volatile T".
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 .. function:: gccjit::type gccjit::type::get_aligned (size_t alignment_in_bytes)
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 Given type "T", get type:
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 .. code-block:: c
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 T __attribute__ ((aligned (ALIGNMENT_IN_BYTES)))
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 The alignment must be a power of two.
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 .. function:: gccjit::type \
kono
parents:
diff changeset
104 gccjit::context::new_array_type (gccjit::type element_type, \
kono
parents:
diff changeset
105 int num_elements, \
kono
parents:
diff changeset
106 gccjit::location loc)
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 Given type "T", get type "T[N]" (for a constant N).
kono
parents:
diff changeset
109 Param "loc" is optional.
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 Vector types
kono
parents:
diff changeset
113 ------------
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 .. function:: gccjit::type gccjit::type::get_vector (size_t num_units)
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 Given type "T", get type:
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 .. code-block:: c
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 T __attribute__ ((vector_size (sizeof(T) * num_units))
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 T must be integral or floating point; num_units must be a power of two.
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 Structures and unions
kono
parents:
diff changeset
127 ---------------------
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 .. class:: gccjit::struct_
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 A compound type analagous to a C `struct`.
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 :class:`gccjit::struct_` is a subclass of :class:`gccjit::type` (and thus
kono
parents:
diff changeset
134 of :class:`gccjit::object` in turn).
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 .. class:: gccjit::field
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 A field within a :class:`gccjit::struct_`.
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 :class:`gccjit::field` is a subclass of :class:`gccjit::object`.
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 You can model C `struct` types by creating :class:`gccjit::struct_` and
kono
parents:
diff changeset
143 :class:`gccjit::field` instances, in either order:
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 * by creating the fields, then the structure. For example, to model:
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 .. code-block:: c
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 struct coord {double x; double y; };
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 you could call:
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 .. code-block:: c++
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 gccjit::field field_x = ctxt.new_field (double_type, "x");
kono
parents:
diff changeset
156 gccjit::field field_y = ctxt.new_field (double_type, "y");
kono
parents:
diff changeset
157 std::vector fields;
kono
parents:
diff changeset
158 fields.push_back (field_x);
kono
parents:
diff changeset
159 fields.push_back (field_y);
kono
parents:
diff changeset
160 gccjit::struct_ coord = ctxt.new_struct_type ("coord", fields);
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 * by creating the structure, then populating it with fields, typically
kono
parents:
diff changeset
163 to allow modelling self-referential structs such as:
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 .. code-block:: c
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 struct node { int m_hash; struct node *m_next; };
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 like this:
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 .. code-block:: c++
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 gccjit::struct_ node = ctxt.new_opaque_struct_type ("node");
kono
parents:
diff changeset
174 gccjit::type node_ptr = node.get_pointer ();
kono
parents:
diff changeset
175 gccjit::field field_hash = ctxt.new_field (int_type, "m_hash");
kono
parents:
diff changeset
176 gccjit::field field_next = ctxt.new_field (node_ptr, "m_next");
kono
parents:
diff changeset
177 std::vector fields;
kono
parents:
diff changeset
178 fields.push_back (field_hash);
kono
parents:
diff changeset
179 fields.push_back (field_next);
kono
parents:
diff changeset
180 node.set_fields (fields);
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 .. FIXME: the above API doesn't seem to exist yet
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 .. function:: gccjit::field \
kono
parents:
diff changeset
185 gccjit::context::new_field (gccjit::type type,\
kono
parents:
diff changeset
186 const char *name, \
kono
parents:
diff changeset
187 gccjit::location loc)
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 Construct a new field, with the given type and name.
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 .. function:: gccjit::struct_ \
kono
parents:
diff changeset
192 gccjit::context::new_struct_type (const std::string &name,\
kono
parents:
diff changeset
193 std::vector<field> &fields,\
kono
parents:
diff changeset
194 gccjit::location loc)
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 Construct a new struct type, with the given name and fields.
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 .. function:: gccjit::struct_ \
kono
parents:
diff changeset
199 gccjit::context::new_opaque_struct (const std::string &name, \
kono
parents:
diff changeset
200 gccjit::location loc)
kono
parents:
diff changeset
201
kono
parents:
diff changeset
202 Construct a new struct type, with the given name, but without
kono
parents:
diff changeset
203 specifying the fields. The fields can be omitted (in which case the
kono
parents:
diff changeset
204 size of the struct is not known), or later specified using
kono
parents:
diff changeset
205 :c:func:`gcc_jit_struct_set_fields`.