1<html lang="en"> 2<head> 3<title>Type encoding - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Objective_002dC.html#Objective_002dC" title="Objective-C"> 9<link rel="prev" href="Executing-code-before-main.html#Executing-code-before-main" title="Executing code before main"> 10<link rel="next" href="Garbage-Collection.html#Garbage-Collection" title="Garbage Collection"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988-2013 Free Software Foundation, Inc. 14 15Permission is granted to copy, distribute and/or modify this document 16under the terms of the GNU Free Documentation License, Version 1.3 or 17any later version published by the Free Software Foundation; with the 18Invariant Sections being ``Funding Free Software'', the Front-Cover 19Texts being (a) (see below), and with the Back-Cover Texts being (b) 20(see below). A copy of the license is included in the section entitled 21``GNU Free Documentation License''. 22 23(a) The FSF's Front-Cover Text is: 24 25 A GNU Manual 26 27(b) The FSF's Back-Cover Text is: 28 29 You have freedom to copy and modify this GNU Manual, like GNU 30 software. Copies published by the Free Software Foundation raise 31 funds for GNU development.--> 32<meta http-equiv="Content-Style-Type" content="text/css"> 33<style type="text/css"><!-- 34 pre.display { font-family:inherit } 35 pre.format { font-family:inherit } 36 pre.smalldisplay { font-family:inherit; font-size:smaller } 37 pre.smallformat { font-family:inherit; font-size:smaller } 38 pre.smallexample { font-size:smaller } 39 pre.smalllisp { font-size:smaller } 40 span.sc { font-variant:small-caps } 41 span.roman { font-family:serif; font-weight:normal; } 42 span.sansserif { font-family:sans-serif; font-weight:normal; } 43--></style> 44<link rel="stylesheet" type="text/css" href="../cs.css"> 45</head> 46<body> 47<div class="node"> 48<a name="Type-encoding"></a> 49<p> 50Next: <a rel="next" accesskey="n" href="Garbage-Collection.html#Garbage-Collection">Garbage Collection</a>, 51Previous: <a rel="previous" accesskey="p" href="Executing-code-before-main.html#Executing-code-before-main">Executing code before main</a>, 52Up: <a rel="up" accesskey="u" href="Objective_002dC.html#Objective_002dC">Objective-C</a> 53<hr> 54</div> 55 56<h3 class="section">8.3 Type encoding</h3> 57 58<p>This is an advanced section. Type encodings are used extensively by 59the compiler and by the runtime, but you generally do not need to know 60about them to use Objective-C. 61 62 <p>The Objective-C compiler generates type encodings for all the types. 63These type encodings are used at runtime to find out information about 64selectors and methods and about objects and classes. 65 66 <p>The types are encoded in the following way: 67 68<!-- @sp 1 --> 69 <p><table summary=""><tr align="left"><td valign="top" width="25%"><code>_Bool</code> 70</td><td valign="top" width="75%"><code>B</code> 71<br></td></tr><tr align="left"><td valign="top" width="25%"><code>char</code> 72</td><td valign="top" width="75%"><code>c</code> 73<br></td></tr><tr align="left"><td valign="top" width="25%"><code>unsigned char</code> 74</td><td valign="top" width="75%"><code>C</code> 75<br></td></tr><tr align="left"><td valign="top" width="25%"><code>short</code> 76</td><td valign="top" width="75%"><code>s</code> 77<br></td></tr><tr align="left"><td valign="top" width="25%"><code>unsigned short</code> 78</td><td valign="top" width="75%"><code>S</code> 79<br></td></tr><tr align="left"><td valign="top" width="25%"><code>int</code> 80</td><td valign="top" width="75%"><code>i</code> 81<br></td></tr><tr align="left"><td valign="top" width="25%"><code>unsigned int</code> 82</td><td valign="top" width="75%"><code>I</code> 83<br></td></tr><tr align="left"><td valign="top" width="25%"><code>long</code> 84</td><td valign="top" width="75%"><code>l</code> 85<br></td></tr><tr align="left"><td valign="top" width="25%"><code>unsigned long</code> 86</td><td valign="top" width="75%"><code>L</code> 87<br></td></tr><tr align="left"><td valign="top" width="25%"><code>long long</code> 88</td><td valign="top" width="75%"><code>q</code> 89<br></td></tr><tr align="left"><td valign="top" width="25%"><code>unsigned long long</code> 90</td><td valign="top" width="75%"><code>Q</code> 91<br></td></tr><tr align="left"><td valign="top" width="25%"><code>float</code> 92</td><td valign="top" width="75%"><code>f</code> 93<br></td></tr><tr align="left"><td valign="top" width="25%"><code>double</code> 94</td><td valign="top" width="75%"><code>d</code> 95<br></td></tr><tr align="left"><td valign="top" width="25%"><code>long double</code> 96</td><td valign="top" width="75%"><code>D</code> 97<br></td></tr><tr align="left"><td valign="top" width="25%"><code>void</code> 98</td><td valign="top" width="75%"><code>v</code> 99<br></td></tr><tr align="left"><td valign="top" width="25%"><code>id</code> 100</td><td valign="top" width="75%"><code>@</code> 101<br></td></tr><tr align="left"><td valign="top" width="25%"><code>Class</code> 102</td><td valign="top" width="75%"><code>#</code> 103<br></td></tr><tr align="left"><td valign="top" width="25%"><code>SEL</code> 104</td><td valign="top" width="75%"><code>:</code> 105<br></td></tr><tr align="left"><td valign="top" width="25%"><code>char*</code> 106</td><td valign="top" width="75%"><code>*</code> 107<br></td></tr><tr align="left"><td valign="top" width="25%"><code>enum</code> 108</td><td valign="top" width="75%">an <code>enum</code> is encoded exactly as the integer type that the compiler uses for it, which depends on the enumeration 109values. Often the compiler users <code>unsigned int</code>, which is then encoded as <code>I</code>. 110<br></td></tr><tr align="left"><td valign="top" width="25%">unknown type 111</td><td valign="top" width="75%"><code>?</code> 112<br></td></tr><tr align="left"><td valign="top" width="25%">Complex types 113</td><td valign="top" width="75%"><code>j</code> followed by the inner type. For example <code>_Complex double</code> is encoded as "jd". 114<br></td></tr><tr align="left"><td valign="top" width="25%">bit-fields 115</td><td valign="top" width="75%"><code>b</code> followed by the starting position of the bit-field, the type of the bit-field and the size of the bit-field (the bit-fields encoding was changed from the NeXT's compiler encoding, see below) 116 <br></td></tr></table> 117 118<!-- @sp 1 --> 119 <p>The encoding of bit-fields has changed to allow bit-fields to be 120properly handled by the runtime functions that compute sizes and 121alignments of types that contain bit-fields. The previous encoding 122contained only the size of the bit-field. Using only this information 123it is not possible to reliably compute the size occupied by the 124bit-field. This is very important in the presence of the Boehm's 125garbage collector because the objects are allocated using the typed 126memory facility available in this collector. The typed memory 127allocation requires information about where the pointers are located 128inside the object. 129 130 <p>The position in the bit-field is the position, counting in bits, of the 131bit closest to the beginning of the structure. 132 133 <p>The non-atomic types are encoded as follows: 134 135<!-- @sp 1 --> 136 <p><table summary=""><tr align="left"><td valign="top" width="20%">pointers 137</td><td valign="top" width="80%">‘<samp><span class="samp">^</span></samp>’ followed by the pointed type. 138<br></td></tr><tr align="left"><td valign="top" width="20%">arrays 139</td><td valign="top" width="80%">‘<samp><span class="samp">[</span></samp>’ followed by the number of elements in the array followed by the type of the elements followed by ‘<samp><span class="samp">]</span></samp>’ 140<br></td></tr><tr align="left"><td valign="top" width="20%">structures 141</td><td valign="top" width="80%">‘<samp><span class="samp">{</span></samp>’ followed by the name of the structure (or ‘<samp><span class="samp">?</span></samp>’ if the structure is unnamed), the ‘<samp><span class="samp">=</span></samp>’ sign, the type of the members and by ‘<samp><span class="samp">}</span></samp>’ 142<br></td></tr><tr align="left"><td valign="top" width="20%">unions 143</td><td valign="top" width="80%">‘<samp><span class="samp">(</span></samp>’ followed by the name of the structure (or ‘<samp><span class="samp">?</span></samp>’ if the union is unnamed), the ‘<samp><span class="samp">=</span></samp>’ sign, the type of the members followed by ‘<samp><span class="samp">)</span></samp>’ 144<br></td></tr><tr align="left"><td valign="top" width="20%">vectors 145</td><td valign="top" width="80%">‘<samp><span class="samp">![</span></samp>’ followed by the vector_size (the number of bytes composing the vector) followed by a comma, followed by the alignment (in bytes) of the vector, followed by the type of the elements followed by ‘<samp><span class="samp">]</span></samp>’ 146 <br></td></tr></table> 147 148 <p>Here are some types and their encodings, as they are generated by the 149compiler on an i386 machine: 150 151 <pre class="sp"> 152 153</pre> 154 <p><table summary=""><tr align="left"><td valign="top" width="25%">Objective-C type 155</td><td valign="top" width="75%">Compiler encoding 156<br></td></tr><tr align="left"><td valign="top" width="25%"> 157<pre class="smallexample"> int a[10]; 158</pre> 159 <p></td><td valign="top" width="75%"><code>[10i]</code> 160<br></td></tr><tr align="left"><td valign="top" width="25%"> 161<pre class="smallexample"> struct { 162 int i; 163 float f[3]; 164 int a:3; 165 int b:2; 166 char c; 167 } 168</pre> 169 <p></td><td valign="top" width="75%"><code>{?=i[3f]b128i3b131i2c}</code> 170<br></td></tr><tr align="left"><td valign="top" width="25%"> 171<pre class="smallexample"> int a __attribute__ ((vector_size (16))); 172</pre> 173 <p></td><td valign="top" width="75%"><code>![16,16i]</code> (alignment would depend on the machine) 174 <br></td></tr></table> 175 176 <pre class="sp"> 177 178</pre> 179 180In addition to the types the compiler also encodes the type 181specifiers. The table below describes the encoding of the current 182Objective-C type specifiers: 183 184 <pre class="sp"> 185 186</pre> 187 <p><table summary=""><tr align="left"><td valign="top" width="25%">Specifier 188</td><td valign="top" width="75%">Encoding 189<br></td></tr><tr align="left"><td valign="top" width="25%"><code>const</code> 190</td><td valign="top" width="75%"><code>r</code> 191<br></td></tr><tr align="left"><td valign="top" width="25%"><code>in</code> 192</td><td valign="top" width="75%"><code>n</code> 193<br></td></tr><tr align="left"><td valign="top" width="25%"><code>inout</code> 194</td><td valign="top" width="75%"><code>N</code> 195<br></td></tr><tr align="left"><td valign="top" width="25%"><code>out</code> 196</td><td valign="top" width="75%"><code>o</code> 197<br></td></tr><tr align="left"><td valign="top" width="25%"><code>bycopy</code> 198</td><td valign="top" width="75%"><code>O</code> 199<br></td></tr><tr align="left"><td valign="top" width="25%"><code>byref</code> 200</td><td valign="top" width="75%"><code>R</code> 201<br></td></tr><tr align="left"><td valign="top" width="25%"><code>oneway</code> 202</td><td valign="top" width="75%"><code>V</code> 203 <br></td></tr></table> 204 205 <pre class="sp"> 206 207</pre> 208 209The type specifiers are encoded just before the type. Unlike types 210however, the type specifiers are only encoded when they appear in method 211argument types. 212 213 <p>Note how <code>const</code> interacts with pointers: 214 215 <pre class="sp"> 216 217</pre> 218 <p><table summary=""><tr align="left"><td valign="top" width="25%">Objective-C type 219</td><td valign="top" width="75%">Compiler encoding 220<br></td></tr><tr align="left"><td valign="top" width="25%"> 221<pre class="smallexample"> const int 222</pre> 223 <p></td><td valign="top" width="75%"><code>ri</code> 224<br></td></tr><tr align="left"><td valign="top" width="25%"> 225<pre class="smallexample"> const int* 226</pre> 227 <p></td><td valign="top" width="75%"><code>^ri</code> 228<br></td></tr><tr align="left"><td valign="top" width="25%"> 229<pre class="smallexample"> int *const 230</pre> 231 <p></td><td valign="top" width="75%"><code>r^i</code> 232 <br></td></tr></table> 233 234 <pre class="sp"> 235 236</pre> 237 238<code>const int*</code> is a pointer to a <code>const int</code>, and so is 239encoded as <code>^ri</code>. <code>int* const</code>, instead, is a <code>const</code> 240pointer to an <code>int</code>, and so is encoded as <code>r^i</code>. 241 242 <p>Finally, there is a complication when encoding <code>const char *</code> 243versus <code>char * const</code>. Because <code>char *</code> is encoded as 244<code>*</code> and not as <code>^c</code>, there is no way to express the fact 245that <code>r</code> applies to the pointer or to the pointee. 246 247 <p>Hence, it is assumed as a convention that <code>r*</code> means <code>const 248char *</code> (since it is what is most often meant), and there is no way to 249encode <code>char *const</code>. <code>char *const</code> would simply be encoded 250as <code>*</code>, and the <code>const</code> is lost. 251 252<ul class="menu"> 253<li><a accesskey="1" href="Legacy-type-encoding.html#Legacy-type-encoding">Legacy type encoding</a> 254<li><a accesskey="2" href="_0040encode.html#g_t_0040encode">@encode</a> 255<li><a accesskey="3" href="Method-signatures.html#Method-signatures">Method signatures</a> 256</ul> 257 258 </body></html> 259 260