forked from RosettaCommons/binder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum.cpp
149 lines (110 loc) · 4.4 KB
/
enum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// -*- 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/enum.cpp
/// @brief Binding generation for C++ enums
/// @author Sergey Lyskov
#include <enum.hpp>
#include <type.hpp>
#include <util.hpp>
#include <fmt/format.h>
#include <clang/AST/ASTContext.h>
using namespace llvm;
using namespace clang;
using std::string;
using std::vector;
using namespace fmt::literals;
namespace binder {
/// extract include needed for this generator and add it to includes vector
void add_relevant_includes(clang::EnumDecl const *E, IncludeSet &includes, int level)
{
if( !includes.add_decl(E, level) ) return;
add_relevant_include_for_decl(E, includes);
if( auto decl = E->getDefinition() ) add_relevant_include_for_decl(decl, includes);
}
/// check if generator can create binding
bool is_bindable(EnumDecl const *E)
{
// if( E->isCXXInstanceMember() or E->isCXXClassMember() ) return false;
// else return true;
if( E->isCXXInstanceMember() ) return false;
string name = E->getNameAsString(); // getQualifiedNameAsString(); //
//if( name.rfind(')') != string::npos ) return false; // checking that this is not an "(anonymous)" enum
if( name.empty() or name.rfind(')') != string::npos ) return false; // checking that this is not an "(anonymous)" enum
return true;
}
// This function takes care about the LLVM/Clang bug which was fixed in LLVM6/Clang6.
// The body of the function is a backport from LLVM6.
std::string getQualifiedNameAsStringLLVM5Fix( NamedDecl const *E) {
std::string correct;
llvm::raw_string_ostream OS(correct);
DeclContext const *Ctx = E->getDeclContext();
SmallVector<DeclContext const *, 10> Contexts;
while (Ctx && isa<NamedDecl>(Ctx)) {
Contexts.push_back(Ctx);
Ctx = Ctx->getParent();
}
for (const DeclContext *DC : reverse(Contexts)) {
if (const auto *ED = dyn_cast<EnumDecl>(DC)) {
if ( ED->isScoped() ) {
OS<<*ED; OS<<"::";
} else continue;
} else {
OS << *cast<NamedDecl>(DC);
OS<<"::";
}
}
if ((E->getDeclName() || isa<DecompositionDecl>(E))) OS<<*E; else OS<<"(anonymous)";
return correct;
}
// Generate binding for given function: py::enum_<MyEnum>(module, "MyEnum")...
std::string bind_enum(std::string const & module, EnumDecl const *E)
{
string name { E->getNameAsString() };
string qualified_name { E->getQualifiedNameAsString() };
string maybe_arithmetic = E->isScoped() ? "" : ", pybind11::arithmetic()";
string r = "\tpybind11::enum_<{}>({}, \"{}\"{}, \"{}\")\n"_format(qualified_name, module, name, maybe_arithmetic, generate_documentation_string_for_declaration(E));
//r += "\t // is_bindable " + E->getNameAsString() + " -> " + std::to_string(is_bindable(E)) + "\n";
for(auto e = E->enumerator_begin(); e != E->enumerator_end(); ++e) {
#if (LLVM_VERSION_MAJOR > 5)
r += "\t\t.value(\"{}\", {})\n"_format(e->getNameAsString(), e->getQualifiedNameAsString());
#else
r += "\t\t.value(\"{}\", {})\n"_format(e->getNameAsString(), getQualifiedNameAsStringLLVM5Fix(*e));
#endif
}
r.pop_back();
return r + ( E->isScopedUsingClassTag() ? ";\n\n" : "\n\t\t.export_values();\n\n" );
}
/// Generate string id that uniquly identify C++ binding object. For functions this is function prototype and for classes forward declaration.
string EnumBinder::id() const
{
return E->getQualifiedNameAsString();
}
/// check if generator can create binding
bool EnumBinder::bindable() const
{
return is_bindable(E);
}
/// check if user requested binding for the given declaration
void EnumBinder::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 EnumBinder::add_relevant_includes(IncludeSet &includes) const
{
binder::add_relevant_includes(E, includes, 0);
}
/// generate binding code for this object and all its dependencies
void EnumBinder::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_enum(module_variable_name, E) + ";\n\n";
}
} // namespace binder