1: 
  2: // Compiler implementation of the D programming language
  3: // Copyright (c) 1999-2010 by Digital Mars
  4: // All Rights Reserved
  5: // written by Walter Bright
  6: // http://www.digitalmars.com
  7: // License for redistribution is by either the Artistic License
  8: // in artistic.txt, or the GNU General Public License in gnu.txt.
  9: // See the included readme.txt for details.
 10: 
 11: #ifndef DMD_MARS_H
 12: #define DMD_MARS_H
 13: 
 14: #ifdef __DMC__
 15: #pragma once
 16: #endif
 17: 
 18: /*
 19: It is very important to use version control macros correctly - the
 20: idea is that host and target are independent. If these are done
 21: correctly, cross compilers can be built.
 22: The host compiler and host operating system are also different,
 23: and are predefined by the host compiler. The ones used in
 24: dmd are:
 25: 
 26: Macros defined by the compiler, not the code:
 27: 
 28:     Compiler:
 29:         __DMC__         Digital Mars compiler
 30:         _MSC_VER        Microsoft compiler
 31:         __GNUC__        Gnu compiler
 32: 
 33:     Host operating system:
 34:         _WIN32          Microsoft NT, Windows 95, Windows 98, Win32s,
 35:                         Windows 2000, Win XP, Vista
 36:         _WIN64          Windows for AMD64
 37:         linux           Linux
 38:         __APPLE__       Mac OSX
 39:         __FreeBSD__     FreeBSD
 40:         __OpenBSD__     OpenBSD
 41:         __sun&&__SVR4   Solaris, OpenSolaris (yes, both macros are necessary)
 42: 
 43: For the target systems, there are the target operating system and
 44: the target object file format:
 45: 
 46:     Target operating system:
 47:         TARGET_WINDOS   Covers 32 bit windows and 64 bit windows
 48:         TARGET_LINUX    Covers 32 and 64 bit linux
 49:         TARGET_OSX      Covers 32 and 64 bit Mac OSX
 50:         TARGET_FREEBSD  Covers 32 and 64 bit FreeBSD
 51:         TARGET_OPENBSD  Covers 32 and 64 bit OpenBSD
 52:         TARGET_SOLARIS  Covers 32 and 64 bit Solaris
 53:         TARGET_NET      Covers .Net
 54: 
 55:     It is expected that the compiler for each platform will be able
 56:     to generate 32 and 64 bit code from the same compiler binary.
 57: 
 58:     Target object module format:
 59:         OMFOBJ          Intel Object Module Format, used on Windows
 60:         ELFOBJ          Elf Object Module Format, used on linux, FreeBSD, OpenBSD and Solaris
 61:         MACHOBJ         Mach-O Object Module Format, used on Mac OSX
 62: 
 63:     There are currently no macros for byte endianness order.
 64:  */
 65: 
 66: 
 67: #include <stdio.h>
 68: #include <stdint.h>
 69: #include <stdarg.h>
 70: 
 71: #ifdef __DMC__
 72: #ifdef DEBUG
 73: #undef assert
 74: #define assert(e) (static_cast<void>((e) || (printf("assert %s(%d) %s\n", __FILE__, __LINE__, #e), halt())))
 75: #endif
 76: #endif
 77: 
 78: #ifdef DEBUG
 79: #define UNITTEST 1
 80: #endif
 81: void unittests();
 82: 
 83: #ifdef IN_GCC
 84: /* Changes for the GDC compiler by David Friedman */
 85: #endif
 86: 
 87: #define DMDV1   0
 88: #define DMDV2   1       // Version 2.0 features
 89: #define BREAKABI 1      // 0 if not ready to break the ABI just yet
 90: #define STRUCTTHISREF DMDV2     // if 'this' for struct is a reference, not a pointer
 91: #define SNAN_DEFAULT_INIT DMDV2 // if floats are default initialized to signalling NaN
 92: #define SARRAYVALUE DMDV2       // static arrays are value types
 93: #define MODULEINFO_IS_STRUCT DMDV2   // if ModuleInfo is a struct rather than a class
 94: 
 95: // Set if C++ mangling is done by the front end
 96: #define CPP_MANGLE (DMDV2 && (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS))
 97: 
 98: /* Other targets are TARGET_LINUX, TARGET_OSX, TARGET_FREEBSD, TARGET_OPENBSD and
 99:  * TARGET_SOLARIS, which are
100:  * set on the command line via the compiler makefile.
101:  */
102: 
103: #if _WIN32
104: #define TARGET_WINDOS 1         // Windows dmd generates Windows targets
warning C4005: 'TARGET_WINDOS' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(186) : see previous definition of 'TARGET_WINDOS'
warning C4005: 'TARGET_WINDOS' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(186) : see previous definition of 'TARGET_WINDOS'
warning C4005: 'TARGET_WINDOS' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(186) : see previous definition of 'TARGET_WINDOS'
warning C4005: 'TARGET_WINDOS' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(186) : see previous definition of 'TARGET_WINDOS'
warning C4005: 'TARGET_WINDOS' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(186) : see previous definition of 'TARGET_WINDOS'
warning C4005: 'TARGET_WINDOS' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(186) : see previous definition of 'TARGET_WINDOS'
warning C4005: 'TARGET_WINDOS' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(186) : see previous definition of 'TARGET_WINDOS'
105: #define OMFOBJ 1
warning C4005: 'OMFOBJ' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(510) : see previous definition of 'OMFOBJ'
warning C4005: 'OMFOBJ' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(510) : see previous definition of 'OMFOBJ'
warning C4005: 'OMFOBJ' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(510) : see previous definition of 'OMFOBJ'
warning C4005: 'OMFOBJ' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(510) : see previous definition of 'OMFOBJ'
warning C4005: 'OMFOBJ' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(510) : see previous definition of 'OMFOBJ'
warning C4005: 'OMFOBJ' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(510) : see previous definition of 'OMFOBJ'
warning C4005: 'OMFOBJ' : macro redefinition C:\Projects\Extern\D\dmd\src\backend\cdef.h(510) : see previous definition of 'OMFOBJ'
106: #endif 107: 108: #if TARGET_LINUX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS 109: #ifndef ELFOBJ 110: #define ELFOBJ 1 111: #endif 112: #endif 113: 114: #if TARGET_OSX 115: #ifndef MACHOBJ 116: #define MACHOBJ 1 117: #endif 118: #endif 119: 120: 121: struct OutBuffer; 122: 123: // Can't include arraytypes.h here, need to declare these directly. 124: template <typename TYPE> struct ArrayBase; 125: typedef ArrayBase<struct Identifier> Identifiers; 126: typedef ArrayBase<char> Strings; 127: 128: // Put command line switches in here 129: struct Param 130: { 131: char obj; // write object file 132: char link; // perform link 133: char dll; // generate shared dynamic library 134: char lib; // write library file instead of object file(s) 135: char multiobj; // break one object file into multiple ones 136: char oneobj; // write one object file instead of multiple ones 137: char trace; // insert profiling hooks 138: char quiet; // suppress non-error messages 139: char verbose; // verbose compile 140: char vtls; // identify thread local variables 141: char symdebug; // insert debug symbolic information 142: char optimize; // run optimizer 143: char map; // generate linker .map file 144: char cpu; // target CPU 145: char is64bit; // generate 64 bit code 146: char isLinux; // generate code for linux 147: char isOSX; // generate code for Mac OSX 148: char isWindows; // generate code for Windows 149: char isFreeBSD; // generate code for FreeBSD 150: char isOPenBSD; // generate code for OpenBSD 151: char isSolaris; // generate code for Solaris 152: char scheduler; // which scheduler to use 153: char useDeprecated; // allow use of deprecated features 154: char useAssert; // generate runtime code for assert()'s 155: char useInvariants; // generate class invariant checks 156: char useIn; // generate precondition checks 157: char useOut; // generate postcondition checks 158: char useArrayBounds; // 0: no array bounds checks 159: // 1: array bounds checks for safe functions only 160: // 2: array bounds checks for all functions 161: char noboundscheck; // no array bounds checking at all 162: char useSwitchError; // check for switches without a default 163: char useUnitTests; // generate unittest code 164: char useInline; // inline expand functions 165: char release; // build release version 166: char preservePaths; // !=0 means don't strip path from source file 167: char warnings; // 0: enable warnings 168: // 1: warnings as errors 169: // 2: informational warnings (no errors) 170: char pic; // generate position-independent-code for shared libs 171: char cov; // generate code coverage data 172: char nofloat; // code should not pull in floating point support 173: char Dversion; // D version number 174: char ignoreUnsupportedPragmas; // rather than error on them 175: char enforcePropertySyntax; 176: 177: char *argv0; // program name 178: Strings *imppath; // array of char*'s of where to look for import modules 179: Strings *fileImppath; // array of char*'s of where to look for file import modules 180: char *objdir; // .obj/.lib file output directory 181: char *objname; // .obj file output name 182: char *libname; // .lib file output name 183: 184: char doDocComments; // process embedded documentation comments 185: char *docdir; // write documentation file to docdir directory 186: char *docname; // write documentation file to docname 187: Strings *ddocfiles; // macro include files for Ddoc 188: 189: char doHdrGeneration; // process embedded documentation comments 190: char *hdrdir; // write 'header' file to docdir directory 191: char *hdrname; // write 'header' file to docname 192: 193: char doXGeneration; // write JSON file 194: char *xfilename; // write JSON file to xfilename 195: 196: unsigned debuglevel; // debug level 197: Strings *debugids; // debug identifiers 198: 199: unsigned versionlevel; // version level 200: Strings *versionids; // version identifiers 201: 202: bool dump_source; 203: 204: const char *defaultlibname; // default library for non-debug builds 205: const char *debuglibname; // default library for debug builds 206: 207: char *moduleDepsFile; // filename for deps output 208: OutBuffer *moduleDeps; // contents to be written to deps file 209: 210: // Hidden debug switches 211: char debuga; 212: char debugb; 213: char debugc; 214: char debugf; 215: char debugr; 216: char debugw; 217: char debugx; 218: char debugy; 219: 220: char run; // run resulting executable 221: size_t runargs_length; 222: char** runargs; // arguments for executable 223: 224: // Linker stuff 225: Strings *objfiles; 226: Strings *linkswitches; 227: Strings *libfiles; 228: char *deffile; 229: char *resfile; 230: char *exefile; 231: char *mapfile; 232: }; 233: 234: struct Global 235: { 236: const char *mars_ext; 237: const char *sym_ext; 238: const char *obj_ext; 239: const char *lib_ext; 240: const char *dll_ext; 241: const char *doc_ext; // for Ddoc generated files 242: const char *ddoc_ext; // for Ddoc macro include files 243: const char *hdr_ext; // for D 'header' import files 244: const char *json_ext; // for JSON files 245: const char *map_ext; // for .map files 246: const char *copyright; 247: const char *written; 248: Strings *path; // Array of char*'s which form the import lookup path 249: Strings *filePath; // Array of char*'s which form the file import lookup path 250: int structalign; 251: const char *version; 252: 253: Param params; 254: unsigned errors; // number of errors reported so far 255: unsigned warnings; // number of warnings reported so far 256: unsigned gag; // !=0 means gag reporting of errors & warnings 257: 258: Global(); 259: }; 260: 261: extern Global global; 262: 263: /* Set if Windows Structured Exception Handling C extensions are supported. 264: * Apparently, VC has dropped support for these? 265: */ 266: #define WINDOWS_SEH (_WIN32 && __DMC__) 267: 268: 269: #ifdef __DMC__ 270: typedef _Complex long double complex_t; 271: #else 272: #ifndef IN_GCC 273: #include "complex_t.h" 274: #endif 275: #ifdef __APPLE__ 276: //#include "complex.h"//This causes problems with include the c++ <complex> and not the C "complex.h" 277: #endif 278: #endif 279: 280: // Be careful not to care about sign when using dinteger_t 281: //typedef uint64_t integer_t; 282: typedef uint64_t dinteger_t; // use this instead of integer_t to 283: // avoid conflicts with system #include's 284: 285: // Signed and unsigned variants 286: typedef int64_t sinteger_t; 287: typedef uint64_t uinteger_t; 288: 289: typedef int8_t d_int8; 290: typedef uint8_t d_uns8; 291: typedef int16_t d_int16; 292: typedef uint16_t d_uns16; 293: typedef int32_t d_int32; 294: typedef uint32_t d_uns32; 295: typedef int64_t d_int64; 296: typedef uint64_t d_uns64; 297: 298: typedef float d_float32; 299: typedef double d_float64; 300: typedef long double d_float80; 301: 302: typedef d_uns8 d_char; 303: typedef d_uns16 d_wchar; 304: typedef d_uns32 d_dchar; 305: 306: #ifdef IN_GCC 307: #include "d-gcc-real.h" 308: #else 309: typedef long double real_t; 310: #endif 311: 312: // Modify OutBuffer::writewchar to write the correct size of wchar 313: #if _WIN32 314: #define writewchar writeword 315: #else 316: // This needs a configuration test... 317: #define writewchar write4 318: #endif 319: 320: #ifdef IN_GCC 321: #include "d-gcc-complex_t.h" 322: #endif 323: 324: struct Module; 325: 326: //typedef unsigned Loc; // file location 327: struct Loc 328: { 329: const char *filename; 330: unsigned linnum; 331: 332: Loc() 333: { 334: linnum = 0; 335: filename = NULL; 336: } 337: 338: Loc(int x) 339: { 340: linnum = x; 341: filename = NULL; 342: } 343: 344: Loc(Module *mod, unsigned linnum); 345: 346: char *toChars(); 347: bool equals(const Loc& loc); 348: }; 349: 350: #ifndef GCC_SAFE_DMD 351: #define TRUE 1 352: #define FALSE 0 353: #endif 354: 355: #define INTERFACE_OFFSET 0 // if 1, put classinfo as first entry 356: // in interface vtbl[]'s 357: #define INTERFACE_VIRTUAL 0 // 1 means if an interface appears 358: // in the inheritance graph multiple 359: // times, only one is used 360: 361: enum LINK 362: { 363: LINKdefault, 364: LINKd, 365: LINKc, 366: LINKcpp, 367: LINKwindows, 368: LINKpascal, 369: }; 370: 371: enum DYNCAST 372: { 373: DYNCAST_OBJECT, 374: DYNCAST_EXPRESSION, 375: DYNCAST_DSYMBOL, 376: DYNCAST_TYPE, 377: DYNCAST_IDENTIFIER, 378: DYNCAST_TUPLE, 379: }; 380: 381: enum MATCH 382: { 383: MATCHnomatch, // no match 384: MATCHconvert, // match with conversions 385: #if DMDV2 386: MATCHconst, // match with conversion to const 387: #endif 388: MATCHexact // exact match 389: }; 390: 391: typedef uint64_t StorageClass; 392: 393: 394: void warning(Loc loc, const char *format, ...); 395: void error(Loc loc, const char *format, ...); 396: void verror(Loc loc, const char *format, va_list); 397: void vwarning(Loc loc, const char *format, va_list); 398: void fatal(); 399: #ifdef _MSC_VER 400: __declspec(noreturn) 401: #endif 402: void err_nomem(); 403: int runLINK(); 404: void deleteExeFile(); 405: int runProgram(); 406: const char *inifile(const char *argv0, const char *inifile); 407: void halt(); 408: void util_progress(); 409: 410: /*** Where to send error messages ***/ 411: #if IN_GCC 412: #define stdmsg stderr 413: #else 414: #define stdmsg stderr 415: #endif 416: 417: struct Dsymbol; 418: struct Library; 419: struct File; 420: void obj_start(char *srcfile); 421: void obj_end(Library *library, File *objfile); 422: void obj_append(Dsymbol *s); 423: void obj_write_deferred(Library *library); 424: 425: const char *importHint(const char *s); 426: 427: #endif /* DMD_MARS_H */ 428: