{"id":1748,"date":"2018-11-02T13:03:52","date_gmt":"2018-11-02T13:03:52","guid":{"rendered":"http:\/\/dlang.org\/blog\/?p=1748"},"modified":"2021-10-08T11:02:20","modified_gmt":"2021-10-08T11:02:20","slug":"1748","status":"publish","type":"post","link":"https:\/\/dlang.org\/blog\/2018\/11\/02\/1748\/","title":{"rendered":"DMD 2.083.0 Released"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"alignleft size-full wp-image-181\" src=\"http:\/\/dlang.org\/blog\/wp-content\/uploads\/2016\/08\/d6.png\" alt=\"\" width=\"200\" height=\"200\" \/><\/p>\n<p>Version 2.083.0 of DMD, the D reference compiler, <a href=\"https:\/\/dlang.org\/download.html\">is ready for download<\/a>. <a href=\"https:\/\/dlang.org\/changelog\/2.083.0.html#bugfix-list\">The changelog lists 47 fixes and enhancements<\/a> across all components of the release package. Notable among them are some C++ compatibility enhancements and some compile-time love.<\/p>\n<h2 id=\"ccompatibility\">C++ compatibility<\/h2>\n<p>D&#8217;s support for linking to C++ binaries has been evolving and improving with nearly every release. This time, the new things aren&#8217;t very dramatic, but still very welcome to those who work with both C++ and D in the same code base.<\/p>\n<h3>What&#8217;s my runtime?<\/h3>\n<p>For a while now, D has <a href=\"https:\/\/dlang.org\/spec\/version.html#version-specification\">had predefined version identifiers<\/a> for user code to detect the C runtime implementation at compile time. These are:<\/p>\n<ul>\n<li><code>CRuntime_Bionic<\/code><\/li>\n<li><code>CRuntime_DigitalMars<\/code><\/li>\n<li><code>CRuntime_Glibc<\/code><\/li>\n<li><code>CRuntime_Microsoft<\/code><\/li>\n<li><code>CRuntime_Musl<\/code><\/li>\n<li><code>CRuntime_UClibc<\/code><\/li>\n<\/ul>\n<p>These aren&#8217;t reliable when linking against C++ code. Where the C runtime in use often depends on the system, the C++ runtime is compiler-specific. To remedy that, 2.083.0 <a href=\"https:\/\/dlang.org\/changelog\/2.083.0.html#cppVersions\">introduces a few new predefined versions<\/a>:<\/p>\n<ul>\n<li><code>CppRuntime_Clang<\/code><\/li>\n<li><code>CppRuntime_DigitalMars<\/code><\/li>\n<li><code>CppRuntime_Gcc<\/code><\/li>\n<li><code>CppRuntime_Microsoft<\/code><\/li>\n<li><code>CppRuntime_Sun<\/code><\/li>\n<\/ul>\n<h3>Why so much conflict?<\/h3>\n<p>C++ support also gets <a href=\"https:\/\/dlang.org\/changelog\/2.083.0.html#mangle_cpp\">a new syntax for declaring C++ linkage<\/a>, which <a href=\"https:\/\/dlang.org\/spec\/attribute.html#linkage\">affects how a symbol is mangled<\/a>. Consider a C++ library, MyLib, which uses the namespace <code>mylib<\/code>. The original syntax for binding a function in that namespace looks like this:<\/p>\n<pre class=\"prettyprint lang-d\">\/*\r\n The original C++:\r\n namespace mylib { void cppFunc(); }\r\n*\/\r\n\/\/ The D declaration\r\nextern(C++, mylib) void cppFunc();<\/pre>\n<p>This declares that <code>cppFunc<\/code> has C++ linkage (the symbol is mangled in a manner specific to the C++ compiler) and that <a href=\"https:\/\/dlang.org\/spec\/attribute.html#namespace\">the symbol belongs to the C++ namespace <code>mylib<\/code>\u00a0<\/a>. On the D side, the function can be referred to either as <code>cppFunc<\/code> or as <code>mylib.cppFunc<\/code>.<\/p>\n<p>In practice, this approach creates opportunities for conflict when a namespace is the same as a D keyword. It also has an impact on how one approaches the organization of a binding.\u00a0 It\u2019s natural to want to name the root package in D <code>mylib<\/code>, as it matches the library name and it is a D convention to name modules and packages using lowercase. In that case, <code>extern(C++, mylib)<\/code> declarations will not be compilable anywhere in the <code>mylib<\/code> package because the symbols conflict.<\/p>\n<p>To alleviate the problem, an alternative syntax was proposed using strings to declare the namespaces in the linkage attribute, rather than identifiers:<\/p>\n<pre class=\"prettyprint lang-d\">\/*\r\n The original C++:\r\n namespace foo { void cppFunc(); }\r\n*\/\r\n\/\/ The D declaration\r\nextern(C++, \"foo\") void cppFunc();<\/pre>\n<p>With this syntax, no <code>mylib<\/code> symbol is created on the D side; it is used solely for name mangling. No more conflicts with keywords, and D packages can be used to match the C++ namespaces on the D side. The old syntax isn&#8217;t going away anytime soon, though.<\/p>\n<h2 id=\"newcompile-timethings\">New compile-time things<\/h2>\n<p>This release provides two new built-in traits for more compile-time reflection options. <a href=\"https:\/\/dlang.org\/spec\/traits.html\">Like all built-in traits<\/a>, they are accessible via the <code>__traits<\/code> expression. There\u2019s also a new <code>pragma<\/code> that lets you bring some linker options into the source code in a very specific circumstance.<\/p>\n<h3 id=\"areyouazero\">Are you a zero?<\/h3>\n<p><code>isZeroInit<\/code> can be used to determine if <a href=\"https:\/\/dlang.org\/spec\/property.html#init\">the default initializer of a given type<\/a> is <code>0<\/code>, or more specifically, it evaluates to <code>true<\/code> <a href=\"https:\/\/dlang.org\/spec\/traits.html#isZeroInit\">if all of the init value\u2019s bits are zero<\/a>. The example below uses compile-time asserts to verify the zeroness and nonzeroness of a few default init values, but I\u2019ve saved a version that prints the results at runtime, for more immediate feedback, and <a href=\"https:\/\/run.dlang.io\/is\/WQ3VoL\">can be compiled and run from the browser<\/a>.<\/p>\n<pre class=\"prettyprint lang-d\">struct ImaZero {\r\n    int x;\r\n}\r\n\r\nstruct ImaNonZero {\r\n    int x = 10;\r\n}\r\n\r\n\/\/ double.init == double.nan\r\nstatic assert(!__traits(isZeroInit, double));\r\n\r\n\/\/ int.init == 0\r\nstatic assert(__traits(isZeroInit, int));\r\n\r\n\/\/ ImaZero.init == ImaZero(0)\r\nstatic assert(__traits(isZeroInit, ImaZero));\r\n\r\n\/\/ ImaNonZeror.init == ImaZero(10)\r\nstatic assert(!__traits(isZeroInit, ImaNonZero));<\/pre>\n<h3 id=\"computerquerytarget.\">Computer, query target.<\/h3>\n<p><a href=\"https:\/\/dlang.org\/spec\/traits.html#getTargetInfo\">The second new trait is <code>getTargetInfo<\/code><\/a>, which allows compile-time queries about the target platform. The argument is a string that serves as a key, and the result is \u201can expression describing the requested target information\u201d. Currently supported strings are <code>\u201ccppRuntimeLibrary\u201d<\/code>, <code>\u201cfloatAbi\u201d<\/code>, and <code>\u201cObjectFormat\u201d<\/code>.<\/p>\n<p>The following prints all three at compile time.<\/p>\n<pre class=\"prettyprint lang-d\">pragma(msg, __traits(getTargetInfo, \"cppRuntimeLibrary\"));\r\npragma(msg, __traits(getTargetInfo, \"floatAbi\"));\r\npragma(msg, __traits(getTargetInfo, \"objectFormat\"));<\/pre>\n<p>On Windows, using the default (<code>-m32<\/code>) DigitalMars toolchain, I see this:<\/p>\n<pre>snn\r\nhard\r\nomf<\/pre>\n<p>With the Microsoft Build Tools (via VS 2017), compiling with <code>-m64<\/code> and <code>-m32mscoff<\/code>, I see this:<\/p>\n<pre>libcmt\r\nhard\r\ncoff<\/pre>\n<h3 id=\"yolinkertakecareofthiswillya\">Yo! Linker! Take care of this, will ya?<\/h3>\n<p>D has long supported a <code>lib<\/code> pragma, allowing programmers to tell the compiler to pass a given library to the linker in source code rather than on the command line. Now, there\u2019s a new pragma in town that let\u2019s the programmer specify specific linker commands in source code and behaves rather differently. <a href=\"https:\/\/dlang.org\/spec\/pragma.html#linkerDirective\">Meet the <code>linkerDirective<\/code> pragma<\/a>:<\/p>\n<pre>pragma(linkerDirective, \"\/FAILIFMISMATCH:_ITERATOR_DEBUG_LEVEL=2\");<\/pre>\n<p>The behavior is specified as \u201cImplementation Defined\u201d. The current implementation is specced to behave like so:<\/p>\n<ul>\n<li>The string literal specifies a linker directive to be embedded in the generated object file.<\/li>\n<li>Linker directives are only supported for MS-COFF output.<\/li>\n<\/ul>\n<p>Just to make sure you didn\u2019t gloss over the first list item, look at it again. The linker directive is not passed by the compiler to the linker, but emitted to the object file. Since it is only supported for MS-COFF, that means its only a thing for folks on Windows when they are compiling with <code>-m64<\/code> or <code>-m32mscoff<\/code>. And some say the D community doesn\u2019t care about Windows!<\/p>\n<h2 id=\"ofcoursetheresmore\">Of course there\u2019s more!<\/h2>\n<p>The above are just a few cherries I picked from the list. For a deeper dive, <a href=\"https:\/\/dlang.org\/changelog\/2.083.0.html\">see the full changelog<\/a>. And <a href=\"https:\/\/dlang.org\/download.html\">head over to the Downloads<\/a> page to get the installer for your platform. It looks a lot nicer than <a href=\"http:\/\/downloads.dlang.org\/releases\/2.x\/2.083.0\/\">the boring list of files linked in the changelog<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Version 2.083.0 of DMD, the D reference compiler, is ready for download. The changelog lists 47 fixes and enhancements across all components of the release package. Notable among them are some C++ compatibility enhancements and some compile-time love. C++ compatibility D&#8217;s support for linking to C++ binaries has been evolving and improving with nearly every [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[26,12,27],"tags":[],"_links":{"self":[{"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/posts\/1748"}],"collection":[{"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/comments?post=1748"}],"version-history":[{"count":21,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/posts\/1748\/revisions"}],"predecessor-version":[{"id":1769,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/posts\/1748\/revisions\/1769"}],"wp:attachment":[{"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/media?parent=1748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/categories?post=1748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/tags?post=1748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}