| 1 |
|
| 2 |
/* Compiler implementation of the D programming language |
| 3 |
* Copyright (C) 1999-2025 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 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include "root/dcompat.h" |
| 14 |
#include "root/ctfloat.h" |
| 15 |
#include "common/outbuffer.h" |
| 16 |
#include "common/charactertables.h" |
| 17 |
#include "root/filename.h" |
| 18 |
#include "compiler.h" |
| 19 |
|
| 20 |
// Can't include arraytypes.h here, need to declare these directly. |
| 21 |
template <typename TYPE> struct Array; |
| 22 |
|
| 23 |
class ErrorSink; |
| 24 |
class FileManager; |
| 25 |
struct Loc; |
| 26 |
|
| 27 |
typedef unsigned char Diagnostic; |
| 28 |
enum |
| 29 |
{ |
| 30 |
DIAGNOSTICerror, // generate an error |
| 31 |
DIAGNOSTICinform, // generate a warning |
| 32 |
DIAGNOSTICoff // disable diagnostic |
| 33 |
}; |
| 34 |
|
| 35 |
enum class MessageStyle : unsigned char |
| 36 |
{ |
| 37 |
digitalmars, // file(line,column): message |
| 38 |
gnu, // file:line:column: message |
| 39 |
sarif // JSON SARIF output, see https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html |
| 40 |
}; |
| 41 |
|
| 42 |
// The state of array bounds checking |
| 43 |
typedef unsigned char CHECKENABLE; |
| 44 |
enum |
| 45 |
{ |
| 46 |
CHECKENABLEdefault, // initial value |
| 47 |
CHECKENABLEoff, // never do bounds checking |
| 48 |
CHECKENABLEon, // always do bounds checking |
| 49 |
CHECKENABLEsafeonly // do bounds checking only in @safe functions |
| 50 |
}; |
| 51 |
|
| 52 |
typedef unsigned char CHECKACTION; |
| 53 |
enum |
| 54 |
{ |
| 55 |
CHECKACTION_D, // call D assert on failure |
| 56 |
CHECKACTION_C, // call C assert on failure |
| 57 |
CHECKACTION_halt, // cause program halt on failure |
| 58 |
CHECKACTION_context // call D assert with the error context on failure |
| 59 |
}; |
| 60 |
|
| 61 |
enum JsonFieldFlags |
| 62 |
{ |
| 63 |
none = 0, |
| 64 |
compilerInfo = (1 << 0), |
| 65 |
buildInfo = (1 << 1), |
| 66 |
modules = (1 << 2), |
| 67 |
semantics = (1 << 3) |
| 68 |
}; |
| 69 |
|
| 70 |
enum CppStdRevision |
| 71 |
{ |
| 72 |
CppStdRevisionCpp98 = 199711, |
| 73 |
CppStdRevisionCpp11 = 201103, |
| 74 |
CppStdRevisionCpp14 = 201402, |
| 75 |
CppStdRevisionCpp17 = 201703, |
| 76 |
CppStdRevisionCpp20 = 202002, |
| 77 |
CppStdRevisionCpp23 = 202302, |
| 78 |
}; |
| 79 |
|
| 80 |
/// Trivalent boolean to represent the state of a `revert`able change |
| 81 |
enum class FeatureState : unsigned char |
| 82 |
{ |
| 83 |
default_ = 0, /// Not specified by the user |
| 84 |
disabled = 1, /// Specified as `-revert=` |
| 85 |
enabled = 2, /// Specified as `-preview=` |
| 86 |
}; |
| 87 |
|
| 88 |
/// Different identifier tables specifiable by CLI |
| 89 |
enum class CLIIdentifierTable : unsigned char |
| 90 |
{ |
| 91 |
default_ = 0, /// Not specified by user |
| 92 |
C99 = 1, /// Tables from C99 standard |
| 93 |
C11 = 2, /// Tables from C11 standard |
| 94 |
UAX31 = 3, /// Tables from the Unicode Standard Annex 31: UNICODE IDENTIFIERS AND SYNTAX |
| 95 |
All = 4, /// The least restrictive set of all other tables |
| 96 |
}; |
| 97 |
|
| 98 |
/// Specifies the mode for error printing |
| 99 |
enum class ErrorPrintMode : unsigned char |
| 100 |
{ |
| 101 |
simpleError, // Print errors without squiggles and carets |
| 102 |
printErrorContext, // Print errors with the error line and caret |
| 103 |
}; |
| 104 |
|
| 105 |
struct Output |
| 106 |
{ |
| 107 |
/// Configuration for the compiler generator |
| 108 |
d_bool doOutput; // Output is enabled |
| 109 |
d_bool fullOutput; // Generate comments for hidden declarations (for -HC), |
| 110 |
// and don't strip the bodies of plain (non-template) functions (for -H) |
| 111 |
DString dir; // write to directory 'dir' |
| 112 |
DString name; // write to file 'name' |
| 113 |
Array<const char*> files; // Other files associated with this output, |
| 114 |
// e.g. macro include files for Ddoc, dependencies for makedeps |
| 115 |
OutBuffer* buffer; // if this output is buffered, this is the buffer |
| 116 |
int bufferLines; // number of lines written to the buffer |
| 117 |
}; |
| 118 |
|
| 119 |
/// Command line state related to printing uasage about other switches |
| 120 |
struct Help |
| 121 |
{ |
| 122 |
d_bool manual; // open browser on compiler manual |
| 123 |
d_bool usage; // print usage and exit |
| 124 |
// print help of switch: |
| 125 |
d_bool mcpu; // -mcpu |
| 126 |
d_bool transition; // -transition |
| 127 |
d_bool check; // -check |
| 128 |
d_bool checkAction; // -checkaction |
| 129 |
d_bool revert; // -revert |
| 130 |
d_bool preview; // -preview |
| 131 |
d_bool externStd; // -extern-std |
| 132 |
d_bool hc; // -HC |
| 133 |
}; |
| 134 |
|
| 135 |
struct Verbose |
| 136 |
{ |
| 137 |
d_bool verbose; // verbose compile |
| 138 |
d_bool showColumns; // print character (column) numbers in diagnostics |
| 139 |
d_bool tls; // identify thread local variables |
| 140 |
d_bool templates; // collect and list statistics on template instantiations |
| 141 |
// collect and list statistics on template instantiations origins. |
| 142 |
// TODO: make this an enum when we want to list other kinds of instances |
| 143 |
d_bool templatesListInstances; |
| 144 |
d_bool gc; // identify gc usage |
| 145 |
d_bool field; // identify non-mutable field variables |
| 146 |
d_bool complex = true; // identify complex/imaginary type usage |
| 147 |
d_bool vin; // identify 'in' parameters |
| 148 |
d_bool showGaggedErrors; // print gagged errors anyway |
| 149 |
d_bool logo; // print compiler logo |
| 150 |
d_bool color; // use ANSI colors in console output |
| 151 |
d_bool cov; // generate code coverage data |
| 152 |
ErrorPrintMode errorPrintMode; // enum for error printing mode |
| 153 |
MessageStyle messageStyle; // style of file/line annotations on messages |
| 154 |
unsigned errorLimit; |
| 155 |
unsigned errorSupplementLimit; // Limit the number of supplemental messages for each error (0 means unlimited) |
| 156 |
unsigned errorSupplementCount(); |
| 157 |
}; |
| 158 |
|
| 159 |
struct ImportPathInfo |
| 160 |
{ |
| 161 |
const char* path; |
| 162 |
|
| 163 |
ImportPathInfo() : path(NULL) { } |
| 164 |
ImportPathInfo(const char* p) : path(p) { } |
| 165 |
}; |
| 166 |
|
| 167 |
// Put command line switches in here |
| 168 |
struct Param |
| 169 |
{ |
| 170 |
d_bool obj; // write object file |
| 171 |
d_bool readStdin; // read source file from stdin |
| 172 |
d_bool multiobj; // break one object file into multiple ones |
| 173 |
d_bool trace; // insert profiling hooks |
| 174 |
d_bool tracegc; // instrument calls to 'new' |
| 175 |
d_bool vcg_ast; // write-out codegen-ast |
| 176 |
Diagnostic useDeprecated; |
| 177 |
d_bool useUnitTests; // generate unittest code |
| 178 |
d_bool useInline; // inline expand functions |
| 179 |
d_bool release; // build release version |
| 180 |
d_bool preservePaths; // true means don't strip path from source file |
| 181 |
Diagnostic useWarnings; |
| 182 |
d_bool cov; // generate code coverage data |
| 183 |
unsigned char covPercent; // 0..100 code coverage percentage required |
| 184 |
d_bool ctfe_cov; // generate coverage data for ctfe |
| 185 |
d_bool ignoreUnsupportedPragmas; // rather than error on them |
| 186 |
d_bool useModuleInfo; // generate runtime module information |
| 187 |
d_bool useTypeInfo; // generate runtime type information |
| 188 |
d_bool useExceptions; // support exception handling |
| 189 |
d_bool useGC; // support features that require the D runtime GC |
| 190 |
d_bool betterC; // be a "better C" compiler; no dependency on D runtime |
| 191 |
d_bool addMain; // add a default main() function |
| 192 |
d_bool allInst; // generate code for all template instantiations |
| 193 |
d_bool bitfields; // support C style bit fields |
| 194 |
CppStdRevision cplusplus; // version of C++ name mangling to support |
| 195 |
|
| 196 |
Help help; |
| 197 |
Verbose v; |
| 198 |
|
| 199 |
// Options for `-preview=/-revert=` |
| 200 |
FeatureState useDIP25; // implement https://wiki.dlang.org/DIP25 |
| 201 |
FeatureState useDIP1000; // implement https://dlang.org/spec/memory-safe-d.html#scope-return-params |
| 202 |
d_bool ehnogc; // use @nogc exception handling |
| 203 |
d_bool useDIP1021; // implement https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1021.md |
| 204 |
FeatureState fieldwise; // do struct equality testing field-wise rather than by memcmp() |
| 205 |
d_bool fixAliasThis; // if the current scope has an alias this, check it before searching upper scopes |
| 206 |
FeatureState rvalueRefParam; // allow rvalues to be arguments to ref parameters |
| 207 |
// https://dconf.org/2019/talks/alexandrescu.html |
| 208 |
// https://gist.github.com/andralex/e5405a5d773f07f73196c05f8339435a |
| 209 |
// https://digitalmars.com/d/archives/digitalmars/D/Binding_rvalues_to_ref_parameters_redux_325087.html |
| 210 |
// Implementation: https://github.com/dlang/dmd/pull/9817 |
| 211 |
FeatureState safer; // safer by default (more @safe checks in unattributed code) |
| 212 |
// https://github.com/WalterBright/documents/blob/38f0a846726b571f8108f6e63e5e217b91421c86/safer.md |
| 213 |
|
| 214 |
FeatureState noSharedAccess; // read/write access to shared memory objects |
| 215 |
d_bool previewIn; // `in` means `[ref] scope const`, accepts rvalues |
| 216 |
d_bool inclusiveInContracts; // 'in' contracts of overridden methods must be a superset of parent contract |
| 217 |
d_bool shortenedMethods; // allow => in normal function declarations |
| 218 |
d_bool fixImmutableConv; // error on unsound immutable conversion - https://github.com/dlang/dmd/pull/14070 |
| 219 |
d_bool fix16997; // fix integral promotions for unary + - ~ operators |
| 220 |
// https://issues.dlang.org/show_bug.cgi?id=16997 |
| 221 |
FeatureState dtorFields; // destruct fields of partially constructed objects |
| 222 |
// https://issues.dlang.org/show_bug.cgi?id=14246 |
| 223 |
FeatureState systemVariables; // limit access to variables marked @system from @safe code |
| 224 |
|
| 225 |
CHECKENABLE useInvariants; // generate class invariant checks |
| 226 |
CHECKENABLE useIn; // generate precondition checks |
| 227 |
CHECKENABLE useOut; // generate postcondition checks |
| 228 |
CHECKENABLE useArrayBounds; // when to generate code for array bounds checks |
| 229 |
CHECKENABLE useAssert; // when to generate code for assert()'s |
| 230 |
CHECKENABLE useSwitchError; // check for switches without a default |
| 231 |
CHECKENABLE boundscheck; // state of -boundscheck switch |
| 232 |
|
| 233 |
CHECKACTION checkAction; // action to take when bounds, asserts or switch defaults are violated |
| 234 |
|
| 235 |
CLIIdentifierTable dIdentifierTable; |
| 236 |
CLIIdentifierTable cIdentifierTable; |
| 237 |
|
| 238 |
DString argv0; // program name |
| 239 |
Array<const char *> modFileAliasStrings; // array of char*'s of -I module filename alias strings |
| 240 |
Array<ImportPathInfo> imppath; // array of import path information of where to look for import modules |
| 241 |
Array<const char *> fileImppath; // array of char*'s of where to look for file import modules |
| 242 |
DString objdir; // .obj/.lib file output directory |
| 243 |
DString objname; // .obj file output name |
| 244 |
DString libname; // .lib file output name |
| 245 |
|
| 246 |
Output ddoc; // Generate embedded documentation comments |
| 247 |
Output dihdr; // Generate `.di` 'header' files |
| 248 |
Output cxxhdr; // Generate 'Cxx header' file |
| 249 |
Output json; // Generate JSON file |
| 250 |
unsigned jsonFieldFlags; // JSON field flags to include |
| 251 |
Output makeDeps; // Generate make file dependencies |
| 252 |
Output mixinOut; // write expanded mixins for debugging |
| 253 |
Output moduleDeps; // Generate `.deps` module dependencies |
| 254 |
|
| 255 |
d_bool debugEnabled; // -debug flag is passed |
| 256 |
|
| 257 |
d_bool run; // run resulting executable |
| 258 |
Strings runargs; // arguments for executable |
| 259 |
|
| 260 |
Array<const char *> cppswitches; // preprocessor switches |
| 261 |
const char *cpp; // if not null, then this specifies the C preprocessor |
| 262 |
|
| 263 |
// Linker stuff |
| 264 |
Array<const char *> objfiles; |
| 265 |
Array<const char *> linkswitches; |
| 266 |
Array<bool> linkswitchIsForCC; |
| 267 |
Array<const char *> libfiles; |
| 268 |
Array<const char *> dllfiles; |
| 269 |
DString deffile; |
| 270 |
DString resfile; |
| 271 |
DString exefile; |
| 272 |
DString mapfile; |
| 273 |
bool fullyQualifiedObjectFiles; |
| 274 |
bool timeTrace; |
| 275 |
uint32_t timeTraceGranularityUs; |
| 276 |
const char* timeTraceFile; |
| 277 |
}; |
| 278 |
|
| 279 |
struct structalign_t |
| 280 |
{ |
| 281 |
unsigned short value; |
| 282 |
d_bool pack; |
| 283 |
|
| 284 |
bool isDefault() const; |
| 285 |
void setDefault(); |
| 286 |
bool isUnknown() const; |
| 287 |
void setUnknown(); |
| 288 |
void set(unsigned value); |
| 289 |
unsigned get() const; |
| 290 |
bool isPack() const; |
| 291 |
void setPack(bool pack); |
| 292 |
}; |
| 293 |
|
| 294 |
// magic value means "match whatever the underlying C compiler does" |
| 295 |
// other values are all powers of 2 |
| 296 |
//#define STRUCTALIGN_DEFAULT ((structalign_t) ~0) |
| 297 |
|
| 298 |
const DString mars_ext = "d"; |
| 299 |
const DString doc_ext = "html"; // for Ddoc generated files |
| 300 |
const DString ddoc_ext = "ddoc"; // for Ddoc macro include files |
| 301 |
const DString dd_ext = "dd"; // for Ddoc source files |
| 302 |
const DString hdr_ext = "di"; // for D 'header' import files |
| 303 |
const DString json_ext = "json"; // for JSON files |
| 304 |
const DString map_ext = "map"; // for .map files |
| 305 |
|
| 306 |
struct CompileEnv |
| 307 |
{ |
| 308 |
uint32_t versionNumber; |
| 309 |
DString date; |
| 310 |
DString time; |
| 311 |
DString vendor; |
| 312 |
DString timestamp; |
| 313 |
d_bool previewIn; |
| 314 |
d_bool transitionIn; |
| 315 |
d_bool ddocOutput; |
| 316 |
d_bool masm; |
| 317 |
IdentifierCharLookup cCharLookupTable; |
| 318 |
IdentifierCharLookup dCharLookupTable; |
| 319 |
}; |
| 320 |
|
| 321 |
struct Global |
| 322 |
{ |
| 323 |
DString inifilename; |
| 324 |
|
| 325 |
const DString copyright; |
| 326 |
const DString written; |
| 327 |
Array<ImportPathInfo> path; // Array of path informations which form the import lookup path |
| 328 |
Array<const char *> importPaths; // Array of char*'s which form the import lookup path without metadata |
| 329 |
Array<const char *> filePath; // Array of char*'s which form the file import lookup path |
| 330 |
|
| 331 |
char datetime[26]; /// string returned by ctime() |
| 332 |
CompileEnv compileEnv; |
| 333 |
|
| 334 |
Param params; |
| 335 |
unsigned errors; // number of errors reported so far |
| 336 |
unsigned deprecations; // number of deprecations reported so far |
| 337 |
unsigned warnings; // number of warnings reported so far |
| 338 |
unsigned gag; // !=0 means gag reporting of errors & warnings |
| 339 |
unsigned gaggedErrors; // number of errors reported while gagged |
| 340 |
unsigned gaggedWarnings; // number of warnings reported while gagged |
| 341 |
|
| 342 |
void* console; // opaque pointer to console for controlling text attributes |
| 343 |
|
| 344 |
Array<class Identifier*> versionids; // command line versions and predefined versions |
| 345 |
Array<class Identifier*> debugids; // command line debug versions and predefined versions |
| 346 |
|
| 347 |
d_bool hasMainFunction; |
| 348 |
unsigned varSequenceNumber; |
| 349 |
|
| 350 |
FileManager* fileManager; |
| 351 |
ErrorSink* errorSink; // where the error messages go |
| 352 |
ErrorSink* errorSinkNull; // where the error messages disappear |
| 353 |
|
| 354 |
DArray<unsigned char> (*preprocess)(FileName, Loc, OutBuffer&); |
| 355 |
|
| 356 |
/* Start gagging. Return the current number of gagged errors |
| 357 |
*/ |
| 358 |
unsigned startGagging(); |
| 359 |
|
| 360 |
/* End gagging, restoring the old gagged state. |
| 361 |
* Return true if errors occurred while gagged. |
| 362 |
*/ |
| 363 |
bool endGagging(unsigned oldGagged); |
| 364 |
|
| 365 |
/* Increment the error count to record that an error |
| 366 |
* has occurred in the current context. An error message |
| 367 |
* may or may not have been printed. |
| 368 |
*/ |
| 369 |
void increaseErrorCount(); |
| 370 |
|
| 371 |
void _init(); |
| 372 |
|
| 373 |
/** |
| 374 |
* Indicate to stateful error sinks that no more errors can be produced. |
| 375 |
* This is to support error sinks that collect information to produce a |
| 376 |
* single (say) report. |
| 377 |
*/ |
| 378 |
void plugErrorSinks(); |
| 379 |
|
| 380 |
/** |
| 381 |
Returns: the version as the number that would be returned for __VERSION__ |
| 382 |
*/ |
| 383 |
unsigned versionNumber(); |
| 384 |
|
| 385 |
/** |
| 386 |
Returns: the compiler version string. |
| 387 |
*/ |
| 388 |
const char * versionChars(); |
| 389 |
}; |
| 390 |
|
| 391 |
extern Global global; |
| 392 |
|
| 393 |
// Because int64_t and friends may be any integral type of the correct size, |
| 394 |
// we have to explicitly ask for the correct integer type to get the correct |
| 395 |
// mangling with dmd. The #if logic here should match the mangling of |
| 396 |
// Tint64 and Tuns64 in cppmangle.d. |
| 397 |
#if MARS && DMD_VERSION >= 2079 && DMD_VERSION <= 2081 && \ |
| 398 |
__APPLE__ && __SIZEOF_LONG__ == 8 |
| 399 |
// DMD versions between 2.079 and 2.081 mapped D long to int64_t on OS X. |
| 400 |
typedef uint64_t dinteger_t; |
| 401 |
typedef int64_t sinteger_t; |
| 402 |
typedef uint64_t uinteger_t; |
| 403 |
#elif __SIZEOF_LONG__ == 8 |
| 404 |
// Be careful not to care about sign when using dinteger_t |
| 405 |
// use this instead of integer_t to |
| 406 |
// avoid conflicts with system #include's |
| 407 |
typedef unsigned long dinteger_t; |
| 408 |
// Signed and unsigned variants |
| 409 |
typedef long sinteger_t; |
| 410 |
typedef unsigned long uinteger_t; |
| 411 |
#else |
| 412 |
typedef unsigned long long dinteger_t; |
| 413 |
typedef long long sinteger_t; |
| 414 |
typedef unsigned long long uinteger_t; |
| 415 |
#endif |
| 416 |
|
| 417 |
// file location |
| 418 |
struct SourceLoc |
| 419 |
{ |
| 420 |
DString filename; |
| 421 |
uint32_t line; |
| 422 |
uint32_t column; |
| 423 |
uint32_t fileOffset; |
| 424 |
DString fileContent; |
| 425 |
}; |
| 426 |
|
| 427 |
struct Loc |
| 428 |
{ |
| 429 |
private: |
| 430 |
|
| 431 |
unsigned int index; |
| 432 |
|
| 433 |
#if MARS && defined(__linux__) && defined(__i386__) |
| 434 |
unsigned int dummy; |
| 435 |
#endif |
| 436 |
|
| 437 |
public: |
| 438 |
static void set(bool showColumns, MessageStyle messageStyle); |
| 439 |
static Loc singleFilename(const char* const filename); |
| 440 |
|
| 441 |
static bool showColumns; |
| 442 |
static MessageStyle messageStyle; |
| 443 |
|
| 444 |
Loc() |
| 445 |
{ |
| 446 |
index = 0; |
| 447 |
} |
| 448 |
|
| 449 |
uint32_t charnum() const; |
| 450 |
uint32_t linnum() const; |
| 451 |
const char *filename() const; |
| 452 |
SourceLoc toSourceLoc() const; |
| 453 |
|
| 454 |
const char *toChars( |
| 455 |
bool showColumns = Loc::showColumns, |
| 456 |
MessageStyle messageStyle = Loc::messageStyle) const; |
| 457 |
bool equals(Loc loc) const; |
| 458 |
}; |
| 459 |
|
| 460 |
enum class LINK : uint8_t |
| 461 |
{ |
| 462 |
default_, |
| 463 |
d, |
| 464 |
c, |
| 465 |
cpp, |
| 466 |
windows, |
| 467 |
objc, |
| 468 |
system |
| 469 |
}; |
| 470 |
|
| 471 |
enum class CPPMANGLE : uint8_t |
| 472 |
{ |
| 473 |
def, |
| 474 |
asStruct, |
| 475 |
asClass |
| 476 |
}; |
| 477 |
|
| 478 |
enum class MATCH : int |
| 479 |
{ |
| 480 |
nomatch, // no match |
| 481 |
convert, // match with conversions |
| 482 |
constant, // match with conversion to const |
| 483 |
exact // exact match |
| 484 |
}; |
| 485 |
|
| 486 |
enum class PINLINE : uint8_t |
| 487 |
{ |
| 488 |
default_, // as specified on the command line |
| 489 |
never, // never inline |
| 490 |
always // always inline |
| 491 |
}; |
| 492 |
|
| 493 |
enum class FileType : uint8_t |
| 494 |
{ |
| 495 |
d, /// normal D source file |
| 496 |
dhdr, /// D header file (.di) |
| 497 |
ddoc, /// Ddoc documentation file (.dd) |
| 498 |
c, /// C source file |
| 499 |
}; |
| 500 |
|
| 501 |
typedef uinteger_t StorageClass; |