Difference between revisions of "Calypso"

From D Wiki
Jump to: navigation, search
(Add usage)
 
(22 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[https://github.com/Syniurge/Calypso Calypso] is an experimental [[LDC]] fork that offers alternative interfacing to C++ based on Clang instead of implementing it "from scratch" as is currently being done in the official compiler. It attempts to widen and facilitate C++ support while minimizing intrusions into the code of the DMD frontend and LDC.
+
[https://github.com/Syniurge/Calypso Calypso] is an experimental [[LDC]] fork that offers alternative interfacing to C++ based on Clang instead of implementing it "from scratch" as currently being done in the official compiler. It attempts to widen and facilitate C++ support while minimizing intrusions into the code of the DMD frontend and LDC. You can follow and help out with the current development branch [https://github.com/Syniurge/Calypso/tree/death-to-ident-lookups-2019 here].
  
 
It is designed so that when the day comes splitting Calypso from LDC will be easy and Calypso could then exist as a LDC plugin in the form of a shared library.
 
It is designed so that when the day comes splitting Calypso from LDC will be easy and Calypso could then exist as a LDC plugin in the form of a shared library.
Line 9: Line 9:
  
 
import (C++) std.vector; // imports the std::vector class template
 
import (C++) std.vector; // imports the std::vector class template
import (C++) std._ : find; // selectively imports the std::find function template, special modules named "_" group symbols from a namespace other than classes, structs, and enums
+
import (C++) std._ : find; // selectively imports the std::find function template,  
 +
          // special modules named "_" group symbols from a namespace other than  
 +
          // classes, structs, and enums
  
 
void main()
 
void main()
Line 15: Line 17:
 
     import std.stdio : writeln;
 
     import std.stdio : writeln;
  
     auto a = new vector!char;
+
     vector!char v; // both C++ classes and structs are value types,
 +
                  // like D structs and unlike D classes
 
     foreach (c; "XYZABC")
 
     foreach (c; "XYZABC")
 
         v.push_back(c);
 
         v.push_back(c);
Line 23: Line 26:
 
     it++;
 
     it++;
  
     writeln(*it); // print 'a'
+
     writeln(*it); // print 'A'
 
}</syntaxhighlight>
 
}</syntaxhighlight>
  
 
== Advanced features ==
 
== Advanced features ==
  
Some of its features include:
+
Calypso's features include:
 
* C++ class and struct construction and destruction
 
* C++ class and struct construction and destruction
 
* Support of C++ classes making use of multiple inheritance
 
* Support of C++ classes making use of multiple inheritance
 
* D classes inheriting from C++ classes, and overriding C++ virtual methods by D methods (with the generation of C++ vtables)
 
* D classes inheriting from C++ classes, and overriding C++ virtual methods by D methods (with the generation of C++ vtables)
* Template support, instantiation of C++ class and function templates by D code
+
* Template support, instantiation of C++ class, function and variable templates by D code
 
* Overloaded operators, conversion operators
 
* Overloaded operators, conversion operators
 +
* Evaluation of <code>constexpr</code> functions at compile time
 
* Constant and empty preprocessor macros (mapped to enums)
 
* Constant and empty preprocessor macros (mapped to enums)
 
* C++ exception catching (any type, not just std::exception)
 
* C++ exception catching (any type, not just std::exception)
 +
* Debug info generation for emitted C++ symbols (template instantiations, inline and implicit functions, ...)
 +
 +
<div style="margin:0; margin-top:10px; margin-right:10px; border:1px solid #e9e7d4; border-bottom: 2px solid #c9c7b4; border-top: 1px solid #fff; padding:0 1em 1em 1em; background-color: #f9f7e4; align:right;vertical-align:top;">
 +
=== Frontend changes ===
 +
 +
Although frontend (DMD) modifications are kept to a minimum, many small changes were still needed. The most numerous are hooks (new virtual methods) here and there, and existing methods made virtual enabling Calypso to do what's specific to C++ symbols and types.
 +
 +
Class values were added to the type system, C++ classes being mapped to value types to avoid value <-> reference uncertainties (if polymorphic C++ classes were mapped to class references, a <code>#define</code> or a new method could change whether a type is value or reference).
 +
 +
Additionally, a few [[Calypso/LanguageAdditions|new language features]] that were deemed essential for reducing code verbosity and solving issues caused by semantics differences between C++ and D in a not too complicated way were implemented.
 +
</div>
  
 
== Current status ==
 
== Current status ==
  
Importing and using C++03 libraries is fully implemented (not C++0x/C++11 yet). However due to sometimes subtle differences between D and C++, to remaining bugs, or because importing a template-heavy library can be very testing for the DMD frontend (resulting in forward referencing errors), hitting a bug that makes the semantic pass fail is still a common occurrence for untested libraries. They are most of the time quickly fixable after investigating.
+
Importing and using C++ libraries is fully implemented, most C++1y features are supported. However due to sometimes subtle differences between D and C++, to remaining bugs, or because importing a template-heavy library can be very testing for the DMD frontend (resulting in forward referencing errors), hitting a bug that makes the semantic pass fail is still a common occurrence for untested libraries. They are most of the time quickly fixable after investigating.
  
Calypso has been tested primarily on Linux. OS X has been barely tested but some examples have been reported to work, and MSVC support is still incomplete.
+
Calypso has been tested primarily on Linux. OS X has been barely tested but some examples have been reported to work, and MSVC support has reached an usable state (tested on 14.0/2015) but still lacks exception handling.
  
 
Libraries known to work well are:
 
Libraries known to work well are:
* Qt5 (both Widgets and Quick)
+
* Qt 5.x (both Widgets and Quick, see [[#Qt 5 tools|below]])
* OpenCV
+
* OpenCV 3.x
 
* Tesseract
 
* Tesseract
* Ogre3D
+
* Ogre3D 1.x
 +
 
 +
This is by no means a exhaustive list of working libraries, only the ones which have been tested by the author and used for personal projects. Others have been reported to work in basic examples (Boost, GDAL, FastFlow).
 +
 
 +
<div style="margin:0; margin-top:10px; margin-right:10px; border:1px solid #e9e7d4; border-bottom: 2px solid #c9c7b4; border-top: 1px solid #fff; padding:0 1em 1em 1em; background-color: #f9f7e4; align:right;vertical-align:top;">
 +
=== Qt 5 tools ===
 +
 
 +
Using the C++ API of Qt 5 isn't straightforward even with Calypso, because [http://doc.qt.io/qt-5/metaobjects.html Qt's meta-object system] relies on code produced by the [http://doc.qt.io/qt-5/why-moc.html MOC C++ parser and code generator]. Without MOC, classes cannot have new signals, slots and properties, cannot be constructed or used from QML, etc.
 +
 
 +
Another smaller issue is that Qt Designer for Widgets' .ui files are translated by <code>uic</code> into C++ headers. Although the generated C++ header could be loaded by Calypso, this makes implementing custom widgets in D impossible.
 +
 
 +
To solve these two problems, a moc package that replicates the C++ MOC's functionality entirely with D's metaprogramming capabilities and a D generator for <code>uic</code> were created :
 +
 
 +
  https://github.com/Syniurge/Calypso-Qt
  
This is by no means a exhaustive list of working libraries, only the ones which have been successfully used in a (commercial) project. Others have been reported to work in basic examples (Boost, GDAL, FastFlow).
+
</div>
  
 
== Missing features ==
 
== Missing features ==
  
 
* Register the destructor of C++ classes and structs while allocating a C++ class through the GC (as is being done for D structs)
 
* Register the destructor of C++ classes and structs while allocating a C++ class through the GC (as is being done for D structs)
* Automatically call copy constructors on function arguments
+
* MSVC exception handling
* '''Currently Calypso has only been truly tested on Linux'''
 
  
=== MSVC TODOs ===
+
== How to help ==
  
* vtable generation for the C++ part of "hybrid" D classes deriving from a C++ class
+
More testing is needed. If your code fails to compile and you don't intend to fix the code of Calypso yourself a good way to help is to open a Github issue, copy the failing code and provide the name and version of the library used, so investigating can begin right away.
* exception handling
 
* linker support in runtime/link_autogen_cpp.cmake
 
  
== How to help ==
+
== See also ==
 +
*[[Calypso/LanguageAdditions]]
 +
*[[Calypso/TipsAndTricks]]
 +
 
 +
== External links ==
 +
 
 +
* [https://github.com/Syniurge/Calypso Source code (GitHub)]
 +
* [https://syniurgeblog.wordpress.com/2015/11/20/calypso-catching-cpp-exceptions-in-d/ Catching C++ exceptions in D with Calypso]
  
More testing is needed. If you don't intend to fix the code of Calypso yourself a good way to help is to open a Github issue, copy the failing code and provide the name and version of the library used, so investigating can begin right away.
+
[[Category:Experimental compilers]]

Latest revision as of 17:05, 17 August 2019

Calypso is an experimental LDC fork that offers alternative interfacing to C++ based on Clang instead of implementing it "from scratch" as currently being done in the official compiler. It attempts to widen and facilitate C++ support while minimizing intrusions into the code of the DMD frontend and LDC. You can follow and help out with the current development branch here.

It is designed so that when the day comes splitting Calypso from LDC will be easy and Calypso could then exist as a LDC plugin in the form of a shared library.

Usage

modmap (C++) "<vector>"; // parses <vector>, doesn't import anything
modmap (C++) "<algorithm>";

import (C++) std.vector; // imports the std::vector class template
import (C++) std._ : find; // selectively imports the std::find function template, 
           // special modules named "_" group symbols from a namespace other than 
           // classes, structs, and enums

void main()
{
    import std.stdio : writeln;

    vector!char v; // both C++ classes and structs are value types, 
                   // like D structs and unlike D classes
    foreach (c; "XYZABC")
        v.push_back(c);
    writeln("v.size = ", v.size);

    auto it = find(v.begin, v.end, 'Z');
    it++;

    writeln(*it); // print 'A'
}

Advanced features

Calypso's features include:

  • C++ class and struct construction and destruction
  • Support of C++ classes making use of multiple inheritance
  • D classes inheriting from C++ classes, and overriding C++ virtual methods by D methods (with the generation of C++ vtables)
  • Template support, instantiation of C++ class, function and variable templates by D code
  • Overloaded operators, conversion operators
  • Evaluation of constexpr functions at compile time
  • Constant and empty preprocessor macros (mapped to enums)
  • C++ exception catching (any type, not just std::exception)
  • Debug info generation for emitted C++ symbols (template instantiations, inline and implicit functions, ...)

Frontend changes

Although frontend (DMD) modifications are kept to a minimum, many small changes were still needed. The most numerous are hooks (new virtual methods) here and there, and existing methods made virtual enabling Calypso to do what's specific to C++ symbols and types.

Class values were added to the type system, C++ classes being mapped to value types to avoid value <-> reference uncertainties (if polymorphic C++ classes were mapped to class references, a #define or a new method could change whether a type is value or reference).

Additionally, a few new language features that were deemed essential for reducing code verbosity and solving issues caused by semantics differences between C++ and D in a not too complicated way were implemented.

Current status

Importing and using C++ libraries is fully implemented, most C++1y features are supported. However due to sometimes subtle differences between D and C++, to remaining bugs, or because importing a template-heavy library can be very testing for the DMD frontend (resulting in forward referencing errors), hitting a bug that makes the semantic pass fail is still a common occurrence for untested libraries. They are most of the time quickly fixable after investigating.

Calypso has been tested primarily on Linux. OS X has been barely tested but some examples have been reported to work, and MSVC support has reached an usable state (tested on 14.0/2015) but still lacks exception handling.

Libraries known to work well are:

  • Qt 5.x (both Widgets and Quick, see below)
  • OpenCV 3.x
  • Tesseract
  • Ogre3D 1.x

This is by no means a exhaustive list of working libraries, only the ones which have been tested by the author and used for personal projects. Others have been reported to work in basic examples (Boost, GDAL, FastFlow).

Qt 5 tools

Using the C++ API of Qt 5 isn't straightforward even with Calypso, because Qt's meta-object system relies on code produced by the MOC C++ parser and code generator. Without MOC, classes cannot have new signals, slots and properties, cannot be constructed or used from QML, etc.

Another smaller issue is that Qt Designer for Widgets' .ui files are translated by uic into C++ headers. Although the generated C++ header could be loaded by Calypso, this makes implementing custom widgets in D impossible.

To solve these two problems, a moc package that replicates the C++ MOC's functionality entirely with D's metaprogramming capabilities and a D generator for uic were created :

 https://github.com/Syniurge/Calypso-Qt

Missing features

  • Register the destructor of C++ classes and structs while allocating a C++ class through the GC (as is being done for D structs)
  • MSVC exception handling

How to help

More testing is needed. If your code fails to compile and you don't intend to fix the code of Calypso yourself a good way to help is to open a Github issue, copy the failing code and provide the name and version of the library used, so investigating can begin right away.

See also

External links