PR c++/68795: fix uninitialized close_paren_loc in cp_parser_postfix_expression
[gcc.git] / gcc / go / gccgo.texi
1 \input texinfo @c -*-texinfo-*-
2 @setfilename gccgo.info
3 @settitle The GNU Go Compiler
4
5 @c Merge the standard indexes into a single one.
6 @syncodeindex fn cp
7 @syncodeindex vr cp
8 @syncodeindex ky cp
9 @syncodeindex pg cp
10 @syncodeindex tp cp
11
12 @include gcc-common.texi
13
14 @c Copyright years for this manual.
15 @set copyrights-go 2010-2016
16
17 @copying
18 @c man begin COPYRIGHT
19 Copyright @copyright{} @value{copyrights-go} Free Software Foundation, Inc.
20
21 Permission is granted to copy, distribute and/or modify this document
22 under the terms of the GNU Free Documentation License, Version 1.3 or
23 any later version published by the Free Software Foundation; with no
24 Invariant Sections, the Front-Cover Texts being (a) (see below), and
25 with the Back-Cover Texts being (b) (see below).
26 A copy of the license is included in the
27 @c man end
28 section entitled ``GNU Free Documentation License''.
29 @ignore
30 @c man begin COPYRIGHT
31 man page gfdl(7).
32 @c man end
33 @end ignore
34
35 @c man begin COPYRIGHT
36
37 (a) The FSF's Front-Cover Text is:
38
39 A GNU Manual
40
41 (b) The FSF's Back-Cover Text is:
42
43 You have freedom to copy and modify this GNU Manual, like GNU
44 software. Copies published by the Free Software Foundation raise
45 funds for GNU development.
46 @c man end
47 @end copying
48
49 @ifinfo
50 @format
51 @dircategory Software development
52 @direntry
53 * Gccgo: (gccgo). A GCC-based compiler for the Go language
54 @end direntry
55 @end format
56
57 @insertcopying
58 @end ifinfo
59
60 @titlepage
61 @title The GNU Go Compiler
62 @versionsubtitle
63 @author Ian Lance Taylor
64
65 @page
66 @vskip 0pt plus 1filll
67 Published by the Free Software Foundation @*
68 51 Franklin Street, Fifth Floor@*
69 Boston, MA 02110-1301, USA@*
70 @sp 1
71 @insertcopying
72 @end titlepage
73 @contents
74 @page
75
76 @node Top
77 @top Introduction
78
79 This manual describes how to use @command{gccgo}, the GNU compiler for
80 the Go programming language. This manual is specifically about
81 @command{gccgo}. For more information about the Go programming
82 language in general, including language specifications and standard
83 package documentation, see @uref{http://golang.org/}.
84
85 @menu
86 * Copying:: The GNU General Public License.
87 * GNU Free Documentation License::
88 How you can share and copy this manual.
89 * Invoking gccgo:: How to run gccgo.
90 * Import and Export:: Importing and exporting package data.
91 * C Interoperability:: Calling C from Go and vice-versa.
92 * Index:: Index.
93 @end menu
94
95
96 @include gpl_v3.texi
97
98 @include fdl.texi
99
100
101 @node Invoking gccgo
102 @chapter Invoking gccgo
103
104 @c man title gccgo A GCC-based compiler for the Go language
105
106 @ignore
107 @c man begin SYNOPSIS gccgo
108 gccgo [@option{-c}|@option{-S}]
109 [@option{-g}] [@option{-pg}] [@option{-O}@var{level}]
110 [@option{-I}@var{dir}@dots{}] [@option{-L}@var{dir}@dots{}]
111 [@option{-o} @var{outfile}] @var{infile}@dots{}
112
113 Only the most useful options are listed here; see below for the
114 remainder.
115 @c man end
116 @c man begin SEEALSO
117 gpl(7), gfdl(7), fsf-funding(7), gcc(1)
118 and the Info entries for @file{gccgo} and @file{gcc}.
119 @c man end
120 @end ignore
121
122 @c man begin DESCRIPTION gccgo
123
124 The @command{gccgo} command is a frontend to @command{gcc} and
125 supports many of the same options. @xref{Option Summary, , Option
126 Summary, gcc, Using the GNU Compiler Collection (GCC)}. This manual
127 only documents the options specific to @command{gccgo}.
128
129 The @command{gccgo} command may be used to compile Go source code into
130 an object file, link a collection of object files together, or do both
131 in sequence.
132
133 Go source code is compiled as packages. A package consists of one or
134 more Go source files. All the files in a single package must be
135 compiled together, by passing all the files as arguments to
136 @command{gccgo}. A single invocation of @command{gccgo} may only
137 compile a single package.
138
139 One Go package may @code{import} a different Go package. The imported
140 package must have already been compiled; @command{gccgo} will read
141 the import data directly from the compiled package. When this package
142 is later linked, the compiled form of the package must be included in
143 the link command.
144
145 Go programs must generally be compiled with debugging information, and
146 @option{-g1} is the default as described below. Stripping a Go
147 program will generally cause it to misbehave or fail.
148
149 @c man end
150
151 @c man begin OPTIONS gccgo
152
153 @table @gcctabopt
154 @item -I@var{dir}
155 @cindex @option{-I}
156 Specify a directory to use when searching for an import package at
157 compile time.
158
159 @item -L@var{dir}
160 @cindex @option{-L}
161 When linking, specify a library search directory, as with
162 @command{gcc}.
163
164 @item -fgo-pkgpath=@var{string}
165 @cindex @option{-fgo-pkgpath}
166 Set the package path to use. This sets the value returned by the
167 PkgPath method of reflect.Type objects. It is also used for the names
168 of globally visible symbols. The argument to this option should
169 normally be the string that will be used to import this package after
170 it has been installed; in other words, a pathname within the
171 directories specified by the @option{-I} option.
172
173 @item -fgo-prefix=@var{string}
174 @cindex @option{-fgo-prefix}
175 An alternative to @option{-fgo-pkgpath}. The argument will be
176 combined with the package name from the source file to produce the
177 package path. If @option{-fgo-pkgpath} is used, @option{-fgo-prefix}
178 will be ignored.
179
180 Go permits a single program to include more than one package with the
181 same name in the @code{package} clause in the source file, though
182 obviously the two packages must be imported using different pathnames.
183 In order for this to work with @command{gccgo}, either
184 @option{-fgo-pkgpath} or @option{-fgo-prefix} must be specified when
185 compiling a package.
186
187 Using either @option{-fgo-pkgpath} or @option{-fgo-prefix} disables
188 the special treatment of the @code{main} package and permits that
189 package to be imported like any other.
190
191 @item -fgo-relative-import-path=@var{dir}
192 @cindex @option{-fgo-relative-import-path}
193 A relative import is an import that starts with @file{./} or
194 @file{../}. If this option is used, @command{gccgo} will use
195 @var{dir} as a prefix for the relative import when searching for it.
196
197 @item -frequire-return-statement
198 @itemx -fno-require-return-statement
199 @cindex @option{-frequire-return-statement}
200 @cindex @option{-fno-require-return-statement}
201 By default @command{gccgo} will warn about functions which have one or
202 more return parameters but lack an explicit @code{return} statement.
203 This warning may be disabled using
204 @option{-fno-require-return-statement}.
205
206 @item -fgo-check-divide-zero
207 @cindex @option{-fgo-check-divide-zero}
208 @cindex @option{-fno-go-check-divide-zero}
209 Add explicit checks for division by zero. In Go a division (or
210 modulos) by zero causes a panic. On Unix systems this is detected in
211 the runtime by catching the @code{SIGFPE} signal. Some processors,
212 such as PowerPC, do not generate a SIGFPE on division by zero. Some
213 runtimes do not generate a signal that can be caught. On those
214 systems, this option may be used. Or the checks may be removed via
215 @option{-fno-go-check-divide-zero}. This option is currently on by
216 default, but in the future may be off by default on systems that do
217 not require it.
218
219 @item -fgo-check-divide-overflow
220 @cindex @option{-fgo-check-divide-overflow}
221 @cindex @option{-fno-go-check-divide-overflow}
222 Add explicit checks for division overflow. For example, division
223 overflow occurs when computing @code{INT_MIN / -1}. In Go this should
224 be wrapped, to produce @code{INT_MIN}. Some processors, such as x86,
225 generate a trap on division overflow. On those systems, this option
226 may be used. Or the checks may be removed via
227 @option{-fno-go-check-divide-overflow}. This option is currently on
228 by default, but in the future may be off by default on systems that do
229 not require it.
230
231 @item -g
232 @cindex @option{-g for gccgo}
233 This is the standard @command{gcc} option (@pxref{Debugging Options, ,
234 Debugging Options, gcc, Using the GNU Compiler Collection (GCC)}). It
235 is mentioned here because by default @command{gccgo} turns on
236 debugging information generation with the equivalent of the standard
237 option @option{-g1}. This is because Go programs require debugging
238 information to be available in order to get backtrace information. An
239 explicit @option{-g0} may be used to disable the generation of
240 debugging information, in which case certain standard library
241 functions, such as @code{runtime.Callers}, will not operate correctly.
242 @end table
243
244 @c man end
245
246 @node Import and Export
247 @chapter Import and Export
248
249 When @command{gccgo} compiles a package which exports anything, the
250 export information will be stored directly in the object file. When a
251 package is imported, @command{gccgo} must be able to find the file.
252
253 @cindex @file{.gox}
254 When Go code imports the package @file{@var{gopackage}}, @command{gccgo}
255 will look for the import data using the following filenames, using the
256 first one that it finds.
257
258 @table @file
259 @item @var{gopackage}.gox
260 @item lib@var{gopackage}.so
261 @item lib@var{gopackage}.a
262 @item @var{gopackage}.o
263 @end table
264
265 The compiler will search for these files in the directories named by
266 any @option{-I} options, in order in which the directories appear on
267 the command line. The compiler will then search several standard
268 system directories. Finally the compiler will search the current
269 directory (to search the current directory earlier, use @samp{-I.}).
270
271 The compiler will extract the export information directly from the
272 compiled object file. The file @file{@var{gopackage}.gox} will
273 typically contain nothing but export data. This can be generated from
274 @file{@var{gopackage}.o} via
275
276 @smallexample
277 objcopy -j .go_export @var{gopackage}.o @var{gopackage}.gox
278 @end smallexample
279
280 For example, it may be desirable to extract the export information
281 from several different packages into their independent
282 @file{@var{gopackage}.gox} files, and then to combine the different
283 package object files together into a single shared library or archive.
284
285 At link time you must explicitly tell @command{gccgo} which files to
286 link together into the executable, as is usual with @command{gcc}.
287 This is different from the behaviour of other Go compilers.
288
289 @node C Interoperability
290 @chapter C Interoperability
291
292 When using @command{gccgo} there is limited interoperability with C,
293 or with C++ code compiled using @code{extern "C"}.
294
295 This information is provided largely for documentation purposes. For
296 ordinary use it is best to build programs with the go tool and then
297 use @code{import "C"}, as described at
298 @url{http://golang.org/cmd/cgo}.
299
300 @menu
301 * C Type Interoperability:: How C and Go types match up.
302 * Function Names:: How Go functions are named.
303 @end menu
304
305 @node C Type Interoperability
306 @section C Type Interoperability
307
308 Basic types map directly: an @code{int} in Go is an @code{int} in C,
309 etc. Go @code{byte} is equivalent to C @code{unsigned char}.
310 Pointers in Go are pointers in C. A Go @code{struct} is the same as C
311 @code{struct} with the same field names and types.
312
313 @cindex @code{string} in C
314 The Go @code{string} type is currently defined as a two-element
315 structure:
316
317 @smallexample
318 struct __go_string @{
319 const unsigned char *__data;
320 int __length;
321 @};
322 @end smallexample
323
324 You can't pass arrays between C and Go. However, a pointer to an
325 array in Go is equivalent to a C pointer to the equivalent of the
326 element type. For example, Go @code{*[10]int} is equivalent to C
327 @code{int*}, assuming that the C pointer does point to 10 elements.
328
329 @cindex @code{slice} in C
330 A slice in Go is a structure. The current definition is:
331
332 @smallexample
333 struct __go_slice @{
334 void *__values;
335 int __count;
336 int __capacity;
337 @};
338 @end smallexample
339
340 The type of a Go function with no receiver is equivalent to a C
341 function whose parameter types are equivalent. When a Go function
342 returns more than one value, the C function returns a struct. For
343 example, these functions have equivalent types:
344
345 @smallexample
346 func GoFunction(int) (int, float)
347 struct @{ int i; float f; @} CFunction(int)
348 @end smallexample
349
350 A pointer to a Go function is equivalent to a pointer to a C function
351 when the functions have equivalent types.
352
353 Go @code{interface}, @code{channel}, and @code{map} types have no
354 corresponding C type (@code{interface} is a two-element struct and
355 @code{channel} and @code{map} are pointers to structs in C, but the
356 structs are deliberately undocumented). C @code{enum} types
357 correspond to some integer type, but precisely which one is difficult
358 to predict in general; use a cast. C @code{union} types have no
359 corresponding Go type. C @code{struct} types containing bitfields
360 have no corresponding Go type. C++ @code{class} types have no
361 corresponding Go type.
362
363 Memory allocation is completely different between C and Go, as Go uses
364 garbage collection. The exact guidelines in this area are
365 undetermined, but it is likely that it will be permitted to pass a
366 pointer to allocated memory from C to Go. The responsibility of
367 eventually freeing the pointer will remain with C side, and of course
368 if the C side frees the pointer while the Go side still has a copy the
369 program will fail. When passing a pointer from Go to C, the Go
370 function must retain a visible copy of it in some Go variable.
371 Otherwise the Go garbage collector may delete the pointer while the C
372 function is still using it.
373
374 @node Function Names
375 @section Function Names
376
377 @cindex @code{extern}
378 @cindex external names
379 Go code can call C functions directly using a Go extension implemented
380 in @command{gccgo}: a function declaration may be preceded by a
381 comment giving the external name. The comment must be at the
382 beginning of the line and must start with @code{//extern}. This must
383 be followed by a space and then the external name of the function.
384 The function declaration must be on the line immediately after the
385 comment. For example, here is how the C function @code{open} can be
386 declared in Go:
387
388 @smallexample
389 //extern open
390 func c_open(name *byte, mode int, perm int) int
391 @end smallexample
392
393 The C function naturally expects a nul terminated string, which in Go
394 is equivalent to a pointer to an array (not a slice!) of @code{byte}
395 with a terminating zero byte. So a sample call from Go would look
396 like (after importing the @code{os} package):
397
398 @smallexample
399 var name = [4]byte@{'f', 'o', 'o', 0@};
400 i := c_open(&name[0], os.O_RDONLY, 0);
401 @end smallexample
402
403 Note that this serves as an example only. To open a file in Go please
404 use Go's @code{os.Open} function instead.
405
406 The name of Go functions accessed from C is subject to change. At
407 present the name of a Go function that does not have a receiver is
408 @code{prefix.package.Functionname}. The prefix is set by the
409 @option{-fgo-prefix} option used when the package is compiled; if the
410 option is not used, the default is simply @code{go}. To call the
411 function from C you must set the name using the @command{gcc}
412 @code{__asm__} extension.
413
414 @smallexample
415 extern int go_function(int) __asm__ ("myprefix.mypackage.Function");
416 @end smallexample
417
418 @node Index
419 @unnumbered Index
420
421 @printindex cp
422
423 @bye