d: Merge upstream dmd, druntime 26f049fb26, phobos 330d6a4fd.
[official-gcc.git] / gcc / d / dmd / globals.h
blob0dad5dd12e78e5bb959384b6a24493f1ad916ca9
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
9 */
11 #pragma once
13 #include "root/dcompat.h"
14 #include "root/ctfloat.h"
15 #include "common/outbuffer.h"
16 #include "root/filename.h"
17 #include "compiler.h"
19 // Can't include arraytypes.h here, need to declare these directly.
20 template <typename TYPE> struct Array;
22 class ErrorSink;
23 class FileManager;
24 struct Loc;
26 typedef unsigned char Diagnostic;
27 enum
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;
42 enum
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;
51 enum
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
59 enum JsonFieldFlags
61 none = 0,
62 compilerInfo = (1 << 0),
63 buildInfo = (1 << 1),
64 modules = (1 << 2),
65 semantics = (1 << 3)
68 enum CppStdRevision
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=`
85 struct Output
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 // Put command line switches in here
100 struct Param
102 d_bool obj; // write object file
103 d_bool multiobj; // break one object file into multiple ones
104 d_bool trace; // insert profiling hooks
105 d_bool tracegc; // instrument calls to 'new'
106 d_bool verbose; // verbose compile
107 d_bool vcg_ast; // write-out codegen-ast
108 d_bool showColumns; // print character (column) numbers in diagnostics
109 d_bool vtls; // identify thread local variables
110 d_bool vtemplates; // collect and list statistics on template instantiations
111 d_bool vtemplatesListInstances; // collect and list statistics on template instantiations origins
112 d_bool vgc; // identify gc usage
113 d_bool vfield; // identify non-mutable field variables
114 d_bool vcomplex; // identify complex/imaginary type usage
115 d_bool vin; // identify 'in' parameters
116 Diagnostic useDeprecated;
117 d_bool useUnitTests; // generate unittest code
118 d_bool useInline; // inline expand functions
119 d_bool release; // build release version
120 d_bool preservePaths; // true means don't strip path from source file
121 Diagnostic warnings;
122 d_bool obsolete; // warn about use of obsolete features
123 d_bool color; // use ANSI colors in console output
124 d_bool cov; // generate code coverage data
125 unsigned char covPercent; // 0..100 code coverage percentage required
126 d_bool ctfe_cov; // generate coverage data for ctfe
127 d_bool ignoreUnsupportedPragmas; // rather than error on them
128 d_bool useModuleInfo; // generate runtime module information
129 d_bool useTypeInfo; // generate runtime type information
130 d_bool useExceptions; // support exception handling
131 d_bool useGC; // support features that require the GC
132 d_bool betterC; // be a "better C" compiler; no dependency on D runtime
133 d_bool addMain; // add a default main() function
134 d_bool allInst; // generate code for all template instantiations
135 d_bool bitfields; // support C style bit fields
136 CppStdRevision cplusplus; // version of C++ name mangling to support
137 d_bool showGaggedErrors; // print gagged errors anyway
138 d_bool printErrorContext; // print errors with the error context (the error line in the source file)
139 d_bool manual; // open browser on compiler manual
140 d_bool usage; // print usage and exit
141 d_bool mcpuUsage; // print help on -mcpu switch
142 d_bool transitionUsage; // print help on -transition switch
143 d_bool checkUsage; // print help on -check switch
144 d_bool checkActionUsage; // print help on -checkaction switch
145 d_bool revertUsage; // print help on -revert switch
146 d_bool previewUsage; // print help on -preview switch
147 d_bool externStdUsage; // print help on -extern-std switch
148 d_bool hcUsage; // print help on -HC switch
149 d_bool logo; // print logo;
151 // Options for `-preview=/-revert=`
152 FeatureState useDIP25; // implement https://wiki.dlang.org/DIP25
153 FeatureState useDIP1000; // implement https://dlang.org/spec/memory-safe-d.html#scope-return-params
154 d_bool ehnogc; // use @nogc exception handling
155 d_bool useDIP1021; // implement https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1021.md
156 FeatureState fieldwise; // do struct equality testing field-wise rather than by memcmp()
157 d_bool fixAliasThis; // if the current scope has an alias this, check it before searching upper scopes
158 FeatureState rvalueRefParam; // allow rvalues to be arguments to ref parameters
159 // https://dconf.org/2019/talks/alexandrescu.html
160 // https://gist.github.com/andralex/e5405a5d773f07f73196c05f8339435a
161 // https://digitalmars.com/d/archives/digitalmars/D/Binding_rvalues_to_ref_parameters_redux_325087.html
162 // Implementation: https://github.com/dlang/dmd/pull/9817
163 FeatureState noSharedAccess; // read/write access to shared memory objects
164 d_bool previewIn; // `in` means `[ref] scope const`, accepts rvalues
165 d_bool inclusiveInContracts; // 'in' contracts of overridden methods must be a superset of parent contract
166 d_bool shortenedMethods; // allow => in normal function declarations
167 d_bool fixImmutableConv; // error on unsound immutable conversion - https://github.com/dlang/dmd/pull/14070
168 d_bool fix16997; // fix integral promotions for unary + - ~ operators
169 // https://issues.dlang.org/show_bug.cgi?id=16997
170 FeatureState dtorFields; // destruct fields of partially constructed objects
171 // https://issues.dlang.org/show_bug.cgi?id=14246
172 FeatureState systemVariables; // limit access to variables marked @system from @safe code
174 CHECKENABLE useInvariants; // generate class invariant checks
175 CHECKENABLE useIn; // generate precondition checks
176 CHECKENABLE useOut; // generate postcondition checks
177 CHECKENABLE useArrayBounds; // when to generate code for array bounds checks
178 CHECKENABLE useAssert; // when to generate code for assert()'s
179 CHECKENABLE useSwitchError; // check for switches without a default
180 CHECKENABLE boundscheck; // state of -boundscheck switch
182 CHECKACTION checkAction; // action to take when bounds, asserts or switch defaults are violated
184 unsigned errorLimit;
185 unsigned errorSupplementLimit; // Limit the number of supplemental messages for each error (0 means unlimited)
187 DString argv0; // program name
188 Array<const char *> modFileAliasStrings; // array of char*'s of -I module filename alias strings
189 Array<const char *> *imppath; // array of char*'s of where to look for import modules
190 Array<const char *> *fileImppath; // array of char*'s of where to look for file import modules
191 DString objdir; // .obj/.lib file output directory
192 DString objname; // .obj file output name
193 DString libname; // .lib file output name
195 Output ddoc; // Generate embedded documentation comments
196 Output dihdr; // Generate `.di` 'header' files
197 Output cxxhdr; // Generate 'Cxx header' file
198 Output json; // Generate JSON file
199 unsigned jsonFieldFlags; // JSON field flags to include
200 Output makeDeps; // Generate make file dependencies
201 Output mixinOut; // write expanded mixins for debugging
202 Output moduleDeps; // Generate `.deps` module dependencies
204 unsigned debuglevel; // debug level
205 Array<const char *> *debugids; // debug identifiers
207 unsigned versionlevel; // version level
208 Array<const char *> *versionids; // version identifiers
211 MessageStyle messageStyle; // style of file/line annotations on messages
213 d_bool run; // run resulting executable
214 Strings runargs; // arguments for executable
216 Array<const char *> cppswitches; // preprocessor switches
217 const char *cpp; // if not null, then this specifies the C preprocessor
219 // Linker stuff
220 Array<const char *> objfiles;
221 Array<const char *> linkswitches;
222 Array<bool> linkswitchIsForCC;
223 Array<const char *> libfiles;
224 Array<const char *> dllfiles;
225 DString deffile;
226 DString resfile;
227 DString exefile;
228 DString mapfile;
231 struct structalign_t
233 unsigned short value;
234 d_bool pack;
236 bool isDefault() const;
237 void setDefault();
238 bool isUnknown() const;
239 void setUnknown();
240 void set(unsigned value);
241 unsigned get() const;
242 bool isPack() const;
243 void setPack(bool pack);
246 // magic value means "match whatever the underlying C compiler does"
247 // other values are all powers of 2
248 //#define STRUCTALIGN_DEFAULT ((structalign_t) ~0)
250 const DString mars_ext = "d";
251 const DString doc_ext = "html"; // for Ddoc generated files
252 const DString ddoc_ext = "ddoc"; // for Ddoc macro include files
253 const DString dd_ext = "dd"; // for Ddoc source files
254 const DString hdr_ext = "di"; // for D 'header' import files
255 const DString json_ext = "json"; // for JSON files
256 const DString map_ext = "map"; // for .map files
258 struct CompileEnv
260 uint32_t versionNumber;
261 DString date;
262 DString time;
263 DString vendor;
264 DString timestamp;
265 bool previewIn;
266 bool ddocOutput;
267 bool shortenedMethods;
268 bool obsolete;
271 struct Global
273 DString inifilename;
275 const DString copyright;
276 const DString written;
277 Array<const char *> *path; // Array of char*'s which form the import lookup path
278 Array<const char *> *filePath; // Array of char*'s which form the file import lookup path
280 CompileEnv compileEnv;
282 Param params;
283 unsigned errors; // number of errors reported so far
284 unsigned warnings; // number of warnings reported so far
285 unsigned gag; // !=0 means gag reporting of errors & warnings
286 unsigned gaggedErrors; // number of errors reported while gagged
287 unsigned gaggedWarnings; // number of warnings reported while gagged
289 void* console; // opaque pointer to console for controlling text attributes
291 Array<class Identifier*>* versionids; // command line versions and predefined versions
292 Array<class Identifier*>* debugids; // command line debug versions and predefined versions
294 d_bool hasMainFunction;
295 unsigned varSequenceNumber;
297 FileManager* fileManager;
298 ErrorSink* errorSink; // where the error messages go
300 FileName (*preprocess)(FileName, const Loc&, bool&, OutBuffer&);
302 /* Start gagging. Return the current number of gagged errors
304 unsigned startGagging();
306 /* End gagging, restoring the old gagged state.
307 * Return true if errors occurred while gagged.
309 bool endGagging(unsigned oldGagged);
311 /* Increment the error count to record that an error
312 * has occurred in the current context. An error message
313 * may or may not have been printed.
315 void increaseErrorCount();
317 void _init();
320 Returns: the version as the number that would be returned for __VERSION__
322 unsigned versionNumber();
325 Returns: the compiler version string.
327 const char * versionChars();
330 extern Global global;
332 // Because int64_t and friends may be any integral type of the correct size,
333 // we have to explicitly ask for the correct integer type to get the correct
334 // mangling with dmd. The #if logic here should match the mangling of
335 // Tint64 and Tuns64 in cppmangle.d.
336 #if MARS && DMD_VERSION >= 2079 && DMD_VERSION <= 2081 && \
337 __APPLE__ && __SIZEOF_LONG__ == 8
338 // DMD versions between 2.079 and 2.081 mapped D long to int64_t on OS X.
339 typedef uint64_t dinteger_t;
340 typedef int64_t sinteger_t;
341 typedef uint64_t uinteger_t;
342 #elif __SIZEOF_LONG__ == 8
343 // Be careful not to care about sign when using dinteger_t
344 // use this instead of integer_t to
345 // avoid conflicts with system #include's
346 typedef unsigned long dinteger_t;
347 // Signed and unsigned variants
348 typedef long sinteger_t;
349 typedef unsigned long uinteger_t;
350 #else
351 typedef unsigned long long dinteger_t;
352 typedef long long sinteger_t;
353 typedef unsigned long long uinteger_t;
354 #endif
356 // file location
357 struct Loc
359 private:
360 unsigned _linnum;
361 unsigned short _charnum;
362 unsigned short fileIndex;
363 public:
364 static void set(bool showColumns, MessageStyle messageStyle);
366 static bool showColumns;
367 static MessageStyle messageStyle;
369 Loc()
371 _linnum = 0;
372 _charnum = 0;
373 fileIndex = 0;
376 Loc(const char *filename, unsigned linnum, unsigned charnum)
378 this->linnum(linnum);
379 this->charnum(charnum);
380 this->filename(filename);
383 uint32_t charnum() const;
384 uint32_t charnum(uint32_t num);
385 uint32_t linnum() const;
386 uint32_t linnum(uint32_t num);
387 const char *filename() const;
388 void filename(const char *name);
390 const char *toChars(
391 bool showColumns = Loc::showColumns,
392 MessageStyle messageStyle = Loc::messageStyle) const;
393 bool equals(const Loc& loc) const;
396 enum class LINK : uint8_t
398 default_,
401 cpp,
402 windows,
403 objc,
404 system
407 enum class CPPMANGLE : uint8_t
409 def,
410 asStruct,
411 asClass
414 enum class MATCH : int
416 nomatch, // no match
417 convert, // match with conversions
418 constant, // match with conversion to const
419 exact // exact match
422 enum class PINLINE : uint8_t
424 default_, // as specified on the command line
425 never, // never inline
426 always // always inline
429 enum class FileType : uint8_t
431 d, /// normal D source file
432 dhdr, /// D header file (.di)
433 ddoc, /// Ddoc documentation file (.dd)
434 c, /// C source file
437 typedef uinteger_t StorageClass;