13
13
#include < fstream>
14
14
#include < string>
15
15
16
-
17
16
#include < pugixml.hpp>
18
17
19
18
#include < xtl/xsystem.hpp>
24
23
#include " xdemangle.hpp"
25
24
#include " xparser.hpp"
26
25
26
+ #include " clang/Interpreter/CppInterOp.h"
27
+
27
28
namespace xcpp
28
29
{
29
30
struct node_predicate
@@ -79,22 +80,23 @@ namespace xcpp
79
80
}
80
81
};
81
82
82
- std::string find_type (const std::string& expression, clang::Interpreter& interpreter)
83
- {
84
- auto PTU = interpreter.Parse (expression + " ;" );
85
- if (llvm::Error Err = PTU.takeError ()) {
86
- llvm::logAllUnhandledErrors (std::move (Err), llvm::errs (), " error: " );
87
- return " " ;
88
- }
83
+ std::string find_type_slow (const std::string& expression) {
84
+ static unsigned long long var_count = 0 ;
85
+
86
+ if (auto *type = Cpp::GetType (expression))
87
+ return Cpp::GetQualifiedName (type);
89
88
90
- clang::Decl *D = *PTU->TUPart ->decls_begin ();
91
- if (!llvm::isa<clang::TopLevelStmtDecl>(D))
92
- return " " ;
89
+ // Here we might need to deal with integral types such as 3.14.
93
90
94
- clang::Expr *E = llvm::cast<clang::Expr>(llvm::cast<clang::TopLevelStmtDecl>(D)->getStmt ());
91
+ std::string id = " __Xeus_GetType_" + std::to_string (var_count++);
92
+ std::string using_clause = " using " + id + " = __typeof__(" + expression + " );\n " ;
95
93
96
- clang::QualType QT = E->getType ();
97
- return QT.getAsString ();
94
+ if (!Cpp::Declare (using_clause.c_str (), /* silent=*/ false )) {
95
+ Cpp::TCppScope_t lookup = Cpp::GetNamed (id, nullptr );
96
+ Cpp::TCppType_t lookup_ty = Cpp::GetTypeFromScope (lookup);
97
+ return Cpp::GetQualifiedCompleteName (Cpp::GetCanonicalType (lookup_ty));
98
+ }
99
+ return " " ;
98
100
}
99
101
100
102
static nl::json read_tagconfs (const char * path)
@@ -111,17 +113,16 @@ namespace xcpp
111
113
return result;
112
114
}
113
115
114
- std::pair<bool , std::smatch> is_inspect_request (const std::string code, std::regex re)
116
+ std::pair<bool , std::smatch> is_inspect_request (const std::string& code,
117
+ const std::regex& re)
115
118
{
116
119
std::smatch inspect;
117
- if (std::regex_search (code, inspect, re)){
120
+ if (std::regex_search (code, inspect, re))
118
121
return std::make_pair (true , inspect);
119
- }
120
122
return std::make_pair (false , inspect);
121
-
122
123
}
123
124
124
- void inspect (const std::string& code, nl::json& kernel_res, clang::Interpreter& interpreter )
125
+ void inspect (const std::string& code, nl::json& kernel_res)
125
126
{
126
127
std::string tagconf_dir = XCPP_TAGCONFS_DIR;
127
128
std::string tagfiles_dir = XCPP_TAGFILES_DIR;
@@ -144,18 +145,18 @@ namespace xcpp
144
145
// Method or variable of class found (xxxx.yyyy)
145
146
if (std::regex_search (to_inspect, method, std::regex (R"( (.*)\.(\w*)$)" )))
146
147
{
147
- std::string typename_ = find_type (method[1 ], interpreter );
148
+ std::string type_name = find_type_slow (method[1 ]);
148
149
149
- if (!typename_ .empty ())
150
+ if (!type_name .empty ())
150
151
{
151
152
for (nl::json::const_iterator it = tagconfs.cbegin (); it != tagconfs.cend (); ++it)
152
153
{
153
154
url = it->at (" url" );
154
155
tagfile = it->at (" tagfile" );
155
156
std::string filename = tagfiles_dir + " /" + tagfile;
156
157
pugi::xml_document doc;
157
- doc.load_file (filename.c_str ());
158
- class_member_predicate predicate{typename_ , " function" , method[2 ]};
158
+ pugi::xml_parse_result result = doc.load_file (filename.c_str ());
159
+ class_member_predicate predicate{type_name , " function" , method[2 ]};
159
160
auto node = doc.find_node (predicate);
160
161
if (!node.empty ())
161
162
{
@@ -178,8 +179,8 @@ namespace xcpp
178
179
}
179
180
else
180
181
{
181
- std::string typename_ = find_type (to_inspect, interpreter );
182
- find_string = (typename_ .empty ()) ? to_inspect : typename_ ;
182
+ std::string type_name = find_type_slow (to_inspect);
183
+ find_string = (type_name .empty ()) ? to_inspect : type_name ;
183
184
}
184
185
185
186
for (nl::json::const_iterator it = tagconfs.cbegin (); it != tagconfs.cend (); ++it)
@@ -188,7 +189,7 @@ namespace xcpp
188
189
tagfile = it->at (" tagfile" );
189
190
std::string filename = tagfiles_dir + " /" + tagfile;
190
191
pugi::xml_document doc;
191
- doc.load_file (filename.c_str ());
192
+ pugi::xml_parse_result result = doc.load_file (filename.c_str ());
192
193
for (auto c : check)
193
194
{
194
195
node_predicate predicate{c, find_string};
@@ -266,8 +267,7 @@ namespace xcpp
266
267
using xpreamble::pattern;
267
268
const std::string spattern = R"( ^\?)" ;
268
269
269
- xintrospection (clang::Interpreter& p)
270
- : m_interpreter{p}
270
+ xintrospection ()
271
271
{
272
272
pattern = spattern;
273
273
}
@@ -277,17 +277,15 @@ namespace xcpp
277
277
std::regex re (spattern + R"( (.*))" );
278
278
std::smatch to_inspect;
279
279
std::regex_search (code, to_inspect, re);
280
- inspect (to_inspect[1 ], kernel_res, m_interpreter );
280
+ inspect (to_inspect[1 ], kernel_res);
281
281
}
282
282
283
283
virtual xpreamble* clone () const override
284
284
{
285
285
return new xintrospection (*this );
286
286
}
287
-
288
- private:
289
-
290
- clang::Interpreter& m_interpreter;
291
287
};
288
+
292
289
}
293
- #endif
290
+
291
+ #endif // XEUS_CPP_INSPECT_HPP
0 commit comments