SlideShare uma empresa Scribd logo
1 de 29
Baixar para ler offline
PY-MA

    Why Extension Programmers Should Stop Worrying
   About Parsing and Start Thinking About Type Systems
                        (or automatic extension building is harder than it looks,
                          but that hasn’t stopped anyone from trying to do it)



                                      David M. Beazley
                               Department of Computer Science
                                  The University of Chicago
                                  beazley@cs.uchicago.edu

                                         November 9, 2002




LL2, November 9, 2002                                         1                     beazley@cs.uchicago.edu
Background

I work on programming tools and applications
       • SWIG project (www.swig.org), 1995-present.
       • Scientific software (molecular dynamics, climate modeling)
       • And some other projects.


I am not a programming languages researcher
       • Which is why I am here




LL2, November 9, 2002                              2                  beazley@cs.uchicago.edu
What Programmers Want
Access to the system
       • Files and I/O
       • Processes
       • Threads
       • Networking


Use of existing libraries
       • Graphics
       • Databases
       • GUI


Mixing of languages
       • C or C++ controlled by a high level language




LL2, November 9, 2002                               3   beazley@cs.uchicago.edu
Extension Programming
Most HLL languages support "extensions"
       • Foreign function interface (FFI).
       • Provides access to compiled code (C, assembler, etc.)
       • However, you usually have to write wrappers


Example: Python
       extern int gcd(int x, int y);
       ...
       /* Python wrapper for gcd() */
       PyObject *wrap_gcd(PyObject *self, PyObject *args) {
           int x, y, r;
           if (!PyArg_ParseTuple(args,"ii",&x,&y)) {
                return NULL;
           }
           r = gcd(x,y);
           return Py_BuildValue("i",r);
       }



LL2, November 9, 2002                              4             beazley@cs.uchicago.edu
Extension Tools
Nobody likes writing wrappers

This has spawned an "extension tool" industry...
       • SWIG                               • SIP
       • Boost.Python                       • Matwrap
       • CABLE                              • tolua
       • h2xs/xsubpp                        • TclObj
       • Inline                             • ModularTcl
       • Vtk                                • Itcl++
       • pyfort                             • GRAD
       • f2py                               • Babel
       • gwrap                              • iVRS
       • Weave                              • Wrappy
       • Pyrex                              • ... and many others.

More extension tools than languages
       • Or so it seems...


LL2, November 9, 2002                   5                    beazley@cs.uchicago.edu
"Mondo" Extension Building
Mixed-language programming
       • Application programmers are combining C/C++/Fortran with a HLL
       • Examples: C++ driven by Python, Scheme, Perl, etc.


The utility of HLLs
       • Control and configuration.
       • User interfaces.
       • Rapid prototyping.
       • Debugging.
       • Testing.
       • Systems integration.


Examples of "mondo"
       • 427 classes, 4351 methods, 220 templates (SWIG mailing list, Oct. 2002)
       • 280 classes, 2500 methods, 45 templates (iVRS, USENIX Freenix’02)
       • 200 classes (Vtk, USENIX 4th Tcl/Tk Workshop, 1996)
       • 300 classes, 5750 methods/functions (SIP PyQT extension).
                                                 ,
LL2, November 9, 2002                             6                  beazley@cs.uchicago.edu
Automation
The only practical way to wrap large applications
       • Hand-written wrappers impossible to maintain.
       • Limited programmer resources.


A common approach: Header file parsing
       • Header files define interfaces in C/C++ (separate compilation)
       • A natural starting point in the eyes of many application programmers.
       • Variations of header parsing used in many different tools.


Extension tools are trying to do more than you think
       • Automatic wrapping of C++ class hierarchies.
       • Overloaded methods, functions, and operators.
       • Templates, STL integration.
       • Smart pointers, memory management.
       • Exception handling
       • Mixed-language polymorphism (terminology?)


LL2, November 9, 2002                               7                  beazley@cs.uchicago.edu
A Prototypical Extension Tool


       .h                  Parsing                          Wrapper                    .c
                            Hack                 ?         Generation
                                                            (printf)
                                             semantics
                                             (optional)



Tools mostly target the common case
       • Can generate useful wrappers with only a superficial approach
       • Simple string matching, regex matching, reduced grammars
       • Some heuristics
       • And some customization features (a.k.a, "workarounds")
       • Ignore the hard corner cases.


Parsing header files doesn’t seem that hard
LL2, November 9, 2002                                8                   beazley@cs.uchicago.edu
Death by Corner Cases
Do any of these tools actually work?
       • Well, it depends on your definition of "works."
       • Tools kind of work "well enough," but they could be a lot better.
       • The problem is very difficult to generalize.
       • Wrapper generation is different than compiling.


PL: Parsing header files will never work
       • Too hard (or bogus). Don’t do it.
       • Have to reimplement the C++ compiler (or instrument an existing one)
       • Not enough information in header files.


Users: Parsing header files might work
       • It seems to work pretty well in a lot of specific applications.
       • Programmers really like it (well, when it works).
       • Have viable alternatives been offered?



LL2, November 9, 2002                                  9                     beazley@cs.uchicago.edu
Parsing is the Wrong Focus
Header file parsing is only a convenient user-interface
       • Yes, users like it.


Extension building is not a parsing problem
       • Marshalling of types (representation).
       • Creating wrappers around objects (C++ types).
       • In fact, you are gluing two type systems together (in a practical sense)
       • Wrappers and parsing are merely implementation details.


Parsing alone is not enough to understand C++
       • Classes introduce "types"
       • The members of a class determine its behavior.
       • Templates/namespaces/typedef introduce very complicated relationships.
       • (Then again, maybe C++ can’t be understood by anything)




LL2, November 9, 2002                                10                  beazley@cs.uchicago.edu
The Classic Parsing Problem
Not enough information in headers:

                        void blah(int *x, int n);


                                 What is this?

       • An input value?
       • An array?
       • An output value?
       • An input/output value?
       • Is it somehow related to n?


Common workarounds (modifiers, naming conventions)
       void blah(%out int *x, int n);
       void blah(int *out_x, int n);

This has never been a major obstacle

LL2, November 9, 2002                            11   beazley@cs.uchicago.edu
Breaking The Common Case
What is this?


       extern binop_t   add, sub, mul, div;

A declaration of 4 global variables (obviously).




LL2, November 9, 2002                   12         beazley@cs.uchicago.edu
Breaking The Common Case
What is this?

       typedef int binop_t(int, int);
       extern binop_t add, sub, mul, div;

A declaration of 4 global variables (obviously).

Well, actually, it’s a declaration of 4 functions.
       • Okay, it’s perverse.


Question: what will a tool do?
       • A lot of tools will break.


==> You can’t just look for simple text patterns.



LL2, November 9, 2002                 13             beazley@cs.uchicago.edu
Breaking Typenames
Consider:
       namespace A {
           typedef int Integer;
       }
       class B {
       public:
           typedef A::Integer type;
       };
       template<class T> struct Foo {
           typedef typename T::type Footype;
       };
       using A::Integer;

       extern Foo<B>::Footype spam(Integer x, B::type y, int z);

Question: Can a tool figure out the types?
       • Not if it looks for simple names like "int"


==> Types aren’t strings and more than a table lookup.

LL2, November 9, 2002                                  14   beazley@cs.uchicago.edu
Breaking Classes
Consider a class:
       class Foo : public Bar {
       public:
             int blah(int x);
       };


Question: Can you create a Foo?
       • Can you create a wrapper that constructs a Foo?
       • Well, that depends. What is Bar?




LL2, November 9, 2002                             15       beazley@cs.uchicago.edu
Breaking Classes
Consider a class:
       class Foo : public Bar {
       public:
             int blah(int x);
       };


Question: Can you create a Foo?
       • Can you create a wrapper that constructs a Foo?
       • Well, that depends. What is Bar?


Examples:
       class Bar {                         class Bar {
       private:                            public:
           Bar();                             virtual void spam() = 0;
       };                                  };


==> Can’t look at classes in isolation

LL2, November 9, 2002                             16          beazley@cs.uchicago.edu
Overloading
What happens here?
       void foo(char *s);               // A
       void foo(double x);              // B
       void foo(int x);                 // C


Consider a language like Tcl
       • Typeless. All objects are strings.


Yet, some tools actually seem to work
       % foo 3              # Invokes C
       % foo 3.0            # Invokes B
       % foo hello          # Invokes A

A type hierarchy imposed on a typeless target (somehow)

==> Definitely more than parsing

LL2, November 9, 2002                          17   beazley@cs.uchicago.edu
Breaking Your Head
Advanced C++ idioms. Example: smart-pointers
       struct Foo {
           int x;
           int blah(int x);
       };
       template<class T> class SmartPtr {
           ...
           T *operator->();
           ...
       };
       extern SmartPtr<Foo> create_foo();


Now, in Python:
       >>> f = create_foo()
       >>> f.x                # Implicit dereference through
       3                      # operator->()
       >>> f.blah()

==> More than parsing (and possibly a bad idea).

LL2, November 9, 2002                  18             beazley@cs.uchicago.edu
Stepping Back
Wrapping C++ is nontrivial
       • You knew this.
       • Many corner cases.
       • Dark and obscure parts of the standard.
       • Even more complicated than C wrapping (which is already hard).


Parsing it is not enough
       • This, I am absolutely sure about.


Non-trivial interaction of several components
       • C++ types, target language, semantics, user-driven customization features.


My view:
       • Tool builders have been working on the problem at the wrong level
       • Reflects my experience with the SWIG project.


LL2, November 9, 2002                              19                  beazley@cs.uchicago.edu
An Alternative View


       .h                                                    Wrapper               .c
                                        Type System         Generation
                                     C++/Target Language
                                                             (printf)
                         Parsing
                          Hack
                        (optional)



A Better Approach
       • Build extension tools around "type" systems, not parsers.


It makes sense
       • Wrapper generation is a fundamentally a problem in type systems.
       • C++ is only understood through its type system.
       • It makes sense even if you don’t parse C++.

LL2, November 9, 2002                                20              beazley@cs.uchicago.edu
A Tale of Two Tools
Two type-based extension building tools
       • Boost.Python (http://www.boost.org/libs/python/doc)
       • SWIG (http://www.swig.org)


Both of these tools
       • Support a large subset of C++
       • Support a variety of advanced features (overloading, templates, etc.)
       • Both tools are primarily built around the C++ type system.
       • Both unconventional in approach.




LL2, November 9, 2002                               21                  beazley@cs.uchicago.edu
Boost.Python
Wrapper generation using the C++ compiler itself
       struct Foo {
           double x;
           Foo(double);
           int blah(int x, int y);
       };

       BOOST_PYTHON_MODULE_INIT(foomodule) {
           class_<Foo>("Foo")
              .def(init<double>())
              .def("blah", &Foo::blah)
              .def_readwrite("x",&Foo::x);
       }
What is this doing?
       • Using C++ template machinery to automatically generate wrappers to C++.
       • Template meta-programming.
       • There is no "tool" here. The C++ compiler is the tool.


C++ type system used to wrap C++ types.
LL2, November 9, 2002                            22                 beazley@cs.uchicago.edu
Boost.Python
This is very cool
       • It is able to handle very complicated C++
       • Especially templates, namespaces, other advanced features.
       • Didn’t require an implementation of a C++ parser or a C++ type system.


Why it’s interesting
       • Very unconventional (the only tool I know that works like this)
       • Operates completely within the C++ type system domain.
       • A good example of the right problem focus.




LL2, November 9, 2002                                23                    beazley@cs.uchicago.edu
SWIG
Wrappers generated through pattern matching rules

       // Typemap patterns (not supplied by user)
       %typemap(in) int { $1 = PyInt_AsLong($input); }
       %typemap(out) int { $result = PyInt_FromLong($1); }
       ...
       %typemap(in) double { $1 = PyFloat_AsDouble($input); }
       %typemap(in) char * { $1 = PyString_AsString($input); }
       ...

       // Code to wrap (supplied by user)
       class Foo {
           double x;
           Foo(double);
           int blah(int x, int y);
       };

Patterns built into the C++ type system.
       • The patterns provide code fragments and other details.


LL2, November 9, 2002                              24             beazley@cs.uchicago.edu
SWIG - Pattern Matching
       %typemap(in) int { $1 = PyInt_AsLong($input); }
       %typemap(out) int { $result = PyInt_FromLong($1); }

       namespace A {
           typedef int Integer;
       }
       class B {
       public:
           typedef A::Integer type;
       };
       template<class T> struct Foo {
           typedef typename T::type Footype;
       };
       using A::Integer;

       extern Foo<B>::Footype spam(Integer x, A::Integer y, int z);

Pattern matching is more than string matching



LL2, November 9, 2002                  25             beazley@cs.uchicago.edu
SWIG - Pattern Matching
Patterns may incorporate more than types
       • Argument names, typedef names, argument sequences

       %typemap(in) int positive { ... }
       %typemap(in) dnonzero_t { ... }
       %typemap(in) (char *str, int len) {
            $1 = PyString_AsString($input);
            $2 = PyString_Size($input);
       }
       ...
       typedef double dnonzero_t;
       extern int factorial(int positive);
       extern double inverse(dnonzero_t);
       extern int find(char *str, int len, char *pat);

There’s more to this, but that’s a different talk
       • Patterns used for specifying behavior (in, out, arrays, checking, etc.)
       • Typemaps can be incorporated into classes, templates, etc.
       • Declaration annotation.


LL2, November 9, 2002                                26                   beazley@cs.uchicago.edu
Big Picture
By shifting the focus to "types"...
       • It solves really hard problems (templates, namespaces, overloading, etc...)
       • It eliminates vast numbers of corner cases.
       • Greater reliability and simplified maintenance
       • It allows tool builders to focus on more important problems
       • Users really like it, but they don’t quite know why (it just "works").


But this work is incomplete and ongoing....
       • No tool is perfect or free of bugs.
       • There is definitely no "silver bullet."
       • Still fighting a very complicated, possibly intractable, but practical problem.




LL2, November 9, 2002                                 27                   beazley@cs.uchicago.edu
Where do we go from here?
There are hard problems that need to be resolved
       • Extension building tools are only starting to play with type systems.
       • I don’t think that anyone has quite figured it out.


Connection to programming languages
       • This is clearly a programming language issue (at least I think so)
       • However, it’s hard for us to make a precise connection to prior work.
       • If not type systems, what is the right connection?
       • Also strong ties to software engineering.


Obstacles
       • Cultural divide. Extension tools primarily built by applications programmers.
       • Language designers have better things to worry about.
       • A lot of work in FFIs primarily focused on C, representation issues.
       • "Wasn’t this problem dismissed/solved in 1971?"



LL2, November 9, 2002                                28                  beazley@cs.uchicago.edu
In Closing
To extension tool builders...
       • Parsing is not the most important problem.
       • Think about the C/C++ type system--that’s where the real action is.
       • You will like it.


To the programming languages community...
       • A lot of people are building extension tools. Pay attention.
       • This is an important practical problem to application programmers.
       • They don’t want to reimplement existing code and libraries.
       • There is an opportunity to make connections (and have impact).
       • Metaobject protocol? AOP? FFI’s? Type systems?


Acknowledgements
       • David Abrahams
       • William Fulton, Matthias Koeppe, Jason Stewart, Lyle Johnson, Richard Palmer,
       Luigi Ballabio, Sam Liddicott, Art Yerkes, Thien-Thi Nguyen, Marcelo Matus, Loic
       Dachary, Masaki Fukushima, and others.

LL2, November 9, 2002                               29                  beazley@cs.uchicago.edu

Mais conteúdo relacionado

Mais procurados

Generator Tricks for Systems Programmers, v2.0
Generator Tricks for Systems Programmers, v2.0Generator Tricks for Systems Programmers, v2.0
Generator Tricks for Systems Programmers, v2.0David Beazley (Dabeaz LLC)
 
In Search of the Perfect Global Interpreter Lock
In Search of the Perfect Global Interpreter LockIn Search of the Perfect Global Interpreter Lock
In Search of the Perfect Global Interpreter LockDavid Beazley (Dabeaz LLC)
 
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsWAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsDavid Beazley (Dabeaz LLC)
 
Ry pyconjp2015 karaoke
Ry pyconjp2015 karaokeRy pyconjp2015 karaoke
Ry pyconjp2015 karaokeRenyuan Lyu
 
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...David Beazley (Dabeaz LLC)
 
groovy DSLs from beginner to expert
groovy DSLs from beginner to expertgroovy DSLs from beginner to expert
groovy DSLs from beginner to expertPaul King
 
Executable Bloat - How it happens and how we can fight it
Executable Bloat - How it happens and how we can fight itExecutable Bloat - How it happens and how we can fight it
Executable Bloat - How it happens and how we can fight itElectronic Arts / DICE
 
Bar Camp Auckland - Mongo DB Presentation BCA4
Bar Camp Auckland - Mongo DB Presentation BCA4Bar Camp Auckland - Mongo DB Presentation BCA4
Bar Camp Auckland - Mongo DB Presentation BCA4John Ballinger
 
Thinking hard about_python
Thinking hard about_pythonThinking hard about_python
Thinking hard about_pythonDaniel Greenfeld
 
Testing Apache Modules with Python and Ctypes
Testing Apache Modules with Python and CtypesTesting Apache Modules with Python and Ctypes
Testing Apache Modules with Python and CtypesMarkus Litz
 
Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)
Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)
Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)Puppet
 
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)Takayuki Shimizukawa
 
Everything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insEverything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insAndrew Dupont
 
Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)Eugene Lazutkin
 

Mais procurados (20)

Generator Tricks for Systems Programmers, v2.0
Generator Tricks for Systems Programmers, v2.0Generator Tricks for Systems Programmers, v2.0
Generator Tricks for Systems Programmers, v2.0
 
An Introduction to Python Concurrency
An Introduction to Python ConcurrencyAn Introduction to Python Concurrency
An Introduction to Python Concurrency
 
Mastering Python 3 I/O (Version 2)
Mastering Python 3 I/O (Version 2)Mastering Python 3 I/O (Version 2)
Mastering Python 3 I/O (Version 2)
 
In Search of the Perfect Global Interpreter Lock
In Search of the Perfect Global Interpreter LockIn Search of the Perfect Global Interpreter Lock
In Search of the Perfect Global Interpreter Lock
 
Python Generator Hacking
Python Generator HackingPython Generator Hacking
Python Generator Hacking
 
Interfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIGInterfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIG
 
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsWAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
 
Ry pyconjp2015 karaoke
Ry pyconjp2015 karaokeRy pyconjp2015 karaoke
Ry pyconjp2015 karaoke
 
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
 
groovy DSLs from beginner to expert
groovy DSLs from beginner to expertgroovy DSLs from beginner to expert
groovy DSLs from beginner to expert
 
Executable Bloat - How it happens and how we can fight it
Executable Bloat - How it happens and how we can fight itExecutable Bloat - How it happens and how we can fight it
Executable Bloat - How it happens and how we can fight it
 
Tales@tdc
Tales@tdcTales@tdc
Tales@tdc
 
Bar Camp Auckland - Mongo DB Presentation BCA4
Bar Camp Auckland - Mongo DB Presentation BCA4Bar Camp Auckland - Mongo DB Presentation BCA4
Bar Camp Auckland - Mongo DB Presentation BCA4
 
Thinking hard about_python
Thinking hard about_pythonThinking hard about_python
Thinking hard about_python
 
Testing Apache Modules with Python and Ctypes
Testing Apache Modules with Python and CtypesTesting Apache Modules with Python and Ctypes
Testing Apache Modules with Python and Ctypes
 
Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)
Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)
Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)
 
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
 
Chapter08
Chapter08Chapter08
Chapter08
 
Everything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insEverything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-ins
 
Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)
 

Destaque

Ctypes в игровых приложениях на python
Ctypes в игровых приложениях на pythonCtypes в игровых приложениях на python
Ctypes в игровых приложениях на pythonPyNSK
 
A Curious Course on Coroutines and Concurrency
A Curious Course on Coroutines and ConcurrencyA Curious Course on Coroutines and Concurrency
A Curious Course on Coroutines and ConcurrencyDavid Beazley (Dabeaz LLC)
 
Using Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard IIUsing Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard IIDavid Beazley (Dabeaz LLC)
 
Подключение внешних библиотек в python
Подключение внешних библиотек в pythonПодключение внешних библиотек в python
Подключение внешних библиотек в pythonMaxim Shalamov
 

Destaque (6)

Generators: The Final Frontier
Generators: The Final FrontierGenerators: The Final Frontier
Generators: The Final Frontier
 
Writing Parsers and Compilers with PLY
Writing Parsers and Compilers with PLYWriting Parsers and Compilers with PLY
Writing Parsers and Compilers with PLY
 
Ctypes в игровых приложениях на python
Ctypes в игровых приложениях на pythonCtypes в игровых приложениях на python
Ctypes в игровых приложениях на python
 
A Curious Course on Coroutines and Concurrency
A Curious Course on Coroutines and ConcurrencyA Curious Course on Coroutines and Concurrency
A Curious Course on Coroutines and Concurrency
 
Using Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard IIUsing Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard II
 
Подключение внешних библиотек в python
Подключение внешних библиотек в pythonПодключение внешних библиотек в python
Подключение внешних библиотек в python
 

Semelhante a Why Extension Programmers Should Stop Worrying About Parsing and Start Thinking about Type Systems

Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...InfinIT - Innovationsnetværket for it
 
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
2015 bioinformatics python_introduction_wim_vancriekinge_vfinalProf. Wim Van Criekinge
 
James Coplien - Trygve - October 17, 2016
James Coplien - Trygve - October 17, 2016James Coplien - Trygve - October 17, 2016
James Coplien - Trygve - October 17, 2016Foo Café Copenhagen
 
An introduction to Julia
An introduction to JuliaAn introduction to Julia
An introduction to JuliaJiahao Chen
 
DSL's with Groovy
DSL's with GroovyDSL's with Groovy
DSL's with Groovypaulbowler
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!Alessandro Giorgetti
 
14_Ed_Symp_Open_Source
14_Ed_Symp_Open_Source14_Ed_Symp_Open_Source
14_Ed_Symp_Open_SourceSteve Arnold
 
Open Source Design at Ignite lightning talk
Open Source Design at Ignite lightning talkOpen Source Design at Ignite lightning talk
Open Source Design at Ignite lightning talkMushon Zer-Aviv
 
Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Codersebbe
 
Intro to Python for C# Developers
Intro to Python for C# DevelopersIntro to Python for C# Developers
Intro to Python for C# DevelopersSarah Dutkiewicz
 
2023-My AI Experience - Colm Dunphy.pdf
2023-My AI Experience - Colm Dunphy.pdf2023-My AI Experience - Colm Dunphy.pdf
2023-My AI Experience - Colm Dunphy.pdfColm Dunphy
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013Iván Montes
 
Documentation An Engineering Problem Unsolved
Documentation  An Engineering Problem UnsolvedDocumentation  An Engineering Problem Unsolved
Documentation An Engineering Problem UnsolvedSchalk Cronjé
 
C-and-Cpp-Brochure-English. .
C-and-Cpp-Brochure-English.               .C-and-Cpp-Brochure-English.               .
C-and-Cpp-Brochure-English. .spotguys705
 
GR8Conf 2011: STS DSL Support
GR8Conf 2011: STS DSL SupportGR8Conf 2011: STS DSL Support
GR8Conf 2011: STS DSL SupportGR8Conf
 

Semelhante a Why Extension Programmers Should Stop Worrying About Parsing and Start Thinking about Type Systems (20)

Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...
 
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
 
James Coplien - Trygve - October 17, 2016
James Coplien - Trygve - October 17, 2016James Coplien - Trygve - October 17, 2016
James Coplien - Trygve - October 17, 2016
 
An introduction to Julia
An introduction to JuliaAn introduction to Julia
An introduction to Julia
 
Metamorphic Domain-Specific Languages
Metamorphic Domain-Specific LanguagesMetamorphic Domain-Specific Languages
Metamorphic Domain-Specific Languages
 
DSL's with Groovy
DSL's with GroovyDSL's with Groovy
DSL's with Groovy
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!
 
Illustrated Code (ASE 2021)
Illustrated Code (ASE 2021)Illustrated Code (ASE 2021)
Illustrated Code (ASE 2021)
 
14_Ed_Symp_Open_Source
14_Ed_Symp_Open_Source14_Ed_Symp_Open_Source
14_Ed_Symp_Open_Source
 
Open Source Design at Ignite lightning talk
Open Source Design at Ignite lightning talkOpen Source Design at Ignite lightning talk
Open Source Design at Ignite lightning talk
 
Java basics
Java basicsJava basics
Java basics
 
Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Code
 
Intro to Python for C# Developers
Intro to Python for C# DevelopersIntro to Python for C# Developers
Intro to Python for C# Developers
 
The Big Picture
The Big PictureThe Big Picture
The Big Picture
 
2023-My AI Experience - Colm Dunphy.pdf
2023-My AI Experience - Colm Dunphy.pdf2023-My AI Experience - Colm Dunphy.pdf
2023-My AI Experience - Colm Dunphy.pdf
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
 
Documentation An Engineering Problem Unsolved
Documentation  An Engineering Problem UnsolvedDocumentation  An Engineering Problem Unsolved
Documentation An Engineering Problem Unsolved
 
C-and-Cpp-Brochure-English. .
C-and-Cpp-Brochure-English.               .C-and-Cpp-Brochure-English.               .
C-and-Cpp-Brochure-English. .
 
Fossetcon15
Fossetcon15Fossetcon15
Fossetcon15
 
GR8Conf 2011: STS DSL Support
GR8Conf 2011: STS DSL SupportGR8Conf 2011: STS DSL Support
GR8Conf 2011: STS DSL Support
 

Último

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 

Último (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Why Extension Programmers Should Stop Worrying About Parsing and Start Thinking about Type Systems

  • 1. PY-MA Why Extension Programmers Should Stop Worrying About Parsing and Start Thinking About Type Systems (or automatic extension building is harder than it looks, but that hasn’t stopped anyone from trying to do it) David M. Beazley Department of Computer Science The University of Chicago beazley@cs.uchicago.edu November 9, 2002 LL2, November 9, 2002 1 beazley@cs.uchicago.edu
  • 2. Background I work on programming tools and applications • SWIG project (www.swig.org), 1995-present. • Scientific software (molecular dynamics, climate modeling) • And some other projects. I am not a programming languages researcher • Which is why I am here LL2, November 9, 2002 2 beazley@cs.uchicago.edu
  • 3. What Programmers Want Access to the system • Files and I/O • Processes • Threads • Networking Use of existing libraries • Graphics • Databases • GUI Mixing of languages • C or C++ controlled by a high level language LL2, November 9, 2002 3 beazley@cs.uchicago.edu
  • 4. Extension Programming Most HLL languages support "extensions" • Foreign function interface (FFI). • Provides access to compiled code (C, assembler, etc.) • However, you usually have to write wrappers Example: Python extern int gcd(int x, int y); ... /* Python wrapper for gcd() */ PyObject *wrap_gcd(PyObject *self, PyObject *args) { int x, y, r; if (!PyArg_ParseTuple(args,"ii",&x,&y)) { return NULL; } r = gcd(x,y); return Py_BuildValue("i",r); } LL2, November 9, 2002 4 beazley@cs.uchicago.edu
  • 5. Extension Tools Nobody likes writing wrappers This has spawned an "extension tool" industry... • SWIG • SIP • Boost.Python • Matwrap • CABLE • tolua • h2xs/xsubpp • TclObj • Inline • ModularTcl • Vtk • Itcl++ • pyfort • GRAD • f2py • Babel • gwrap • iVRS • Weave • Wrappy • Pyrex • ... and many others. More extension tools than languages • Or so it seems... LL2, November 9, 2002 5 beazley@cs.uchicago.edu
  • 6. "Mondo" Extension Building Mixed-language programming • Application programmers are combining C/C++/Fortran with a HLL • Examples: C++ driven by Python, Scheme, Perl, etc. The utility of HLLs • Control and configuration. • User interfaces. • Rapid prototyping. • Debugging. • Testing. • Systems integration. Examples of "mondo" • 427 classes, 4351 methods, 220 templates (SWIG mailing list, Oct. 2002) • 280 classes, 2500 methods, 45 templates (iVRS, USENIX Freenix’02) • 200 classes (Vtk, USENIX 4th Tcl/Tk Workshop, 1996) • 300 classes, 5750 methods/functions (SIP PyQT extension). , LL2, November 9, 2002 6 beazley@cs.uchicago.edu
  • 7. Automation The only practical way to wrap large applications • Hand-written wrappers impossible to maintain. • Limited programmer resources. A common approach: Header file parsing • Header files define interfaces in C/C++ (separate compilation) • A natural starting point in the eyes of many application programmers. • Variations of header parsing used in many different tools. Extension tools are trying to do more than you think • Automatic wrapping of C++ class hierarchies. • Overloaded methods, functions, and operators. • Templates, STL integration. • Smart pointers, memory management. • Exception handling • Mixed-language polymorphism (terminology?) LL2, November 9, 2002 7 beazley@cs.uchicago.edu
  • 8. A Prototypical Extension Tool .h Parsing Wrapper .c Hack ? Generation (printf) semantics (optional) Tools mostly target the common case • Can generate useful wrappers with only a superficial approach • Simple string matching, regex matching, reduced grammars • Some heuristics • And some customization features (a.k.a, "workarounds") • Ignore the hard corner cases. Parsing header files doesn’t seem that hard LL2, November 9, 2002 8 beazley@cs.uchicago.edu
  • 9. Death by Corner Cases Do any of these tools actually work? • Well, it depends on your definition of "works." • Tools kind of work "well enough," but they could be a lot better. • The problem is very difficult to generalize. • Wrapper generation is different than compiling. PL: Parsing header files will never work • Too hard (or bogus). Don’t do it. • Have to reimplement the C++ compiler (or instrument an existing one) • Not enough information in header files. Users: Parsing header files might work • It seems to work pretty well in a lot of specific applications. • Programmers really like it (well, when it works). • Have viable alternatives been offered? LL2, November 9, 2002 9 beazley@cs.uchicago.edu
  • 10. Parsing is the Wrong Focus Header file parsing is only a convenient user-interface • Yes, users like it. Extension building is not a parsing problem • Marshalling of types (representation). • Creating wrappers around objects (C++ types). • In fact, you are gluing two type systems together (in a practical sense) • Wrappers and parsing are merely implementation details. Parsing alone is not enough to understand C++ • Classes introduce "types" • The members of a class determine its behavior. • Templates/namespaces/typedef introduce very complicated relationships. • (Then again, maybe C++ can’t be understood by anything) LL2, November 9, 2002 10 beazley@cs.uchicago.edu
  • 11. The Classic Parsing Problem Not enough information in headers: void blah(int *x, int n); What is this? • An input value? • An array? • An output value? • An input/output value? • Is it somehow related to n? Common workarounds (modifiers, naming conventions) void blah(%out int *x, int n); void blah(int *out_x, int n); This has never been a major obstacle LL2, November 9, 2002 11 beazley@cs.uchicago.edu
  • 12. Breaking The Common Case What is this? extern binop_t add, sub, mul, div; A declaration of 4 global variables (obviously). LL2, November 9, 2002 12 beazley@cs.uchicago.edu
  • 13. Breaking The Common Case What is this? typedef int binop_t(int, int); extern binop_t add, sub, mul, div; A declaration of 4 global variables (obviously). Well, actually, it’s a declaration of 4 functions. • Okay, it’s perverse. Question: what will a tool do? • A lot of tools will break. ==> You can’t just look for simple text patterns. LL2, November 9, 2002 13 beazley@cs.uchicago.edu
  • 14. Breaking Typenames Consider: namespace A { typedef int Integer; } class B { public: typedef A::Integer type; }; template<class T> struct Foo { typedef typename T::type Footype; }; using A::Integer; extern Foo<B>::Footype spam(Integer x, B::type y, int z); Question: Can a tool figure out the types? • Not if it looks for simple names like "int" ==> Types aren’t strings and more than a table lookup. LL2, November 9, 2002 14 beazley@cs.uchicago.edu
  • 15. Breaking Classes Consider a class: class Foo : public Bar { public: int blah(int x); }; Question: Can you create a Foo? • Can you create a wrapper that constructs a Foo? • Well, that depends. What is Bar? LL2, November 9, 2002 15 beazley@cs.uchicago.edu
  • 16. Breaking Classes Consider a class: class Foo : public Bar { public: int blah(int x); }; Question: Can you create a Foo? • Can you create a wrapper that constructs a Foo? • Well, that depends. What is Bar? Examples: class Bar { class Bar { private: public: Bar(); virtual void spam() = 0; }; }; ==> Can’t look at classes in isolation LL2, November 9, 2002 16 beazley@cs.uchicago.edu
  • 17. Overloading What happens here? void foo(char *s); // A void foo(double x); // B void foo(int x); // C Consider a language like Tcl • Typeless. All objects are strings. Yet, some tools actually seem to work % foo 3 # Invokes C % foo 3.0 # Invokes B % foo hello # Invokes A A type hierarchy imposed on a typeless target (somehow) ==> Definitely more than parsing LL2, November 9, 2002 17 beazley@cs.uchicago.edu
  • 18. Breaking Your Head Advanced C++ idioms. Example: smart-pointers struct Foo { int x; int blah(int x); }; template<class T> class SmartPtr { ... T *operator->(); ... }; extern SmartPtr<Foo> create_foo(); Now, in Python: >>> f = create_foo() >>> f.x # Implicit dereference through 3 # operator->() >>> f.blah() ==> More than parsing (and possibly a bad idea). LL2, November 9, 2002 18 beazley@cs.uchicago.edu
  • 19. Stepping Back Wrapping C++ is nontrivial • You knew this. • Many corner cases. • Dark and obscure parts of the standard. • Even more complicated than C wrapping (which is already hard). Parsing it is not enough • This, I am absolutely sure about. Non-trivial interaction of several components • C++ types, target language, semantics, user-driven customization features. My view: • Tool builders have been working on the problem at the wrong level • Reflects my experience with the SWIG project. LL2, November 9, 2002 19 beazley@cs.uchicago.edu
  • 20. An Alternative View .h Wrapper .c Type System Generation C++/Target Language (printf) Parsing Hack (optional) A Better Approach • Build extension tools around "type" systems, not parsers. It makes sense • Wrapper generation is a fundamentally a problem in type systems. • C++ is only understood through its type system. • It makes sense even if you don’t parse C++. LL2, November 9, 2002 20 beazley@cs.uchicago.edu
  • 21. A Tale of Two Tools Two type-based extension building tools • Boost.Python (http://www.boost.org/libs/python/doc) • SWIG (http://www.swig.org) Both of these tools • Support a large subset of C++ • Support a variety of advanced features (overloading, templates, etc.) • Both tools are primarily built around the C++ type system. • Both unconventional in approach. LL2, November 9, 2002 21 beazley@cs.uchicago.edu
  • 22. Boost.Python Wrapper generation using the C++ compiler itself struct Foo { double x; Foo(double); int blah(int x, int y); }; BOOST_PYTHON_MODULE_INIT(foomodule) { class_<Foo>("Foo") .def(init<double>()) .def("blah", &Foo::blah) .def_readwrite("x",&Foo::x); } What is this doing? • Using C++ template machinery to automatically generate wrappers to C++. • Template meta-programming. • There is no "tool" here. The C++ compiler is the tool. C++ type system used to wrap C++ types. LL2, November 9, 2002 22 beazley@cs.uchicago.edu
  • 23. Boost.Python This is very cool • It is able to handle very complicated C++ • Especially templates, namespaces, other advanced features. • Didn’t require an implementation of a C++ parser or a C++ type system. Why it’s interesting • Very unconventional (the only tool I know that works like this) • Operates completely within the C++ type system domain. • A good example of the right problem focus. LL2, November 9, 2002 23 beazley@cs.uchicago.edu
  • 24. SWIG Wrappers generated through pattern matching rules // Typemap patterns (not supplied by user) %typemap(in) int { $1 = PyInt_AsLong($input); } %typemap(out) int { $result = PyInt_FromLong($1); } ... %typemap(in) double { $1 = PyFloat_AsDouble($input); } %typemap(in) char * { $1 = PyString_AsString($input); } ... // Code to wrap (supplied by user) class Foo { double x; Foo(double); int blah(int x, int y); }; Patterns built into the C++ type system. • The patterns provide code fragments and other details. LL2, November 9, 2002 24 beazley@cs.uchicago.edu
  • 25. SWIG - Pattern Matching %typemap(in) int { $1 = PyInt_AsLong($input); } %typemap(out) int { $result = PyInt_FromLong($1); } namespace A { typedef int Integer; } class B { public: typedef A::Integer type; }; template<class T> struct Foo { typedef typename T::type Footype; }; using A::Integer; extern Foo<B>::Footype spam(Integer x, A::Integer y, int z); Pattern matching is more than string matching LL2, November 9, 2002 25 beazley@cs.uchicago.edu
  • 26. SWIG - Pattern Matching Patterns may incorporate more than types • Argument names, typedef names, argument sequences %typemap(in) int positive { ... } %typemap(in) dnonzero_t { ... } %typemap(in) (char *str, int len) { $1 = PyString_AsString($input); $2 = PyString_Size($input); } ... typedef double dnonzero_t; extern int factorial(int positive); extern double inverse(dnonzero_t); extern int find(char *str, int len, char *pat); There’s more to this, but that’s a different talk • Patterns used for specifying behavior (in, out, arrays, checking, etc.) • Typemaps can be incorporated into classes, templates, etc. • Declaration annotation. LL2, November 9, 2002 26 beazley@cs.uchicago.edu
  • 27. Big Picture By shifting the focus to "types"... • It solves really hard problems (templates, namespaces, overloading, etc...) • It eliminates vast numbers of corner cases. • Greater reliability and simplified maintenance • It allows tool builders to focus on more important problems • Users really like it, but they don’t quite know why (it just "works"). But this work is incomplete and ongoing.... • No tool is perfect or free of bugs. • There is definitely no "silver bullet." • Still fighting a very complicated, possibly intractable, but practical problem. LL2, November 9, 2002 27 beazley@cs.uchicago.edu
  • 28. Where do we go from here? There are hard problems that need to be resolved • Extension building tools are only starting to play with type systems. • I don’t think that anyone has quite figured it out. Connection to programming languages • This is clearly a programming language issue (at least I think so) • However, it’s hard for us to make a precise connection to prior work. • If not type systems, what is the right connection? • Also strong ties to software engineering. Obstacles • Cultural divide. Extension tools primarily built by applications programmers. • Language designers have better things to worry about. • A lot of work in FFIs primarily focused on C, representation issues. • "Wasn’t this problem dismissed/solved in 1971?" LL2, November 9, 2002 28 beazley@cs.uchicago.edu
  • 29. In Closing To extension tool builders... • Parsing is not the most important problem. • Think about the C/C++ type system--that’s where the real action is. • You will like it. To the programming languages community... • A lot of people are building extension tools. Pay attention. • This is an important practical problem to application programmers. • They don’t want to reimplement existing code and libraries. • There is an opportunity to make connections (and have impact). • Metaobject protocol? AOP? FFI’s? Type systems? Acknowledgements • David Abrahams • William Fulton, Matthias Koeppe, Jason Stewart, Lyle Johnson, Richard Palmer, Luigi Ballabio, Sam Liddicott, Art Yerkes, Thien-Thi Nguyen, Marcelo Matus, Loic Dachary, Masaki Fukushima, and others. LL2, November 9, 2002 29 beazley@cs.uchicago.edu