]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/d/gdc.texi
Add D front-end, libphobos library, and D2 testsuite.
[thirdparty/gcc.git] / gcc / d / gdc.texi
CommitLineData
b4c522fa
IB
1\input texinfo @c -*-texinfo-*-
2@setfilename gdc.info
3@settitle The GNU D 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-d 2006-2018
16
17@copying
18@c man begin COPYRIGHT
19Copyright @copyright{} @value{copyrights-d} Free Software Foundation, Inc.
20
21Permission is granted to copy, distribute and/or modify this document
22under the terms of the GNU Free Documentation License, Version 1.3 or
23any later version published by the Free Software Foundation; with no
24Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
25A copy of the license is included in the
26@c man end
27section entitled ``GNU Free Documentation License''.
28@ignore
29@c man begin COPYRIGHT
30man page gfdl(7).
31@c man end
32@end ignore
33@end copying
34
35@ifinfo
36@format
37@dircategory Software development
38@direntry
39* gdc: (gdc). A GCC-based compiler for the D language
40@end direntry
41@end format
42
43@insertcopying
44@end ifinfo
45
46@titlepage
47@title The GNU D Compiler
48@versionsubtitle
49@author David Friedman, Iain Buclaw
50
51@page
52@vskip 0pt plus 1filll
53Published by the Free Software Foundation @*
5451 Franklin Street, Fifth Floor@*
55Boston, MA 02110-1301, USA@*
56@sp 1
57@insertcopying
58@end titlepage
59@contents
60@page
61
62@node Top
63@top Introduction
64
65This manual describes how to use @command{gdc}, the GNU compiler for
66the D programming language. This manual is specifically about
67@command{gdc}. For more information about the D programming
68language in general, including language specifications and standard
69package documentation, see @uref{http://dlang.org/}.
70
71@menu
72* Copying:: The GNU General Public License.
73* GNU Free Documentation License::
74 How you can share and copy this manual.
75* Invoking gdc:: How to run gdc.
76* Index:: Index.
77@end menu
78
79
80@include gpl_v3.texi
81
82@include fdl.texi
83
84
85@node Invoking gdc
86@chapter Invoking gdc
87
88@c man title gdc A GCC-based compiler for the D language
89
90@ignore
91@c man begin SYNOPSIS gdc
92gdc [@option{-c}|@option{-S}] [@option{-g}] [@option{-pg}]
93 [@option{-O}@var{level}] [@option{-W}@var{warn}@dots{}]
94 [@option{-I}@var{dir}@dots{}] [@option{-L}@var{dir}@dots{}]
95 [@option{-f}@var{option}@dots{}] [@option{-m}@var{machine-option}@dots{}]
96 [@option{-o} @var{outfile}] [@@@var{file}] @var{infile}@dots{}
97
98Only the most useful options are listed here; see below for the
99remainder.
100@c man end
101@c man begin SEEALSO
102gpl(7), gfdl(7), fsf-funding(7), gcc(1)
103and the Info entries for @file{gdc} and @file{gcc}.
104@c man end
105@end ignore
106
107@c man begin DESCRIPTION gdc
108
109The @command{gdc} command is the GNU compiler for the D language and
110supports many of the same options as @command{gcc}. @xref{Option Summary, ,
111Option Summary, gcc, Using the GNU Compiler Collection (GCC)}.
112This manual only documents the options specific to @command{gdc}.
113
114@c man end
115
116@menu
117* Input and Output files:: Controlling the kind of output:
118 an executable, object files, assembler files,
119* Runtime Options:: Options controlling runtime behavior
120* Directory Options:: Where to find module files
121* Code Generation:: Options controlling the output of gdc
122* Warnings:: Options controlling warnings specific to gdc
123* Linking:: Options influencing the linking step
124* Developer Options:: Options useful for developers of gdc
125@end menu
126
127@c man begin OPTIONS
128
129@node Input and Output files
130@section Input and Output files
131@cindex suffixes for D source
132@cindex D source file suffixes
133
134For any given input file, the file name suffix determines what kind of
135compilation is done. The following kinds of input file names are supported:
136
137@table @gcctabopt
138@item @var{file}.d
139D source files.
140@item @var{file}.dd
141Ddoc source files.
142@item @var{file}.di
143D interface files.
144@end table
145
146You can specify more than one input file on the @command{gdc} command line,
147each being compiled separately in the compilation process. If you specify a
148@code{-o @var{file}} option, all the input files are compiled together,
149producing a single output file, named @var{file}. This is allowed even
150when using @code{-S} or @code{-c}.
151
152@cindex D interface files.
153A D interface file contains only what an import of the module needs,
154rather than the whole implementation of that module. They can be created
155by @command{gdc} from a D source file by using the @code{-H} option.
156When the compiler resolves an import declaration, it searches for matching
157@file{.di} files first, then for @file{.d}.
158
159@cindex Ddoc source files.
160A Ddoc source file contains code in the D macro processor language. It is
161primarily designed for use in producing user documentation from embedded
162comments, with a slight affinity towards HTML generation. If a @file{.d}
163source file starts with the string @code{Ddoc} then it is treated as general
164purpose documentation, not as a D source file.
165
166@node Runtime Options
167@section Runtime Options
168@cindex options, runtime
169
170These options affect the runtime behavior of programs compiled with
171@command{gdc}.
172
173@table @gcctabopt
174
175@item -fall-instantiations
176@cindex @option{-fall-instantiations}
177@cindex @option{-fno-all-instantiations}
178Generate code for all template instantiations. The default template emission
179strategy is to not generate code for declarations that were either
180instantiated speculatively, such as from @code{__traits(compiles, ...)}, or
181that come from an imported module not being compiled.
182
183@item -fno-assert
184@cindex @option{-fassert}
185@cindex @option{-fno-assert}
186Turn off code generation for @code{assert} contracts.
187
188@item -fno-bounds-check
189@cindex @option{-fbounds-check}
190@cindex @option{-fno-bounds-check}
191Turns off array bounds checking for all functions, which can improve
192performance for code that uses arrays extensively. Note that this
193can result in unpredictable behavior if the code in question actually
194does violate array bounds constraints. It is safe to use this option
195if you are sure that your code never throws a @code{RangeError}.
196
197@item -fbounds-check=@var{value}
198@cindex @option{-fbounds-check=}
199An alternative to @option{-fbounds-check} that allows more control
200as to where bounds checking is turned on or off. The following values
201are supported:
202
203@table @samp
204@item on
205Turns on array bounds checking for all functions.
206@item safeonly
207Turns on array bounds checking only for @code{@@safe} functions.
208@item off
209Turns off array bounds checking completely.
210@end table
211
212@item -fno-builtin
213@cindex @option{-fbuiltin}
214@cindex @option{-fno-builtin}
215Don't recognize built-in functions unless they begin with the prefix
216@samp{__builtin_}. By default, the compiler will recognize when a
217function in the @code{core.stdc} package is a built-in function.
218
219@item -fdebug
220@item -fdebug=@var{value}
221@cindex @option{-fdebug}
222@cindex @option{-fno-debug}
223Turn on compilation of conditional @code{debug} code into the program.
224The @option{-fdebug} option itself sets the debug level to @code{1},
225while @option{-fdebug=} enables @code{debug} code that are identified
226by any of the following values:
227
228@table @samp
229@item level
230Sets the debug level to @var{level}, any @code{debug} code <= @var{level}
231is compiled into the program.
232@item ident
233Turns on compilation of any @code{debug} code identified by @var{ident}.
234@end table
235
236@item -fno-invariants
237@cindex @option{-finvariants}
238@cindex @option{-fno-invariants}
239Turns off code generation for class @code{invariant} contracts.
240
241@item -fno-moduleinfo
242@cindex @option{-fmoduleinfo}
243@cindex @option{-fno-moduleinfo}
244Turns off generation of the @code{ModuleInfo} and related functions
245that would become unreferenced without it, which may allow linking
246to programs not written in D. Functions that are not be generated
247include module constructors and destructors (@code{static this} and
248@code{static ~this}), @code{unittest} code, and @code{DSO} registry
249functions for dynamically linked code.
250
251@item -fonly=@var{filename}
252@cindex @option{-fonly}
253Tells the compiler to parse and run semantic analysis on all modules
254on the command line, but only generate code for the module specified
255by @var{filename}.
256
257@item -fno-postconditions
258@cindex @option{-fpostconditions}
259@cindex @option{-fno-postconditions}
260Turns off code generation for postcondition @code{out} contracts.
261
262@item -fno-preconditions
263@cindex @option{-fpreconditions}
264@cindex @option{-fno-preconditions}
265Turns off code generation for precondition @code{in} contracts.
266
267@item -frelease
268@cindex @option{-frelease}
269@cindex @option{-fno-release}
270Turns on compiling in release mode, which means not emitting runtime
271checks for contracts and asserts. Array bounds checking is not done
272for @code{@@system} and @code{@@trusted} functions, and assertion
273failures are undefined behavior.
274
275This is equivalent to compiling with the following options:
276
277@example
278gdc -fno-assert -fbounds-check=safe -fno-invariants \
279 -fno-postconditions -fno-preconditions -fno-switch-errors
280@end example
281
282@item -fno-switch-errors
283@cindex @option{-fswitch-errors}
284@cindex @option{-fno-switch-errors}
285This option controls what code is generated when no case is matched
286in a @code{final switch} statement. The default run time behavior
287is to throw a @code{SwitchError}. Turning off @option{-fswitch-errors}
288means that instead the execution of the program is immediately halted.
289
290@item -funittest
291@cindex @option{-funittest}
292@cindex @option{-fno-unittest}
293Turns on compilation of @code{unittest} code, and turns on the
294@code{version(unittest)} identifier. This implies @option{-fassert}.
295
296@item -fversion=@var{value}
297@cindex @option{-fversion}
298Turns on compilation of conditional @code{version} code into the program
299identified by any of the following values:
300
301@table @samp
302@item level
303Sets the version level to @var{level}, any @code{version} code >= @var{level}
304is compiled into the program.
305@item ident
306Turns on compilation of @code{version} code identified by @var{ident}.
307@end table
308
309@item -fno-weak
310@cindex @option{-fweak}
311@cindex @option{-fno-weak}
312Turns off emission of instantiated declarations that can be defined in multiple
313objects as weak or one-only symbols. The default is to emit all public symbols
314as weak, unless the target lacks support for weak symbols. Disabling this
315option means that common symbols are instead put in COMDAT or become private.
316
317@end table
318
319@node Directory Options
320@section Options for Directory Search
321@cindex directory options
322@cindex options, directory search
323@cindex search path
324
325These options specify directories to search for files, libraries, and
326other parts of the compiler:
327
328@table @gcctabopt
329
330@item -I@var{dir}
331@cindex @option{-I}
332Specify a directory to use when searching for imported modules at
333compile time. Multiple @option{-I} options can be used, and the
334paths are searched in the same order.
335
336@item -J@var{dir}
337@cindex @option{-J}
338Specify a directory to use when searching for files in string imports
339at compile time. This switch is required in order to use
340@code{import(file)} expressions. Multiple @option{-J} options can be
341used, and the paths are searched in the same order.
342
343@item -L@var{dir}
344@cindex @option{-L}
345When linking, specify a library search directory, as with @command{gcc}.
346
347@item -B@var{dir}
348@cindex @option{-B}
349This option specifies where to find the executables, libraries,
350source files, and data files of the compiler itself, as with @command{gcc}.
351
352@item -fmodule-file=@var{module}=@var{spec}
353@cindex @option{-fmodule-file}
354This option manipulates file paths of imported modules, such that if an
355imported module matches all or the leftmost part of @var{module}, the file
356path in @var{spec} is used as the location to search for D sources.
357This is used when the source file path and names are not the same as the
358package and module hierarchy. Consider the following examples:
359
360@example
361gdc test.d -fmodule-file=A.B=foo.d -fmodule-file=C=bar
362@end example
363
364This will tell the compiler to search in all import paths for the source
365file @var{foo.d} when importing @var{A.B}, and the directory @var{bar/}
366when importing @var{C}, as annotated in the following D code:
367
368@example
369module test;
370import A.B; // Matches A.B, searches for foo.d
371import C.D.E; // Matches C, searches for bar/D/E.d
372import A.B.C; // No match, searches for A/B/C.d
373@end example
374
375@item -imultilib @var{dir}
376@cindex @option{-imultilib}
377Use @var{dir} as a subdirectory of the gcc directory containing
378target-specific D sources and interfaces.
379
380@item -iprefix @var{prefix}
381@cindex @option{-iprefix}
382Specify @var{prefix} as the prefix for the gcc directory containing
383target-specific D sources and interfaces. If the @var{prefix} represents
384a directory, you should include the final @code{'/'}.
385
386@item -nostdinc
387@cindex @option{-nostdinc}
388Do not search the standard system directories for D source and interface
389files. Only the directories that have been specified with @option{-I} options
390(and the directory of the current file, if appropriate) are searched.
391
392@end table
393
394@node Code Generation
395@section Code Generation
396@cindex options, code generation
397
398In addition to the many @command{gcc} options controlling code generation,
399@command{gdc} has several options specific to itself.
400
401@table @gcctabopt
402
403@item -H
404@cindex @option{-H}
405Generates D interface files for all modules being compiled. The compiler
406determines the output file based on the name of the input file, removes
407any directory components and suffix, and applies the @file{.di} suffix.
408
409@item -Hd @var{dir}
410@cindex @option{-Hd}
411Same as @option{-H}, but writes interface files to directory @var{dir}.
412This option can be used with @option{-Hf @var{file}} to independently set the
413output file and directory path.
414
415@item -Hf @var{file}
416@cindex @option{-Hf}
417Same as @option{-H} but writes interface files to @var{file}. This option can
418be used with @option{-Hd @var{dir}} to independently set the output file and
419directory path.
420
421@item -M
422@cindex @option{-M}
423Output the module dependencies of all source files being compiled in a
424format suitable for @command{make}. The compiler outputs one
425@command{make} rule containing the object file name for that source file,
426a colon, and the names of all imported files.
427
428@item -MM
429@cindex @option{-MM}
430Like @option{-M} but does not mention imported modules from the D standard
431library package directories.
432
433@item -MF @var{file}
434@cindex @option{-MF}
435When used with @option{-M} or @option{-MM}, specifies a @var{file} to write
436the dependencies to. When used with the driver options @option{-MD} or
437@option{-MMD}, @option{-MF} overrides the default dependency output file.
438
439@item -MG
440@cindex @option{-MG}
441This option is for compatibility with @command{gcc}, and is ignored by the
442compiler.
443
444@item -MP
445@cindex @option{-MP}
446Outputs a phony target for each dependency other than the modules being
447compiled, causing each to depend on nothing.
448
449@item -MT @var{target}
450@cindex @option{-MT}
451Change the @var{target} of the rule emitted by dependency generation
452to be exactly the string you specify. If you want multiple targets,
453you can specify them as a single argument to @option{-MT}, or use
454multiple @option{-MT} options.
455
456@item -MQ @var{target}
457@cindex @option{-MQ}
458Same as @option{-MT}, but it quotes any characters which are special to
459@command{make}.
460
461@item -MD
462@cindex @option{-MD}
463This option is equivalent to @option{-M -MF @var{file}}. The driver
464determines @var{file} by removing any directory components and suffix
465from the input file, and then adding a @file{.deps} suffix.
466
467@item -MMD
468@cindex @option{-MMD}
469Like @option{-MD} but does not mention imported modules from the D standard
470library package directories.
471
472@item -X
473@cindex @option{-X}
474Output information describing the contents of all source files being
475compiled in JSON format to a file. The driver determines @var{file} by
476removing any directory components and suffix from the input file, and then
477adding a @file{.json} suffix.
478
479@item -Xf @var{file}
480@cindex @option{-Xf}
481Same as @option{-X}, but writes all JSON contents to the specified
482@var{file}.
483
484@item -fdoc
485@cindex @option{-fdoc}
486Generates @code{Ddoc} documentation and writes it to a file. The compiler
487determines @var{file} by removing any directory components and suffix
488from the input file, and then adding a @file{.html} suffix.
489
490@item -fdoc-dir=@var{dir}
491@cindex @option{-fdoc-dir}
492Same as @option{-fdoc}, but writes documentation to directory @var{dir}.
493This option can be used with @option{-fdoc-file=@var{file}} to
494independently set the output file and directory path.
495
496@item -fdoc-file=@var{file}
497@cindex @option{-fdoc-file}
498Same as @option{-fdoc}, but writes documentation to @var{file}. This
499option can be used with @option{-fdoc-dir=@var{dir}} to independently
500set the output file and directory path.
501
502@item -fdoc-inc=@var{file}
503@cindex @option{-fdoc-inc}
504Specify @var{file} as a @var{Ddoc} macro file to be read. Multiple
505@option{-fdoc-inc} options can be used, and files are read and processed
506in the same order.
507
508@end table
509
510@node Warnings
511@section Warnings
512@cindex options to control warnings
513@cindex warning messages
514@cindex messages, warning
515@cindex suppressing warnings
516
517Warnings are diagnostic messages that report constructions that
518are not inherently erroneous but that are risky or suggest there
519is likely to be a bug in the program. Unless @option{-Werror} is
520specified, they do not prevent compilation of the program.
521
522@table @gcctabopt
523
524@item -Wall
525@cindex @option{-Wall}
526@cindex @option{-Wno-all}
527Turns on all warnings messages. Warnings are not a defined part of
528the D language, and all constructs for which this may generate a
529warning message are valid code.
530
531@item -Walloca
532@cindex @option{-Walloca}
533This option warns on all uses of "alloca" in the source.
534
535@item -Walloca-larger-than=@var{n}
536@cindex @option{-Walloca-larger-than}
537@cindex @option{-Wno-alloca-larger-than}
538Warn on unbounded uses of alloca, and on bounded uses of alloca
539whose bound can be larger than @var{n} bytes.
540@option{-Wno-alloca-larger-than} disables
541@option{-Walloca-larger-than} warning and is equivalent to
542@option{-Walloca-larger-than=@var{SIZE_MAX}} or larger.
543
544@item -Wcast-result
545@cindex @option{-Wcast-result}
546@cindex @option{-Wno-cast-result}
547Warn about casts that will produce a null or zero result. Currently
548this is only done for casting between an imaginary and non-imaginary
549data type, or casting between a D and C++ class.
550
551@item -Wno-deprecated
552@cindex @option{-Wdeprecated}
553@cindex @option{-Wno-deprecated}
554Do not warn about usage of deprecated features and symbols with
555@code{deprecated} attributes.
556
557@item -Werror
558@cindex @option{-Werror}
559@cindex @option{-Wno-error}
560Turns all warnings into errors.
561
562@item -Wspeculative
563@cindex @option{-Wspeculative}
564@cindex @option{-Wno-speculative}
565List all error messages from speculative compiles, such as
566@code{__traits(compiles, ...)}. This option does not report
567messages as warnings, and these messages therefore never become
568errors when the @option{-Werror} option is also used.
569
570@item -Wtemplates
571@cindex @option{-Wtemplates}
572@cindex @option{-Wno-templates}
573Warn when a template instantiation is encountered. Some coding
574rules disallow templates, and this may be used to enforce that rule.
575
576@item -Wunknown-pragmas
577@cindex @option{-Wunknown-pragmas}
578@cindex @option{-Wno-unknown-pragmas}
579Warn when a @code{pragma()} is encountered that is not understood by
580@command{gdc}. This differs from @option{-fignore-unknown-pragmas}
581where a pragma that is part of the D language, but not implemented by
582the compiler, won't get reported.
583
584@item -fignore-unknown-pragmas
585@cindex @option{-fignore-unknown-pragmas}
586@cindex @option{-fno-ignore-unknown-pragmas}
587Turns off errors for unsupported pragmas.
588
589@item -fmax-errors=@var{n}
590@cindex @option{-fmax-errors}
591Limits the maximum number of error messages to @var{n}, at which point
592@command{gdc} bails out rather than attempting to continue processing the
593source code. If @var{n} is 0 (the default), there is no limit on the
594number of error messages produced.
595
596@item -fsyntax-only
597@cindex @option{-fsyntax-only}
598@cindex @option{-fno-syntax-only}
599Check the code for syntax errors, but do not actually compile it. This
600can be used in conjunction with @option{-fdoc} or @option{-H} to generate
601files for each module present on the command-line, but no other output
602file.
603
604@item -ftransition=@var{id}
605@cindex @option{-ftransition}
606Report additional information about D language changes identified by
607@var{id}. The following values are supported:
608
609@table @samp
610@item all
611List information on all language changes.
612@item checkimports
613Give deprecation messages about @option{-ftransition=import} anomalies.
614@item complex
615List all usages of complex or imaginary types.
616@item dip1000
617Implements @uref{http://wiki.dlang.org/DIP1000} (experimental).
618@item dip25
619Implements @uref{http://wiki.dlang.org/DIP25} (experimental).
620@item field
621List all non-mutable fields which occupy an object instance.
622@item import
623Tells the compiler to revert to using an old lookup behavior for resolving
624unqualified symbol names, where this was done in a single pass, ignoring
625any protection attributes. The default name lookup strategy is to use two
626passes, the first ignoring imported declarations, and the second only
627looking at imports. The protection (@code{private}, @code{package},
628@code{protected}) of symbols is also enforced to resolve any conflicts
629between private and public symbols.
630@item nogc
631List all hidden GC allocations.
632@item tls
633List all variables going into thread local storage.
634@end table
635
636@end table
637
638@node Linking
639@section Options for Linking
640@cindex options, linking
641@cindex linking, static
642
643These options come into play when the compiler links object files into an
644executable output file. They are meaningless if the compiler is not doing
645a link step.
646
647@table @gcctabopt
648
649@item -defaultlib @var{libname}
650@cindex @option{-defaultlib}
651Specify the library to use instead of libphobos when linking. Options
652specifying the linkage of libphobos, such as @option{-static-libphobos}
653or @option{-shared-libphobos}, are ignored.
654
655@item -debuglib
656@cindex @option{-debuglib}
657Specify the debug library to use instead of libphobos when linking.
658This option has no effect unless the @option{-g} option was also given
659on the command line. Options specifying the linkage of libphobos, such
660as @option{-static-libphobos} or @option{-shared-libphobos}, are ignored.
661
662@item -nophoboslib
663@cindex @option{-nophoboslib}
664Do not use the Phobos or D runtime library when linking. Options specifying
665the linkage of libphobos, such as @option{-static-libphobos} or
666@option{-shared-libphobos}, are ignored. The standard system libraries are
667used normally, unless @option{-nostdlib} or @option{-nodefaultlibs} is used.
668
669@item -shared-libphobos
670@cindex @option{-shared-libphobos}
671On systems that provide @file{libgphobos} and @file{libgdruntime} as a
672shared and a static library, this option forces the use of the shared
673version. If no shared version was built when the compiler was configured,
674this option has no effect.
675
676@item -static-libphobos
677@cindex @option{-static-libphobos}
678On systems that provide @file{libgphobos} and @file{libgdruntime} as a
679shared and a static library, this option forces the use of the static
680version. If no static version was built when the compiler was configured,
681this option has no effect.
682
683@end table
684
685@node Developer Options
686@section Developer Options
687@cindex developer options
688@cindex debug dump options
689@cindex dump options
690
691This section describes command-line options that are primarily of
692interest to developers or language tooling.
693
694@table @gcctabopt
695
696@item -fdump-d-original
697@cindex @option{-fdump-d-original}
698Output the internal front-end AST after the @code{semantic3} stage.
699This option is only useful for debugging the GNU D compiler itself.
700
701@item -v
702@cindex @option{-v}
703Dump information about the compiler language processing stages as the source
704program is being compiled. This includes listing all modules that are
705processed through the @code{parse}, @code{semantic}, @code{semantic2}, and
706@code{semantic3} stages; all @code{import} modules and their file paths;
707and all @code{function} bodies that are being compiled.
708
709@end table
710
711@c man end
712
713@node Index
714@unnumbered Index
715
716@printindex cp
717
718@bye