-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First attempt to bind constants #155
base: master
Are you sure you want to change the base?
Changes from all commits
f6ede53
e1828c7
b49b5fd
38b4899
a2d154e
cb83ff1
d2361cd
008d334
8bb5333
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,9 @@ add_clang_executable(binder | |
enum.hpp | ||
enum.cpp | ||
|
||
const.hpp | ||
const.cpp | ||
|
||
function.hpp | ||
function.cpp | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*- | ||
// vi: set ts=2 noet: | ||
// | ||
// Copyright (c) 2021 Sergey Lyskov <[email protected]> | ||
// | ||
// All rights reserved. Use of this source code is governed by a | ||
// MIT license that can be found in the LICENSE file. | ||
|
||
/// @file binder/const.cpp | ||
/// @brief Binding generation for C++ constants | ||
/// @author Sergey Lyskov, Andrii Verbytskyi | ||
|
||
|
||
#include <const.hpp> | ||
|
||
#include <type.hpp> | ||
#include <util.hpp> | ||
|
||
#include <fmt/format.h> | ||
|
||
#include <clang/AST/ASTContext.h> | ||
|
||
using namespace llvm; | ||
using namespace clang; | ||
#include <iostream> | ||
using std::string; | ||
using std::vector; | ||
|
||
using namespace fmt::literals; | ||
|
||
namespace binder { | ||
|
||
/// check if generator can create binding | ||
bool is_bindable(VarDecl const *E) | ||
{ | ||
if ( !E->getType().isConstQualified() ) return false; | ||
if ( !E->hasInit() ) return false; | ||
if ( E->getType().getTypePtr()->isArrayType()) return false; | ||
if ( E->isCXXInstanceMember() or E->isCXXClassMember() ) return false; | ||
if ( E->isCXXInstanceMember() ) return false; | ||
if ( standard_name( E->getType().getCanonicalType().getAsString() ) == "const std::string" ) return true; | ||
if ( E->getType().getTypePtr()->isRealFloatingType() ) return true; | ||
if ( E->getType().getTypePtr()->isIntegerType() ) return true; | ||
if ( E->getType().getTypePtr()->isBooleanType() ) return true; | ||
return false; | ||
} | ||
|
||
// Generate binding for given function: py::enum_<MyEnum>(module, "MyEnum")... | ||
std::string bind_const(std::string const & module, VarDecl const *E) | ||
{ | ||
string r="\t"; | ||
clang::Expr const* init = E->getInit(); | ||
if ( init ){ | ||
string name { E->getNameAsString() }; | ||
std::string type = E->getType().getCanonicalType().getAsString(); | ||
std::string pytype = ""; | ||
bool pytype_set = false; | ||
//This is a list of types that can be binded with pybind, see https://pybind11.readthedocs.io/en/stable/advanced/pycpp/object.html | ||
if ( !pytype_set and standard_name( type ) == "const std::string" ) { pytype_set = true; pytype = "str";} | ||
if ( !pytype_set and E->getType().getTypePtr()->isRealFloatingType() ) { pytype_set = true; pytype = "float_"; } | ||
if ( !pytype_set and E->getType().getTypePtr()->isIntegerType() ) { pytype_set = true; pytype = "int_"; } | ||
if ( !pytype_set and E->getType().getTypePtr()->isBooleanType() ) { pytype_set = true; pytype = "bool_"; } | ||
if ( pytype_set ) { | ||
std::string rhs; | ||
llvm::raw_string_ostream rso(rhs); | ||
clang::LangOptions lang_opts; | ||
lang_opts.CPlusPlus = true; | ||
clang::PrintingPolicy Policy(lang_opts); | ||
init->printPretty(rso, 0, Policy); | ||
r = "\t{}.attr(\"{}\") = pybind11::{}({})\n"_format( module, name, pytype, rhs); | ||
} | ||
} | ||
r.pop_back(); | ||
return r; | ||
} | ||
|
||
|
||
/// Generate string id that uniquly identify C++ binding object. For functions this is function prototype and for classes forward declaration. | ||
string ConstBinder::id() const | ||
{ | ||
return E->getQualifiedNameAsString(); | ||
} | ||
|
||
|
||
/// check if generator can create binding | ||
bool ConstBinder::bindable() const | ||
{ | ||
return is_bindable(E); | ||
} | ||
|
||
/// check if user requested binding for the given declaration | ||
void ConstBinder::request_bindings_and_skipping(Config const &config) | ||
{ | ||
if( config.is_namespace_binding_requested( namespace_from_named_decl(E) ) ) Binder::request_bindings(); | ||
} | ||
|
||
|
||
/// extract include needed for this generator and add it to includes vector | ||
void ConstBinder::add_relevant_includes(IncludeSet &includes) const | ||
{ | ||
} | ||
|
||
/// generate binding code for this object and all its dependencies | ||
void ConstBinder::bind(Context &context) | ||
{ | ||
if( is_binded() ) return; | ||
|
||
string const module_variable_name = context.module_variable_name( namespace_from_named_decl(E) ); | ||
|
||
code() = "\t" + generate_comment_for_declaration(E); | ||
code() += bind_const(module_variable_name, E) + ";\n\n"; | ||
} | ||
|
||
} // namespace binder |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*- | ||
// vi: set ts=2 noet: | ||
// | ||
// Copyright (c) 2021 Sergey Lyskov <[email protected]> | ||
// | ||
// All rights reserved. Use of this source code is governed by a | ||
// MIT license that can be found in the LICENSE file. | ||
|
||
/// @file binder/const.hpp | ||
/// @brief Binding generation for C++ constant expressions | ||
/// @author Sergey Lyskov, Andrii Verbytskyi | ||
|
||
|
||
#ifndef _INCLUDED_const_hpp_ | ||
#define _INCLUDED_const_hpp_ | ||
|
||
#include <context.hpp> | ||
|
||
#include <clang/AST/Decl.h> | ||
|
||
#include <string> | ||
|
||
namespace binder { | ||
|
||
/// check if generator can create binding | ||
bool is_bindable(clang::VarDecl const *E); | ||
|
||
|
||
// Generate binding for given function | ||
std::string bind_const(std::string const & module, clang::VarDecl const *E); | ||
|
||
|
||
class ConstBinder : public Binder | ||
{ | ||
public: | ||
ConstBinder(clang::VarDecl const *e) : E(e) {} | ||
|
||
/// Generate string id that uniquly identify C++ binding object. For functions this is function prototype and for classes forward declaration. | ||
string id() const override; | ||
// return Clang AST NamedDecl pointer to original declaration used to create this Binder | ||
clang::NamedDecl const * named_decl() const override { return E; }; | ||
|
||
/// check if generator can create binding | ||
bool bindable() const override; | ||
|
||
/// check if user requested binding for the given declaration | ||
virtual void request_bindings_and_skipping(Config const &) override; | ||
|
||
/// extract include needed for this generator and add it to includes vector | ||
void add_relevant_includes(IncludeSet &includes) const override; | ||
|
||
/// generate binding code for this object and all its dependencies | ||
void bind(Context &) override; | ||
|
||
|
||
private: | ||
clang::VarDecl const *E; | ||
}; | ||
|
||
|
||
} // namespace binder | ||
|
||
#endif // _INCLUDED_const_hpp_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*- | ||
// vi: set ts=2 noet: | ||
// | ||
// Copyright (c) 2016 Sergey Lyskov <[email protected]> | ||
// | ||
// All rights reserved. Use of this source code is governed by a | ||
// MIT license that can be found in the LICENSE file. | ||
|
||
/// @file binder/test/T00.basic.hpp | ||
/// @brief Binder self-test file. Basic functionality. | ||
/// @author Sergey Lyskov | ||
|
||
#ifndef _INCLUDED_T60_basic_hpp_ | ||
#define _INCLUDED_T60_basic_hpp_ | ||
#include <string> | ||
#include <vector> | ||
|
||
int const global_int = 1; | ||
long const global_long = 2; | ||
|
||
unsigned int const global_unsigned_int = 3; | ||
unsigned long const global_unsigned_long = 4; | ||
|
||
float const global_float = 5.0; | ||
double const global_double = 6.0; | ||
|
||
std::string const global_string1 = "Some test string"; | ||
|
||
std::string const global_string2 = std::string("Some test string"); | ||
|
||
|
||
double const expression_global_double = 8.0 + 1.0/5.0; | ||
|
||
double const array_global_double_not_binded[5] = {1.0, 2.0, 3.0, 4.0, 5.0}; //This should not appear in bindings so far. | ||
|
||
std::vector<double> const vector_global_double_not_binded{1.0, 2.0, 3.0, 4.0, 5.0}; //This should not appear in bindings so far. | ||
|
||
int foo_char(char *) { return 0; } //This is just for control. | ||
|
||
namespace foo | ||
{ | ||
double const foonamespaced_global_double = 7.0; | ||
|
||
int foonamespaced_foo_char(char *) { return 0; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please elaborate why do we need this function here, what do we test with it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a test again. |
||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add some user defined data types here? If they can not be bound that totally fine but i want to make sure that Binder is always generate compilable code. Thanks, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constants can be bound using the special types like "float_", so I custom types cannot use it, as far as I understand. |
||
|
||
#endif // _INCLUDED_T60_basic_hpp_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this function is empty, was this just an oversight or we can not deduce includes for some reason? Can we just do
binder::add_relevant_includes(E, includes, 0);
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding was that once only the standard types are used, no extra includes are needed.
And the std::string is included anyway.