1313#include < fstream>
1414#include < string>
1515
16-
1716#include < pugixml.hpp>
1817
1918#include < xtl/xsystem.hpp>
2423#include " xdemangle.hpp"
2524#include " xparser.hpp"
2625
26+ #include " clang/Interpreter/CppInterOp.h"
27+
2728namespace xcpp
2829{
2930 struct node_predicate
@@ -79,22 +80,23 @@ namespace xcpp
7980 }
8081 };
8182
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);
8988
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.
9390
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 " ;
9593
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 " " ;
98100 }
99101
100102 static nl::json read_tagconfs (const char * path)
@@ -111,17 +113,16 @@ namespace xcpp
111113 return result;
112114 }
113115
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)
115118 {
116119 std::smatch inspect;
117- if (std::regex_search (code, inspect, re)){
120+ if (std::regex_search (code, inspect, re))
118121 return std::make_pair (true , inspect);
119- }
120122 return std::make_pair (false , inspect);
121-
122123 }
123124
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)
125126 {
126127 std::string tagconf_dir = XCPP_TAGCONFS_DIR;
127128 std::string tagfiles_dir = XCPP_TAGFILES_DIR;
@@ -144,18 +145,18 @@ namespace xcpp
144145 // Method or variable of class found (xxxx.yyyy)
145146 if (std::regex_search (to_inspect, method, std::regex (R"( (.*)\.(\w*)$)" )))
146147 {
147- std::string typename_ = find_type (method[1 ], interpreter );
148+ std::string type_name = find_type_slow (method[1 ]);
148149
149- if (!typename_ .empty ())
150+ if (!type_name .empty ())
150151 {
151152 for (nl::json::const_iterator it = tagconfs.cbegin (); it != tagconfs.cend (); ++it)
152153 {
153154 url = it->at (" url" );
154155 tagfile = it->at (" tagfile" );
155156 std::string filename = tagfiles_dir + " /" + tagfile;
156157 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 ]};
159160 auto node = doc.find_node (predicate);
160161 if (!node.empty ())
161162 {
@@ -178,8 +179,8 @@ namespace xcpp
178179 }
179180 else
180181 {
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 ;
183184 }
184185
185186 for (nl::json::const_iterator it = tagconfs.cbegin (); it != tagconfs.cend (); ++it)
@@ -188,7 +189,7 @@ namespace xcpp
188189 tagfile = it->at (" tagfile" );
189190 std::string filename = tagfiles_dir + " /" + tagfile;
190191 pugi::xml_document doc;
191- doc.load_file (filename.c_str ());
192+ pugi::xml_parse_result result = doc.load_file (filename.c_str ());
192193 for (auto c : check)
193194 {
194195 node_predicate predicate{c, find_string};
@@ -266,8 +267,7 @@ namespace xcpp
266267 using xpreamble::pattern;
267268 const std::string spattern = R"( ^\?)" ;
268269
269- xintrospection (clang::Interpreter& p)
270- : m_interpreter{p}
270+ xintrospection ()
271271 {
272272 pattern = spattern;
273273 }
@@ -277,17 +277,15 @@ namespace xcpp
277277 std::regex re (spattern + R"( (.*))" );
278278 std::smatch to_inspect;
279279 std::regex_search (code, to_inspect, re);
280- inspect (to_inspect[1 ], kernel_res, m_interpreter );
280+ inspect (to_inspect[1 ], kernel_res);
281281 }
282282
283283 virtual xpreamble* clone () const override
284284 {
285285 return new xintrospection (*this );
286286 }
287-
288- private:
289-
290- clang::Interpreter& m_interpreter;
291287 };
288+
292289}
293- #endif
290+
291+ #endif // XEUS_CPP_INSPECT_HPP
0 commit comments