Home | History | Annotate | Line # | Download | only in dmd
      1 
      2 /* Compiler implementation of the D programming language
      3  * Copyright (C) 1999-2022 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 "root/filename.h"
     17 #include "compiler.h"
     18 
     19 // Can't include arraytypes.h here, need to declare these directly.
     20 template <typename TYPE> struct Array;
     21 
     22 class FileManager;
     23 
     24 typedef unsigned char Diagnostic;
     25 enum
     26 {
     27     DIAGNOSTICerror,  // generate an error
     28     DIAGNOSTICinform, // generate a warning
     29     DIAGNOSTICoff     // disable diagnostic
     30 };
     31 
     32 typedef unsigned char MessageStyle;
     33 enum
     34 {
     35     MESSAGESTYLEdigitalmars, // file(line,column): message
     36     MESSAGESTYLEgnu          // file:line:column: message
     37 };
     38 
     39 // The state of array bounds checking
     40 typedef unsigned char CHECKENABLE;
     41 enum
     42 {
     43     CHECKENABLEdefault, // initial value
     44     CHECKENABLEoff,     // never do bounds checking
     45     CHECKENABLEon,      // always do bounds checking
     46     CHECKENABLEsafeonly // do bounds checking only in @safe functions
     47 };
     48 
     49 typedef unsigned char CHECKACTION;
     50 enum
     51 {
     52     CHECKACTION_D,        // call D assert on failure
     53     CHECKACTION_C,        // call C assert on failure
     54     CHECKACTION_halt,     // cause program halt on failure
     55     CHECKACTION_context   // call D assert with the error context on failure
     56 };
     57 
     58 enum JsonFieldFlags
     59 {
     60     none         = 0,
     61     compilerInfo = (1 << 0),
     62     buildInfo    = (1 << 1),
     63     modules      = (1 << 2),
     64     semantics    = (1 << 3)
     65 };
     66 
     67 enum CppStdRevision
     68 {
     69     CppStdRevisionCpp98 = 199711,
     70     CppStdRevisionCpp11 = 201103,
     71     CppStdRevisionCpp14 = 201402,
     72     CppStdRevisionCpp17 = 201703,
     73     CppStdRevisionCpp20 = 202002
     74 };
     75 
     76 /// Configuration for the C++ header generator
     77 enum class CxxHeaderMode
     78 {
     79     none,   /// Don't generate headers
     80     silent, /// Generate headers
     81     verbose /// Generate headers and add comments for hidden declarations
     82 };
     83 
     84 /// Trivalent boolean to represent the state of a `revert`able change
     85 enum class FeatureState : signed char
     86 {
     87     default_ = -1, /// Not specified by the user
     88     disabled = 0,  /// Specified as `-revert=`
     89     enabled = 1    /// Specified as `-preview=`
     90 };
     91 
     92 // Put command line switches in here
     93 struct Param
     94 {
     95     bool obj;           // write object file
     96     bool link;          // perform link
     97     bool dll;           // generate shared dynamic library
     98     bool lib;           // write library file instead of object file(s)
     99     bool multiobj;      // break one object file into multiple ones
    100     bool oneobj;        // write one object file instead of multiple ones
    101     bool trace;         // insert profiling hooks
    102     bool tracegc;       // instrument calls to 'new'
    103     bool verbose;       // verbose compile
    104     bool vcg_ast;       // write-out codegen-ast
    105     bool showColumns;   // print character (column) numbers in diagnostics
    106     bool vtls;          // identify thread local variables
    107     bool vtemplates;    // collect and list statistics on template instantiations
    108     bool vtemplatesListInstances; // collect and list statistics on template instantiations origins
    109     bool vgc;           // identify gc usage
    110     bool vfield;        // identify non-mutable field variables
    111     bool vcomplex;      // identify complex/imaginary type usage
    112     bool vin;           // identify 'in' parameters
    113     unsigned char symdebug;  // insert debug symbolic information
    114     bool symdebugref;   // insert debug information for all referenced types, too
    115     bool optimize;      // run optimizer
    116     Diagnostic useDeprecated;
    117     bool stackstomp;    // add stack stomping code
    118     bool useUnitTests;  // generate unittest code
    119     bool useInline;     // inline expand functions
    120     FeatureState useDIP25;      // implement https://wiki.dlang.org/DIP25
    121     FeatureState useDIP1000; // implement https://dlang.org/spec/memory-safe-d.html#scope-return-params
    122     bool useDIP1021;    // implement https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1021.md
    123     bool release;       // build release version
    124     bool preservePaths; // true means don't strip path from source file
    125     Diagnostic warnings;
    126     unsigned char pic;  // generate position-independent-code for shared libs
    127     bool color;         // use ANSI colors in console output
    128     bool cov;           // generate code coverage data
    129     unsigned char covPercent;   // 0..100 code coverage percentage required
    130     bool ctfe_cov;      // generate coverage data for ctfe
    131     bool nofloat;       // code should not pull in floating point support
    132     bool ignoreUnsupportedPragmas;      // rather than error on them
    133     bool useModuleInfo; // generate runtime module information
    134     bool useTypeInfo;   // generate runtime type information
    135     bool useExceptions; // support exception handling
    136     bool noSharedAccess; // read/write access to shared memory objects
    137     bool previewIn;     // `in` means `scope const`, perhaps `ref`, accepts rvalues
    138     bool shortenedMethods; // allow => in normal function declarations
    139     bool betterC;       // be a "better C" compiler; no dependency on D runtime
    140     bool addMain;       // add a default main() function
    141     bool allInst;       // generate code for all template instantiations
    142     bool fix16997;      // fix integral promotions for unary + - ~ operators
    143                         // https://issues.dlang.org/show_bug.cgi?id=16997
    144     bool fixAliasThis;  // if the current scope has an alias this, check it before searching upper scopes
    145     bool inclusiveInContracts;   // 'in' contracts of overridden methods must be a superset of parent contract
    146     bool ehnogc;        // use @nogc exception handling
    147     FeatureState dtorFields;  // destruct fields of partially constructed objects
    148                               // https://issues.dlang.org/show_bug.cgi?id=14246
    149     bool fieldwise;         // do struct equality testing field-wise rather than by memcmp()
    150     FeatureState rvalueRefParam;    // allow rvalues to be arguments to ref parameters
    151     CppStdRevision cplusplus;  // version of C++ name mangling to support
    152     bool markdown;          // enable Markdown replacements in Ddoc
    153     bool vmarkdown;         // list instances of Markdown replacements in Ddoc
    154     bool showGaggedErrors;  // print gagged errors anyway
    155     bool printErrorContext;  // print errors with the error context (the error line in the source file)
    156     bool manual;            // open browser on compiler manual
    157     bool usage;             // print usage and exit
    158     bool mcpuUsage;         // print help on -mcpu switch
    159     bool transitionUsage;   // print help on -transition switch
    160     bool checkUsage;        // print help on -check switch
    161     bool checkActionUsage;  // print help on -checkaction switch
    162     bool revertUsage;       // print help on -revert switch
    163     bool previewUsage;      // print help on -preview switch
    164     bool externStdUsage;    // print help on -extern-std switch
    165     bool hcUsage;           // print help on -HC switch
    166     bool logo;              // print logo;
    167 
    168     CHECKENABLE useInvariants;     // generate class invariant checks
    169     CHECKENABLE useIn;             // generate precondition checks
    170     CHECKENABLE useOut;            // generate postcondition checks
    171     CHECKENABLE useArrayBounds;    // when to generate code for array bounds checks
    172     CHECKENABLE useAssert;         // when to generate code for assert()'s
    173     CHECKENABLE useSwitchError;    // check for switches without a default
    174     CHECKENABLE boundscheck;       // state of -boundscheck switch
    175 
    176     CHECKACTION checkAction;       // action to take when bounds, asserts or switch defaults are violated
    177 
    178     unsigned errorLimit;
    179 
    180     DString  argv0;    // program name
    181     Array<const char *> modFileAliasStrings; // array of char*'s of -I module filename alias strings
    182     Array<const char *> *imppath;     // array of char*'s of where to look for import modules
    183     Array<const char *> *fileImppath; // array of char*'s of where to look for file import modules
    184     DString objdir;    // .obj/.lib file output directory
    185     DString objname;   // .obj file output name
    186     DString libname;   // .lib file output name
    187 
    188     bool doDocComments;  // process embedded documentation comments
    189     DString docdir;      // write documentation file to docdir directory
    190     DString docname;     // write documentation file to docname
    191     Array<const char *> ddocfiles;  // macro include files for Ddoc
    192 
    193     bool doHdrGeneration;  // process embedded documentation comments
    194     DString hdrdir;        // write 'header' file to docdir directory
    195     DString hdrname;       // write 'header' file to docname
    196     bool hdrStripPlainFunctions; // strip the bodies of plain (non-template) functions
    197 
    198     CxxHeaderMode doCxxHdrGeneration;  // write 'Cxx header' file
    199     DString cxxhdrdir;        // write 'header' file to docdir directory
    200     DString cxxhdrname;       // write 'header' file to docname
    201 
    202     bool doJsonGeneration;    // write JSON file
    203     DString jsonfilename;     // write JSON file to jsonfilename
    204     unsigned jsonFieldFlags;  // JSON field flags to include
    205 
    206     OutBuffer *mixinOut;                // write expanded mixins for debugging
    207     const char *mixinFile;             // .mixin file output name
    208     int mixinLines;                     // Number of lines in writeMixins
    209 
    210     unsigned debuglevel;   // debug level
    211     Array<const char *> *debugids;     // debug identifiers
    212 
    213     unsigned versionlevel; // version level
    214     Array<const char *> *versionids;   // version identifiers
    215 
    216     DString defaultlibname;     // default library for non-debug builds
    217     DString debuglibname;       // default library for debug builds
    218     DString mscrtlib;           // MS C runtime library
    219 
    220     DString moduleDepsFile;     // filename for deps output
    221     OutBuffer *moduleDeps;      // contents to be written to deps file
    222 
    223     bool emitMakeDeps;                // whether to emit makedeps
    224     DString makeDepsFile;             // filename for makedeps output
    225     Array<const char *> makeDeps;     // dependencies for makedeps
    226 
    227     MessageStyle messageStyle;  // style of file/line annotations on messages
    228 
    229     bool run;           // run resulting executable
    230     Strings runargs;    // arguments for executable
    231 
    232     // Linker stuff
    233     Array<const char *> objfiles;
    234     Array<const char *> linkswitches;
    235     Array<bool> linkswitchIsForCC;
    236     Array<const char *> libfiles;
    237     Array<const char *> dllfiles;
    238     DString deffile;
    239     DString resfile;
    240     DString exefile;
    241     DString mapfile;
    242 };
    243 
    244 struct structalign_t
    245 {
    246     unsigned short value;
    247     bool pack;
    248 
    249     bool isDefault() const;
    250     void setDefault();
    251     bool isUnknown() const;
    252     void setUnknown();
    253     void set(unsigned value);
    254     unsigned get() const;
    255     bool isPack() const;
    256     void setPack(bool pack);
    257 };
    258 
    259 // magic value means "match whatever the underlying C compiler does"
    260 // other values are all powers of 2
    261 //#define STRUCTALIGN_DEFAULT ((structalign_t) ~0)
    262 
    263 const DString mars_ext = "d";
    264 const DString doc_ext  = "html";     // for Ddoc generated files
    265 const DString ddoc_ext = "ddoc";     // for Ddoc macro include files
    266 const DString dd_ext   = "dd";       // for Ddoc source files
    267 const DString hdr_ext  = "di";       // for D 'header' import files
    268 const DString json_ext = "json";     // for JSON files
    269 const DString map_ext  = "map";      // for .map files
    270 
    271 struct Global
    272 {
    273     DString inifilename;
    274 
    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
    279 
    280     DString vendor;          // Compiler backend name
    281 
    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
    288 
    289     void* console;         // opaque pointer to console for controlling text attributes
    290 
    291     Array<class Identifier*>* versionids; // command line versions and predefined versions
    292     Array<class Identifier*>* debugids;   // command line debug versions and predefined versions
    293 
    294     bool hasMainFunction;
    295     unsigned varSequenceNumber;
    296 
    297     FileManager* fileManager;
    298 
    299     /* Start gagging. Return the current number of gagged errors
    300      */
    301     unsigned startGagging();
    302 
    303     /* End gagging, restoring the old gagged state.
    304      * Return true if errors occurred while gagged.
    305      */
    306     bool endGagging(unsigned oldGagged);
    307 
    308     /*  Increment the error count to record that an error
    309      *  has occurred in the current context. An error message
    310      *  may or may not have been printed.
    311      */
    312     void increaseErrorCount();
    313 
    314     void _init();
    315 
    316     /**
    317     Returns: the version as the number that would be returned for __VERSION__
    318     */
    319     unsigned versionNumber();
    320 
    321     /**
    322     Returns: the compiler version string.
    323     */
    324     const char * versionChars();
    325 };
    326 
    327 extern Global global;
    328 
    329 // Because int64_t and friends may be any integral type of the correct size,
    330 // we have to explicitly ask for the correct integer type to get the correct
    331 // mangling with dmd. The #if logic here should match the mangling of
    332 // Tint64 and Tuns64 in cppmangle.d.
    333 #if MARS && DMD_VERSION >= 2079 && DMD_VERSION <= 2081 && \
    334     __APPLE__ && __SIZEOF_LONG__ == 8
    335 // DMD versions between 2.079 and 2.081 mapped D long to int64_t on OS X.
    336 typedef uint64_t dinteger_t;
    337 typedef int64_t sinteger_t;
    338 typedef uint64_t uinteger_t;
    339 #elif __SIZEOF_LONG__ == 8
    340 // Be careful not to care about sign when using dinteger_t
    341 // use this instead of integer_t to
    342 // avoid conflicts with system #include's
    343 typedef unsigned long dinteger_t;
    344 // Signed and unsigned variants
    345 typedef long sinteger_t;
    346 typedef unsigned long uinteger_t;
    347 #else
    348 typedef unsigned long long dinteger_t;
    349 typedef long long sinteger_t;
    350 typedef unsigned long long uinteger_t;
    351 #endif
    352 
    353 // file location
    354 struct Loc
    355 {
    356     const char *filename; // either absolute or relative to cwd
    357     unsigned linnum;
    358     unsigned charnum;
    359 
    360     Loc()
    361     {
    362         linnum = 0;
    363         charnum = 0;
    364         filename = NULL;
    365     }
    366 
    367     Loc(const char *filename, unsigned linnum, unsigned charnum)
    368     {
    369         this->linnum = linnum;
    370         this->charnum = charnum;
    371         this->filename = filename;
    372     }
    373 
    374     const char *toChars(
    375         bool showColumns = global.params.showColumns,
    376         MessageStyle messageStyle = global.params.messageStyle) const;
    377     bool equals(const Loc& loc) const;
    378 };
    379 
    380 enum class LINK : uint8_t
    381 {
    382     default_,
    383     d,
    384     c,
    385     cpp,
    386     windows,
    387     objc,
    388     system
    389 };
    390 
    391 enum class CPPMANGLE : uint8_t
    392 {
    393     def,
    394     asStruct,
    395     asClass
    396 };
    397 
    398 enum class MATCH : int
    399 {
    400     nomatch,       // no match
    401     convert,       // match with conversions
    402     constant,      // match with conversion to const
    403     exact          // exact match
    404 };
    405 
    406 enum class PINLINE : uint8_t
    407 {
    408     default_,     // as specified on the command line
    409     never,        // never inline
    410     always        // always inline
    411 };
    412 
    413 enum class FileType : uint8_t
    414 {
    415     d,    /// normal D source file
    416     dhdr, /// D header file (.di)
    417     ddoc, /// Ddoc documentation file (.dd)
    418     c,    /// C source file
    419 };
    420 
    421 typedef uinteger_t StorageClass;
    422