2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2023 by The D Language Foundation, All Rights Reserved
4 * written by Walter Bright
5 * https://www.digitalmars.com
6 * Distributed under the Boost Software License, Version 1.0.
7 * https://www.boost.org/LICENSE_1_0.txt
8 * https://github.com/dlang/dmd/blob/master/src/dmd/globals.h
13 #include "root/dcompat.h"
14 #include "root/ctfloat.h"
15 #include "common/outbuffer.h"
16 #include "root/filename.h"
19 // Can't include arraytypes.h here, need to declare these directly.
20 template <typename TYPE
> struct Array
;
26 typedef unsigned char Diagnostic
;
29 DIAGNOSTICerror
, // generate an error
30 DIAGNOSTICinform
, // generate a warning
31 DIAGNOSTICoff
// disable diagnostic
34 enum class MessageStyle
: unsigned char
36 digitalmars
, // file(line,column): message
37 gnu
// file:line:column: message
40 // The state of array bounds checking
41 typedef unsigned char CHECKENABLE
;
44 CHECKENABLEdefault
, // initial value
45 CHECKENABLEoff
, // never do bounds checking
46 CHECKENABLEon
, // always do bounds checking
47 CHECKENABLEsafeonly
// do bounds checking only in @safe functions
50 typedef unsigned char CHECKACTION
;
53 CHECKACTION_D
, // call D assert on failure
54 CHECKACTION_C
, // call C assert on failure
55 CHECKACTION_halt
, // cause program halt on failure
56 CHECKACTION_context
// call D assert with the error context on failure
62 compilerInfo
= (1 << 0),
70 CppStdRevisionCpp98
= 199711,
71 CppStdRevisionCpp11
= 201103,
72 CppStdRevisionCpp14
= 201402,
73 CppStdRevisionCpp17
= 201703,
74 CppStdRevisionCpp20
= 202002
77 /// Trivalent boolean to represent the state of a `revert`able change
78 enum class FeatureState
: unsigned char
80 default_
= 0, /// Not specified by the user
81 disabled
= 1, /// Specified as `-revert=`
82 enabled
= 2, /// Specified as `-preview=`
87 /// Configuration for the compiler generator
88 d_bool doOutput
; // Output is enabled
89 d_bool fullOutput
; // Generate comments for hidden declarations (for -HC),
90 // and don't strip the bodies of plain (non-template) functions (for -H)
91 DString dir
; // write to directory 'dir'
92 DString name
; // write to file 'name'
93 Array
<const char*> files
; // Other files associated with this output,
94 // e.g. macro include files for Ddoc, dependencies for makedeps
95 OutBuffer
* buffer
; // if this output is buffered, this is the buffer
96 int bufferLines
; // number of lines written to the buffer
99 /// Command line state related to printing uasage about other switches
102 d_bool manual
; // open browser on compiler manual
103 d_bool usage
; // print usage and exit
104 // print help of switch:
105 d_bool mcpu
; // -mcpu
106 d_bool transition
; // -transition
107 d_bool check
; // -check
108 d_bool checkAction
; // -checkaction
109 d_bool revert
; // -revert
110 d_bool preview
; // -preview
111 d_bool externStd
; // -extern-std
117 d_bool verbose
; // verbose compile
118 d_bool showColumns
; // print character (column) numbers in diagnostics
119 d_bool tls
; // identify thread local variables
120 d_bool templates
; // collect and list statistics on template instantiations
121 // collect and list statistics on template instantiations origins.
122 // TODO: make this an enum when we want to list other kinds of instances
123 d_bool templatesListInstances
;
124 d_bool gc
; // identify gc usage
125 d_bool field
; // identify non-mutable field variables
126 d_bool
complex = true; // identify complex/imaginary type usage
127 d_bool vin
; // identify 'in' parameters
128 d_bool showGaggedErrors
; // print gagged errors anyway
129 d_bool printErrorContext
; // print errors with the error context (the error line in the source file)
130 d_bool logo
; // print compiler logo
131 d_bool color
; // use ANSI colors in console output
132 d_bool cov
; // generate code coverage data
133 MessageStyle messageStyle
; // style of file/line annotations on messages
135 unsigned errorSupplementLimit
; // Limit the number of supplemental messages for each error (0 means unlimited)
136 unsigned errorSupplementCount();
139 // Put command line switches in here
142 d_bool obj
; // write object file
143 d_bool multiobj
; // break one object file into multiple ones
144 d_bool trace
; // insert profiling hooks
145 d_bool tracegc
; // instrument calls to 'new'
146 d_bool vcg_ast
; // write-out codegen-ast
147 Diagnostic useDeprecated
;
148 d_bool useUnitTests
; // generate unittest code
149 d_bool useInline
; // inline expand functions
150 d_bool release
; // build release version
151 d_bool preservePaths
; // true means don't strip path from source file
153 d_bool cov
; // generate code coverage data
154 unsigned char covPercent
; // 0..100 code coverage percentage required
155 d_bool ctfe_cov
; // generate coverage data for ctfe
156 d_bool ignoreUnsupportedPragmas
; // rather than error on them
157 d_bool useModuleInfo
; // generate runtime module information
158 d_bool useTypeInfo
; // generate runtime type information
159 d_bool useExceptions
; // support exception handling
160 d_bool useGC
; // support features that require the D runtime GC
161 d_bool betterC
; // be a "better C" compiler; no dependency on D runtime
162 d_bool addMain
; // add a default main() function
163 d_bool allInst
; // generate code for all template instantiations
164 d_bool bitfields
; // support C style bit fields
165 CppStdRevision cplusplus
; // version of C++ name mangling to support
170 // Options for `-preview=/-revert=`
171 FeatureState useDIP25
; // implement https://wiki.dlang.org/DIP25
172 FeatureState useDIP1000
; // implement https://dlang.org/spec/memory-safe-d.html#scope-return-params
173 d_bool ehnogc
; // use @nogc exception handling
174 d_bool useDIP1021
; // implement https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1021.md
175 FeatureState fieldwise
; // do struct equality testing field-wise rather than by memcmp()
176 d_bool fixAliasThis
; // if the current scope has an alias this, check it before searching upper scopes
177 FeatureState rvalueRefParam
; // allow rvalues to be arguments to ref parameters
178 // https://dconf.org/2019/talks/alexandrescu.html
179 // https://gist.github.com/andralex/e5405a5d773f07f73196c05f8339435a
180 // https://digitalmars.com/d/archives/digitalmars/D/Binding_rvalues_to_ref_parameters_redux_325087.html
181 // Implementation: https://github.com/dlang/dmd/pull/9817
182 FeatureState noSharedAccess
; // read/write access to shared memory objects
183 d_bool previewIn
; // `in` means `[ref] scope const`, accepts rvalues
184 d_bool inclusiveInContracts
; // 'in' contracts of overridden methods must be a superset of parent contract
185 d_bool shortenedMethods
; // allow => in normal function declarations
186 d_bool fixImmutableConv
; // error on unsound immutable conversion - https://github.com/dlang/dmd/pull/14070
187 d_bool fix16997
; // fix integral promotions for unary + - ~ operators
188 // https://issues.dlang.org/show_bug.cgi?id=16997
189 FeatureState dtorFields
; // destruct fields of partially constructed objects
190 // https://issues.dlang.org/show_bug.cgi?id=14246
191 FeatureState systemVariables
; // limit access to variables marked @system from @safe code
193 CHECKENABLE useInvariants
; // generate class invariant checks
194 CHECKENABLE useIn
; // generate precondition checks
195 CHECKENABLE useOut
; // generate postcondition checks
196 CHECKENABLE useArrayBounds
; // when to generate code for array bounds checks
197 CHECKENABLE useAssert
; // when to generate code for assert()'s
198 CHECKENABLE useSwitchError
; // check for switches without a default
199 CHECKENABLE boundscheck
; // state of -boundscheck switch
201 CHECKACTION checkAction
; // action to take when bounds, asserts or switch defaults are violated
203 DString argv0
; // program name
204 Array
<const char *> modFileAliasStrings
; // array of char*'s of -I module filename alias strings
205 Array
<const char *> *imppath
; // array of char*'s of where to look for import modules
206 Array
<const char *> *fileImppath
; // array of char*'s of where to look for file import modules
207 DString objdir
; // .obj/.lib file output directory
208 DString objname
; // .obj file output name
209 DString libname
; // .lib file output name
211 Output ddoc
; // Generate embedded documentation comments
212 Output dihdr
; // Generate `.di` 'header' files
213 Output cxxhdr
; // Generate 'Cxx header' file
214 Output json
; // Generate JSON file
215 unsigned jsonFieldFlags
; // JSON field flags to include
216 Output makeDeps
; // Generate make file dependencies
217 Output mixinOut
; // write expanded mixins for debugging
218 Output moduleDeps
; // Generate `.deps` module dependencies
220 unsigned debuglevel
; // debug level
221 unsigned versionlevel
; // version level
223 d_bool run
; // run resulting executable
224 Strings runargs
; // arguments for executable
226 Array
<const char *> cppswitches
; // preprocessor switches
227 const char *cpp
; // if not null, then this specifies the C preprocessor
230 Array
<const char *> objfiles
;
231 Array
<const char *> linkswitches
;
232 Array
<bool> linkswitchIsForCC
;
233 Array
<const char *> libfiles
;
234 Array
<const char *> dllfiles
;
243 unsigned short value
;
246 bool isDefault() const;
248 bool isUnknown() const;
250 void set(unsigned value
);
251 unsigned get() const;
253 void setPack(bool pack
);
256 // magic value means "match whatever the underlying C compiler does"
257 // other values are all powers of 2
258 //#define STRUCTALIGN_DEFAULT ((structalign_t) ~0)
260 const DString mars_ext
= "d";
261 const DString doc_ext
= "html"; // for Ddoc generated files
262 const DString ddoc_ext
= "ddoc"; // for Ddoc macro include files
263 const DString dd_ext
= "dd"; // for Ddoc source files
264 const DString hdr_ext
= "di"; // for D 'header' import files
265 const DString json_ext
= "json"; // for JSON files
266 const DString map_ext
= "map"; // for .map files
270 uint32_t versionNumber
;
277 d_bool shortenedMethods
;
284 const DString copyright
;
285 const DString written
;
286 Array
<const char *> *path
; // Array of char*'s which form the import lookup path
287 Array
<const char *> *filePath
; // Array of char*'s which form the file import lookup path
289 char datetime
[26]; /// string returned by ctime()
290 CompileEnv compileEnv
;
293 unsigned errors
; // number of errors reported so far
294 unsigned warnings
; // number of warnings reported so far
295 unsigned gag
; // !=0 means gag reporting of errors & warnings
296 unsigned gaggedErrors
; // number of errors reported while gagged
297 unsigned gaggedWarnings
; // number of warnings reported while gagged
299 void* console
; // opaque pointer to console for controlling text attributes
301 Array
<class Identifier
*>* versionids
; // command line versions and predefined versions
302 Array
<class Identifier
*>* debugids
; // command line debug versions and predefined versions
304 d_bool hasMainFunction
;
305 unsigned varSequenceNumber
;
307 FileManager
* fileManager
;
308 ErrorSink
* errorSink
; // where the error messages go
309 ErrorSink
* errorSinkNull
; // where the error messages disappear
311 FileName (*preprocess
)(FileName
, const Loc
&, bool&, OutBuffer
&);
313 /* Start gagging. Return the current number of gagged errors
315 unsigned startGagging();
317 /* End gagging, restoring the old gagged state.
318 * Return true if errors occurred while gagged.
320 bool endGagging(unsigned oldGagged
);
322 /* Increment the error count to record that an error
323 * has occurred in the current context. An error message
324 * may or may not have been printed.
326 void increaseErrorCount();
331 Returns: the version as the number that would be returned for __VERSION__
333 unsigned versionNumber();
336 Returns: the compiler version string.
338 const char * versionChars();
341 extern Global global
;
343 // Because int64_t and friends may be any integral type of the correct size,
344 // we have to explicitly ask for the correct integer type to get the correct
345 // mangling with dmd. The #if logic here should match the mangling of
346 // Tint64 and Tuns64 in cppmangle.d.
347 #if MARS && DMD_VERSION >= 2079 && DMD_VERSION <= 2081 && \
348 __APPLE__ && __SIZEOF_LONG__ == 8
349 // DMD versions between 2.079 and 2.081 mapped D long to int64_t on OS X.
350 typedef uint64_t dinteger_t
;
351 typedef int64_t sinteger_t
;
352 typedef uint64_t uinteger_t
;
353 #elif __SIZEOF_LONG__ == 8
354 // Be careful not to care about sign when using dinteger_t
355 // use this instead of integer_t to
356 // avoid conflicts with system #include's
357 typedef unsigned long dinteger_t
;
358 // Signed and unsigned variants
359 typedef long sinteger_t
;
360 typedef unsigned long uinteger_t
;
362 typedef unsigned long long dinteger_t
;
363 typedef long long sinteger_t
;
364 typedef unsigned long long uinteger_t
;
375 static void set(bool showColumns
, MessageStyle messageStyle
);
377 static bool showColumns
;
378 static MessageStyle messageStyle
;
387 Loc(const char *filename
, unsigned linnum
, unsigned charnum
)
389 this->linnum(linnum
);
390 this->charnum(charnum
);
391 this->filename(filename
);
394 uint32_t charnum() const;
395 uint32_t charnum(uint32_t num
);
396 uint32_t linnum() const;
397 uint32_t linnum(uint32_t num
);
398 const char *filename() const;
399 void filename(const char *name
);
402 bool showColumns
= Loc::showColumns
,
403 MessageStyle messageStyle
= Loc::messageStyle
) const;
404 bool equals(const Loc
& loc
) const;
407 enum class LINK
: uint8_t
418 enum class CPPMANGLE
: uint8_t
425 enum class MATCH
: int
428 convert
, // match with conversions
429 constant
, // match with conversion to const
433 enum class PINLINE
: uint8_t
435 default_
, // as specified on the command line
436 never
, // never inline
437 always
// always inline
440 enum class FileType
: uint8_t
442 d
, /// normal D source file
443 dhdr
, /// D header file (.di)
444 ddoc
, /// Ddoc documentation file (.dd)
448 typedef uinteger_t StorageClass
;