99#ifndef XEUS_CPP_INSPECT_HPP
1010#define XEUS_CPP_INSPECT_HPP
1111
12+ #include < filesystem>
1213#include < fstream>
1314#include < string>
1415
15-
1616#include < pugixml.hpp>
1717
1818#include < xtl/xsystem.hpp>
2323#include " xdemangle.hpp"
2424#include " xparser.hpp"
2525
26- #include " llvm/Support/FileSystem.h"
27- #include " llvm/Support/Path.h"
26+ // #include "llvm/Support/FileSystem.h"
27+ // #include "llvm/Support/Path.h"
28+
29+ // #include "clang/Interpreter/CppInterOp.h"
30+
2831
2932namespace xcpp
3033{
@@ -81,27 +84,57 @@ namespace xcpp
8184 }
8285 };
8386
84- std::string find_type (const std::string& expression, clang::Interpreter& interpreter)
87+
88+ std::string find_type_slow (const std::string& expression) {
89+ static unsigned long long var_count = 0 ;
90+
91+ if (auto type = Cpp::GetType (expression))
92+ return Cpp::GetQualifiedName (type);
93+
94+ // Here we might need to deal with integral types such as 3.14.
95+
96+ std::string id = " __Xeus_GetType_" + std::to_string (var_count++);
97+ std::string using_clause = " using " + id + " = __typeof__(" + expression + " );\n " ;
98+
99+ if (!Cpp::Declare (using_clause.c_str (), /* silent=*/ false )) {
100+ Cpp::TCppScope_t lookup = Cpp::GetNamed (id, 0 );
101+ Cpp::TCppType_t lookup_ty = Cpp::GetTypeFromScope (lookup);
102+ return Cpp::GetQualifiedCompleteName (Cpp::GetCanonicalType (lookup_ty));
103+ }
104+ return " " ;
105+ }
106+ /*
107+ std::string find_type(const std::string& expression)
85108 {
86109 auto PTU = interpreter.Parse(expression + ";");
87110 if (llvm::Error Err = PTU.takeError()) {
88111 llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
89112 return "";
90113 }
91114
92- clang::Decl *D = *PTU->TUPart ->decls_begin ();
93- if (!llvm::isa<clang::TopLevelStmtDecl>(D))
94- return " " ;
115+ clang::Decl *D = *PTU->TUPart->decls_begin();
116+ if (!llvm::isa<clang::TopLevelStmtDecl>(D))
117+ return "";
95118
96- clang::Expr *E = llvm::cast<clang::Expr>(llvm::cast<clang::TopLevelStmtDecl>(D)->getStmt ());
119+ clang::Expr *E = llvm::cast<clang::Expr>(llvm::cast<clang::TopLevelStmtDecl>(D)->getStmt());
97120
98- clang::QualType QT = E->getType ();
121+ clang::QualType QT = E->getType();
99122 return QT.getAsString();
100123 }
101-
124+ */
102125 static nl::json read_tagconfs (const char * path)
103126 {
104127 nl::json result = nl::json::array ();
128+ for (auto &entry: std::filesystem::directory_iterator (path)) {
129+ if (entry.path ().extension () != " .json" )
130+ continue ;
131+ std::ifstream i (entry.path ());
132+ nl::json json_entry;
133+ i >> json_entry;
134+ result.emplace_back (std::move (json_entry));
135+ }
136+ return result;
137+ /*
105138 std::error_code EC;
106139 for (llvm::sys::fs::directory_iterator File(path, EC), FileEnd;
107140 File != FileEnd && !EC; File.increment(EC)) {
@@ -115,19 +148,19 @@ namespace xcpp
115148 result.emplace_back(std::move(entry));
116149 }
117150 return result;
151+ */
118152 }
119153
120- std::pair<bool , std::smatch> is_inspect_request (const std::string code, std::regex re)
154+ std::pair<bool , std::smatch> is_inspect_request (const std::string code, std::regex re)
121155 {
122156 std::smatch inspect;
123157 if (std::regex_search (code, inspect, re)){
124158 return std::make_pair (true , inspect);
125159 }
126160 return std::make_pair (false , inspect);
127-
128161 }
129162
130- void inspect (const std::string& code, nl::json& kernel_res, clang::Interpreter& interpreter )
163+ void inspect (const std::string& code, nl::json& kernel_res)
131164 {
132165 std::string tagconf_dir = XCPP_TAGCONFS_DIR;
133166 std::string tagfiles_dir = XCPP_TAGFILES_DIR;
@@ -150,7 +183,7 @@ namespace xcpp
150183 // Method or variable of class found (xxxx.yyyy)
151184 if (std::regex_search (to_inspect, method, std::regex (R"( (.*)\.(\w*)$)" )))
152185 {
153- std::string typename_ = find_type (method[1 ], interpreter );
186+ std::string typename_ = find_type_slow (method[1 ]);
154187
155188 if (!typename_.empty ())
156189 {
@@ -184,7 +217,7 @@ namespace xcpp
184217 }
185218 else
186219 {
187- std::string typename_ = find_type (to_inspect, interpreter );
220+ std::string typename_ = find_type_slow (to_inspect);
188221 find_string = (typename_.empty ()) ? to_inspect : typename_;
189222 }
190223
0 commit comments