d: Merge dmd. druntime e770945277, phobos 6d6e0b9b9
[official-gcc.git] / gcc / d / dmd / globals.h
blobf553ae6d8a9e7466fd64102825c3907cbe301c43
2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2024 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 /// Command line state related to printing uasage about other switches
100 struct Help
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
112 d_bool hc; // -HC
115 struct Verbose
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
134 unsigned errorLimit;
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
140 struct Param
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
152 Diagnostic warnings;
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
167 Help help;
168 Verbose v;
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
229 // Linker stuff
230 Array<const char *> objfiles;
231 Array<const char *> linkswitches;
232 Array<bool> linkswitchIsForCC;
233 Array<const char *> libfiles;
234 Array<const char *> dllfiles;
235 DString deffile;
236 DString resfile;
237 DString exefile;
238 DString mapfile;
241 struct structalign_t
243 unsigned short value;
244 d_bool pack;
246 bool isDefault() const;
247 void setDefault();
248 bool isUnknown() const;
249 void setUnknown();
250 void set(unsigned value);
251 unsigned get() const;
252 bool isPack() 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
268 struct CompileEnv
270 uint32_t versionNumber;
271 DString date;
272 DString time;
273 DString vendor;
274 DString timestamp;
275 d_bool previewIn;
276 d_bool ddocOutput;
279 struct Global
281 DString inifilename;
283 const DString copyright;
284 const DString written;
285 Array<const char *> path; // Array of char*'s which form the import lookup path
286 Array<const char *> filePath; // Array of char*'s which form the file import lookup path
288 char datetime[26]; /// string returned by ctime()
289 CompileEnv compileEnv;
291 Param params;
292 unsigned errors; // number of errors reported so far
293 unsigned warnings; // number of warnings reported so far
294 unsigned gag; // !=0 means gag reporting of errors & warnings
295 unsigned gaggedErrors; // number of errors reported while gagged
296 unsigned gaggedWarnings; // number of warnings reported while gagged
298 void* console; // opaque pointer to console for controlling text attributes
300 Array<class Identifier*> versionids; // command line versions and predefined versions
301 Array<class Identifier*> debugids; // command line debug versions and predefined versions
303 d_bool hasMainFunction;
304 unsigned varSequenceNumber;
306 FileManager* fileManager;
307 ErrorSink* errorSink; // where the error messages go
308 ErrorSink* errorSinkNull; // where the error messages disappear
310 DArray<unsigned char> (*preprocess)(FileName, const Loc&, OutBuffer&);
312 /* Start gagging. Return the current number of gagged errors
314 unsigned startGagging();
316 /* End gagging, restoring the old gagged state.
317 * Return true if errors occurred while gagged.
319 bool endGagging(unsigned oldGagged);
321 /* Increment the error count to record that an error
322 * has occurred in the current context. An error message
323 * may or may not have been printed.
325 void increaseErrorCount();
327 void _init();
330 Returns: the version as the number that would be returned for __VERSION__
332 unsigned versionNumber();
335 Returns: the compiler version string.
337 const char * versionChars();
340 extern Global global;
342 // Because int64_t and friends may be any integral type of the correct size,
343 // we have to explicitly ask for the correct integer type to get the correct
344 // mangling with dmd. The #if logic here should match the mangling of
345 // Tint64 and Tuns64 in cppmangle.d.
346 #if MARS && DMD_VERSION >= 2079 && DMD_VERSION <= 2081 && \
347 __APPLE__ && __SIZEOF_LONG__ == 8
348 // DMD versions between 2.079 and 2.081 mapped D long to int64_t on OS X.
349 typedef uint64_t dinteger_t;
350 typedef int64_t sinteger_t;
351 typedef uint64_t uinteger_t;
352 #elif __SIZEOF_LONG__ == 8
353 // Be careful not to care about sign when using dinteger_t
354 // use this instead of integer_t to
355 // avoid conflicts with system #include's
356 typedef unsigned long dinteger_t;
357 // Signed and unsigned variants
358 typedef long sinteger_t;
359 typedef unsigned long uinteger_t;
360 #else
361 typedef unsigned long long dinteger_t;
362 typedef long long sinteger_t;
363 typedef unsigned long long uinteger_t;
364 #endif
366 // file location
367 struct Loc
369 private:
370 unsigned _linnum;
371 unsigned _charnum;
372 unsigned fileIndex;
373 public:
374 static void set(bool showColumns, MessageStyle messageStyle);
376 static bool showColumns;
377 static MessageStyle messageStyle;
379 Loc()
381 _linnum = 0;
382 _charnum = 0;
383 fileIndex = 0;
386 Loc(const char *filename, unsigned linnum, unsigned charnum)
388 this->linnum(linnum);
389 this->charnum(charnum);
390 this->filename(filename);
393 uint32_t charnum() const;
394 uint32_t charnum(uint32_t num);
395 uint32_t linnum() const;
396 uint32_t linnum(uint32_t num);
397 const char *filename() const;
398 void filename(const char *name);
400 const char *toChars(
401 bool showColumns = Loc::showColumns,
402 MessageStyle messageStyle = Loc::messageStyle) const;
403 bool equals(const Loc& loc) const;
406 enum class LINK : uint8_t
408 default_,
411 cpp,
412 windows,
413 objc,
414 system
417 enum class CPPMANGLE : uint8_t
419 def,
420 asStruct,
421 asClass
424 enum class MATCH : int
426 nomatch, // no match
427 convert, // match with conversions
428 constant, // match with conversion to const
429 exact // exact match
432 enum class PINLINE : uint8_t
434 default_, // as specified on the command line
435 never, // never inline
436 always // always inline
439 enum class FileType : uint8_t
441 d, /// normal D source file
442 dhdr, /// D header file (.di)
443 ddoc, /// Ddoc documentation file (.dd)
444 c, /// C source file
447 typedef uinteger_t StorageClass;