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