Cappulada: What weve learned And why binding C++ is hard Johannes - - PowerPoint PPT Presentation

cappulada what we ve learned and why binding c is hard
SMART_READER_LITE
LIVE PREVIEW

Cappulada: What weve learned And why binding C++ is hard Johannes - - PowerPoint PPT Presentation

Cappulada: What weve learned And why binding C++ is hard Johannes Kliemann FOSDEM, Brussels, 2020-02-01 Recall Cappulada 2019 Goals Automatically bind C++ APIs to Existing solutions (GCC) Ada No template support Maintain


slide-1
SLIDE 1

Cappulada: What we’ve learned And why binding C++ is hard

Johannes Kliemann FOSDEM, Brussels, 2020-02-01

slide-2
SLIDE 2

2 2020-02-01

Recall Cappulada 2019 Goals

■ Automatically bind C++ APIs to Ada ■ Maintain API layout and type safety ■ Maintain semantically appropriate mappings ■ Generate mangled symbols ■ Detect and avoid name collisions ■ Be SPARK compatible where possible ■ Be better than existing solutions ■ Existing solutions (GCC) ▪ No template support ▪ No proper support to use non- valid identifiers in Ada ▪ Generates uncompilable code ▪ No automatic handling of constructors/destructors ▪ Fixing requires maintaining a fork in the long term

slide-3
SLIDE 3

3 2020-02-01

Recall Cappulada 2019 Achievements and Shortcomings

■ Achievements ▪ Templates ▪ Classes, namespaces, nesting ▪ Inheritance, with virtual classes ▪ Builtin types, typedefs, enums, arrays, pointers, references ▪ Member functions, function pointers ▪ Private, public, protected scopes ▪ Mangling ■ Shortcomings ▪ Partial template specialization ▪ Typedefs on specific types ▪ Auto keyword ▪ Operator overloads ▪ Function templates ▪ Merging multiple equally named namespaces ▪ Destructors ▪ Multiple inheritance

slide-4
SLIDE 4

4 2020-02-01

Why is it hard? Complexity and Semantics

■ Both languages are complex ▪ Ada 2012 Standard has ~1300 pages ▪ C++17 Standard has ~1600 pages ▪ C++ builds upon C so we need to support C, too ▪ C11 Standard has ~700 pages ■ Inherent semantic differences ▪ Arrays: separate type in Ada, builtin construct in C++ ▪ C++ templates can be used for meta programming, Ada generics cannot ▪ Both have different calling conventions

slide-5
SLIDE 5

2020-02-01 5

What doesn’t work?

slide-6
SLIDE 6

2020-02-01 6

What doesn’t work? Using Ada Generics with C++ Templates

■ Templates in C++ are static ■ Linker symbol is generated from the template arguments ■ Template arguments are always static

template <typename T> class A { void inc(T *t); }; A<int>::inc(int *t); _ZN1AIiE3incEPi

slide-7
SLIDE 7

2020-02-01 7

What doesn’t work? Using Ada Generics with C++ Templates

generic type T is private package A is type Class is limited null record with Convention => CPP; procedure Inc (This : in out Class; X : in out T) with Import, Convention => CPP, External_Name => “_ZN1AI” & M (X) & ”E3incEP” & M (X);

  • - _ZN1AIiE3incEPi

end A;

a.ads: entity for aspect "Import" must be a static expression a.ads: non-static function call (RM 4.9(6,18))

slide-8
SLIDE 8

2020-02-01 8

What doesn’t work? Using Ada Generics with C++ Templates

■ Theory ▪ Overloading M for Mangling ■ Practice ▪ Overloading doesn’t work on private types ▪ Return value of M is not static ▪ Generic formal parameters are never considered static ■ Potential Solution ▪ Preprocessor

function M (X : Integer) return String is (“i”);

slide-9
SLIDE 9

2020-02-01 9

What doesn’t work? C++ Pass by Value

type A is limited record X : Integer; end record with Import, Convention => CPP; function Con return A with Import, Convention => CPP, External_Name => “...”; pragma CPP_Constructor(Con); class A { public: int X; A(); };

slide-10
SLIDE 10

2020-02-01 10

What doesn’t work? C++ Pass by Value

procedure Print (X : A) with Import, Convention => CPP, External_Name => “...”; class A { public: int X; A(); }; void print(A a);

slide-11
SLIDE 11

2020-02-01 11

What doesn’t work? C++ Pass by Value

■ Problem: A will be passed by reference from Ada but expected by value in C++ ■ Considered Solution: Import Print with Convention C_Pass_By_Copy ■ Problem: C_Pass_By_Copy Convention allowed only for record type ■ Potential Solution: Define a record identical to the class ■ Problem: Unable to convert between both safely (unlike in C++)

slide-12
SLIDE 12

2020-02-01 12

What doesn’t work? Automatic destructor calling

■ Automatically called destructors are not supported in the compiler ■ Destructor could be called manually ■ Controlled objects can implement this functionality

slide-13
SLIDE 13

2020-02-01 13

What have we learned?

■ Even if everything fits it’s much work ■ Some things could in theory work ▪ With really high effort ▪ With additional tools ▪ At the cost of usability and safety ■ Some things just won’t work at all

slide-14
SLIDE 14

2020-02-01 14

Don’t you fear that you import the weirdness of C++ into Ada?

slide-15
SLIDE 15

2020-02-01 15

YES!

slide-16
SLIDE 16

2020-02-01 16

Questions?

Gneiss: A Nice Component Framework in SPARK Sunday 12:00 K.4.601 (Microkernel devroom) Johannes Kliemann kliemann@componolit.com

@Componolit · componolit.com · github.com/Componolit