This module creates descriptions of C++ classes from source code, by using external parsers (GCC-XML, Clang AST) and the type system.
author: | Anthony Scopatz <scopatz@gmail.com> |
---|
A key component of API wrapper generation is having a a top-level, abstract representation of the software that is being wrapped. In C++ there are three basic constructs which may be wrapped: variables, functions, and classes. Here we restrict ourselves to wrapping classes (though ironically these are the most complex of the three).
The abstract representation of a C++ class is known as a description (abbr. desc). This description is simply a Python dictionary with a specific structure. This structure makes heavy use of the type system to declare the types of all needed parameters.
The following are valid top-level keys in a description dictionary: name, parents, namespace, attrs, methods, docstrings, and extra.
name: | str, the class name |
---|---|
parents: | list of strings, the immediate parents of the class (not grandparents) |
namespace: | str or None, the namespace or module the class lives in. |
attrs: | dict or dict-like, the names of the attributes (member variables) of the class mapped to their types, given in the format of the type system. |
methods: | dict or dict-like, similar to the attrs except that the keys are now function signatures and the values are the method return types. The signatures themselves are tuples. The first element of these tuples is the method name. The remaining elements (if any) are the function arguments. Arguments are themselves length-2 or -3 tuples whose first elements are the argument names, the second element is the argument type, and the third element (if present) is the default value. If the return type is None (as opposed to ‘void’), then this method is assumed to be a constructor or destructor. |
docstrings: | dict, optional, this dictionary is meant for storing documentation strings. All values are thus either strings or dictionaries of strings. Valid keys include: module, class, attrs, and methods. The attrs and methods keys are dictionaries which may include keys that mirror the top-level keys of the same name. |
extra: | dict, optional, this stores arbitrary metadata that may be used with different backends. It is not added by any auto-describe routine but may be inserted later if needed. One example use case is that the Cython generation looks for the pyx, pxd, and cpppxd keys for strings of supplemental Cython code to insert directly into the wrapper. |
Suppose we have a C++ class called Toaster that takes bread and makes delicious toast. A valid description dictionary for this class would be as follows:
desc = {
'name': 'Toaster',
'parents': ['FCComp'],
'namespace': 'bright',
'attrs': {
'n_slices': 'int32',
'rate': 'float64',
'toastiness': 'str',
},
'methods': {
('Toaster',): None,
('Toaster', ('name', 'str', '""')): None,
('Toaster', ('paramtrack', ('set', 'str')), ('name', 'str', '""')): None,
('~Toaster',): None,
('tostring',): 'str',
('calc',): 'Material',
('calc', ('incomp', ('map', 'int32', 'float64'))): 'Material',
('calc', ('mat', 'Material')): 'Material',
('write', ('filename', 'str', '"toaster.txt"')): 'void',
('write', ('filename', ('char' '*'), '"toaster.txt"')): 'void',
},
'docstrings': {
'module': "This is where Toaster lives.",
'class': "I am a toaster!",
'attrs': {
'n_slices': 'the number of slices',
'rate': 'the toast rate',
'toastiness': 'the toastiness level',
},
'methods': {
'Toaster': "Make me a toaster!",
'~Toaster': "Noooooo",
'tostring': "string representation of the toaster",
'calc': "actually makes the toast.",
'write': "persists the toaster state."
},
},
'extra': {
'pyx': 'toaster = Toaster() # make toaster singleton'
},
}
The purpose of this module is to create description dictionaries like those above by automatically parsing C++ classes. In theory this parsing step may be handled by visiting any syntax tree of C++ code. Two options were pursued here: GCC-XML and the Python bindings to the Clang AST. Unfortunately, the Clang AST bindings lack exposure for template argument types. These are needed to use any standard library containers. Thus while the Clang method was pursued to a mostly working state, the GCC-XML version is the only fully functional automatic describer for the moment.
For a Clang type located at a root node, compute the cooresponding typesystem type.
Takes a root type.
Class used to generate descriptions via GCC-XML output.
Parameters : | classname : str
root : element tree node, optional
onlyin : str, optional
verbose : bool, optional
|
---|
Resolves the context from its id and information in the element tree.
Resolves the type from its id and information in the root element tree.
Visits the class node and all sub-nodes, generating the description dictionary as it goes.
Parameters : | node : element tree node, optional
|
---|
visits a constructor, destructor, or method argument.
visits an array type and maps it to a ‘*’ refinement type.
visits a base class.
visits a class or struct.
visits a class constructor.
visits a class destructor.
visits a member variable.
visits a base C++ type, mapping it to the approriate type in the type system.
visits a member function.
visits the namespace that a node is defined in.
visits a pointer and maps it to a ‘*’ refinement type.
visits a refernece and maps it to a ‘&’ refinement type.
visits a class or struct.
visits a type definition anywhere.
Use clang to describe the class.
Finds attributes one level below the Clang node.
Find the node for a given class underneath the current node.
Finds declarations one level below the Clang node.
Returns whether a given Clang location is part of a source file range.
Get the text present on a source range.
Automatically describes a class in a file. This is the main entry point.
Parameters : | filename : str
classname : str or None, optional
parser : str, optional
verbose : bool, optional
|
---|---|
Returns : | desc : dict
|
Use GCC-XML to describe the class.
Parameters : | filename : str
classname : str or None, optional
verbose : bool, optional
|
---|---|
Returns : | desc : dict
|
Given a sequence of descriptions, in order of increasing precedence, merge them into a single description dictionary.