DMD Source code

This is the source code to the DMD compiler for the D Programming Language defined in the documents at https://dlang.org/

These sources are free, they are redistributable and modifiable under the terms of the Boost Software License, Version 1.0. The terms of this license are in the file boostlicense.txt, or see https://www.boost.org/LICENSE_1_0.txt.

If a particular file has a different license in it, that overrides this license for that file.

-Walter Bright

Directory structure

FolderPurpose
dmd/The dmd driver and front-end
dmd/backend/Code generation for x86 or x86-64. Shared by the Digital Mars C compiler, but not LDC or GDC.
dmd/common/Code shared by the front-end and back-end
dmd/root/Meant as a portable utility library, but “it wasn't very good and the only project left using it is dmd”.

DMD has a mostly flat directory structure, so this section aims to divide all source files into logical groups for easier navigation. The groups are roughly ordered by how late they appear in the compilation process. Note that these groups have no strict meaning, the category assignments are a bit subjective.

Driver

FilePurpose
mars.dThe entry point. Contains main.
cli.dDefine the command line interface
dmdparams.dDMD-specific parameters
globals.dDefine a structure storing command line options
dinifile.dParse settings from .ini file (sc.ini / dmd.conf)
vsoptions.dDetect the Microsoft Visual Studio toolchain for linking
frontend.dAn interface for using DMD as a library
errors.dError reporting implementation
errorsink.dError reporting interface
target.dManage target-specific parameters for cross-compiling (for LDC/GDC)
compiler.dDescribe a back-end compiler and implements compiler-specific actions

Lexing / parsing

FilePurpose
lexer.dConvert source code into tokens for the D and ImportC parsers
location.dEncapsulate file/line/column info for error messages, etc.
entity.dDefine “\&Entity;” escape sequence for strings / character literals
tokens.dDefine lexical tokens.
parse.dD parser, converting tokens into an Abstract Syntax Tree (AST)
cparse.dImportC parser, converting tokens into an Abstract Syntax Tree (AST)

Semantic analysis

Symbols and declarations

FilePurpose
dsymbol.dBase class for a D symbol, e.g. a variable, function, module, enum etc.
identifier.dRepresents the name of a Dsymbol
id.dDefine strings for pre-defined identifiers (e.g. sizeof, string)
dscope.dDefine a ‘scope’ on which symbol lookup can be performed
dtemplate.dA template declaration or instance
dmodule.dDefine a package and module
mtype.dDefine expression types such as int, char[], void function()
arraytypes.dFor certain Declaration nodes of type T, provides aliases for Array!T
declaration.dMisc. declarations of alias, variables, type tuples, ClassInfo etc.
denum.dDefines enum declarations and enum members
attrib.dDeclarations of ‘attributes’ such as private, pragma(), immutable, @UDA, align, extern(C++) and more
func.dDefine a function declaration (includes function literals, invariant, unittest)
dversion.dDefines a version symbol, e.g. version = ident, debug = ident

AST nodes

FilePurpose
ast_node.dDefine an abstract AST node class
astbase.dNamespace of AST nodes that can be produced by the parser
astcodegen.dNamespace of AST nodes of a AST ready for code generation
astenums.dEnums common to DMD and AST
expression.dDefine expression AST nodes
statement.dDefine statement AST nodes
staticassert.dDefine a static assert AST node
aggregate.dDefine an aggregate (struct, union or class) AST node
dclass.dDefine a class AST node
dstruct.dDefine a struct or union AST node
init.dDefine variable initializers

AST visitors

FilePurpose
parsetimevisitor.dGeneral visitor for AST nodes
permissivevisitor.dSubclass of ParseTimeVisitor that does not assert(0) on unimplemented nodes
strictvisitor.dVisitor that forces derived classes to implement visit for every possible node
visitor.dA visitor implementing visit for all nodes present in the compiler
transitivevisitor.dProvide a mixin template with visit methods for the parse time AST
apply.dDepth-first expression visitor
sapply.dDepth-first statement visitor
statement_rewrite_walker.dStatement visitor that allows replacing the currently visited node

Semantic passes

FilePurpose
dsymbolsem.dDo semantic 1 pass (symbol identifiers/types)
semantic2.dDo semantic 2 pass (symbol initializers)
semantic3.dDo semantic 3 pass (function bodies)
inline.dDo inline pass (optimization pass that dmd does in the front-end)
inlinecost.dCompute the cost of inlining a function call.
expressionsem.dDo semantic analysis for expressions
statementsem.dDo semantic analysis for statements
initsem.dDo semantic analysis for initializers
templateparamsem.dDo semantic analysis for template parameters
typesem.dDo semantic analysis for types

Semantic helpers

FilePurpose
opover.dOperator overloading
clone.dGenerate automatic opEquals, opAssign and constructors for structs
blockexit.dFind out in what ways control flow can exit a block
ctorflow.dControl flow in constructors
constfold.dDo constant folding of arithmetic expressions
optimize.dDo constant folding more generally
dcast.dImplicit or explicit cast(), finding common types e.g. in x ? a : b, integral promotions
impcnvtab.dDefine an implicit conversion table for basic types
importc.dHelpers specific to ImportC
sideeffect.dExtract side-effects of expressions for certain lowerings.
mustuse.dHelpers related to the @mustuse attribute

Compile Time Function Execution (CTFE)

FilePurpose
dinterpret.dCTFE entry point
ctfeexpr.dCTFE for expressions involving pointers, slices, array concatenation etc.
builtin.dAllow CTFE of certain external functions (core.math, std.math and core.bitop)

Specific language features

Attribute checks

FilePurpose
nogc.d@nogc checks
safe.d@safe checks
canthrow.dnothrow checks
escape.dscope checks
access.dpublic / private checks
ob.dOwnership / borrowing (@live) checks

Inline Assembly

FilePurpose
iasm.dInline assembly depending on the compiler
iasmdmd.dInline assembly for DMD
iasmgcc.dInline assembly for GDC

Other

FilePurpose
aliasthis.dResolve implicit conversions for alias X this
traits.d__traits()
lambdacomp.d__traits(isSame, x => y, z => w)
cond.dEvaluate static if, version debug
staticcond.dLazily evaluate static conditions for static if, static assert and template constraints
delegatize.dConverts expression to delegates for lazy parameters
eh.dGenerate tables for exception handling
nspace.dNamespace for extern (C++, Module)
intrange.dValue range propagation
dimport.dRenamed imports (import aliasSymbol = pkg1.pkg2.symbol)
arrayop.dArray operations (a[] = b[] + c[])
cpreprocess.dRun the C preprocessor on C source files
typinf.dGenerate typeinfo for typeid() (as well as internals)
FilePurpose
chkformat.dValidate arguments with format specifiers for printf / scanf etc.
imphint.dGive a suggestion to e.g. import std.stdio when writeln could not be resolved.

Library files

FilePurpose
lib.dAbstract library class
libelf.dLibrary in ELF format (Unix)
libmach.dLibrary in Mach-O format (macOS)
libmscoff.dLibrary in COFF format (32/64-bit Windows)
libomf.dLibrary in OMF format (legacy 32-bit Windows)
scanelf.dExtract symbol names from a library in ELF format
scanmach.dExtract symbol names from a library in Mach-O format
scanmscoff.dExtract symbol names from a library in COFF format
scanomf.dExtract symbol names from a library in OMF format

Code generation / back-end interfacing

FilePurpose
dmsc.dConfigures and initializes the back-end
toobj.dConvert an AST that went through all semantic phases into an object file
toir.dConvert Dsymbols intermediate representation
e2ir.dConvert Expressions to intermediate representation
s2ir.dConvert Statements to intermediate representation
stmtstate.dUsed to help transform statement AST into flow graph
toctype.dConvert a D type to a type the back-end understands
tocsym.dConvert a D symbol to a symbol the linker understands (with mangled name)
argtypes_x86.dConvert a D type into simple (register) types for the 32-bit x86 ABI
argtypes_sysv_x64.d‘argtypes’ for the x86_64 System V ABI
argtypes_aarch64.d‘argtypes’ for the AArch64 ABI
glue.dGenerate the object file for function declarations
gluelayer.dDeclarations for back-end functions that the front-end invokes
todt.dConvert initializers into structures that the back-end will add to the data segment
tocvdebug.dGenerate debug info in the CV4 debug format.
objc.dObjective-C interfacing
objc_glue.dGlue code for Objective-C interop.

Name mangling

FilePurpose
cppmangle.dC++ name mangling
cppmanglewin.dC++ name mangling for Windows
dmangle.dD name mangling

Linking

FilePurpose
link.dInvoke the linker as a separate process

Special output

FilePurpose
doc.dDocumentation generation
dmacro.dDDoc macro processing
hdrgen.dConvert an AST into D source code for .di header generation, as well as -vcg-ast and error messages
json.dDescribe the module in a .json file for the -X flag
dtoh.dC++ header generation from D source files

Utility

Note: many other utilities are in dmd/root.

FilePurpose
console.dPrint error messages in color
file_manager.dKeep file contents in memory
utils.dUtility functions related to files and file paths
FilePurpose
asttypename.dPrint the internal name of an AST node (for debugging only)
printast.dPrint the AST data structure
foreachvar.dUsed in ob.d to iterate over all variables in an expression