MCPcopy Create free account
hub / github.com/chigraph/chigraph / createNodeTypeFromCCode

Method createNodeTypeFromCCode

libchigraph/src/GraphModule.cpp:1029–1070  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1027}
1028
1029Result GraphModule::createNodeTypeFromCCode(boost::string_view code,
1030 boost::string_view functionName,
1031 std::vector<std::string> clangArgs,
1032 std::unique_ptr<NodeType>* toFill) {
1033 assert(toFill != nullptr);
1034
1035 Result res;
1036
1037 // add -I for the .c dir
1038 clangArgs.push_back("-I");
1039 clangArgs.push_back(pathToCSources().string());
1040
1041 auto mod = compileCCode(llvm::sys::fs::getMainExecutable(nullptr, nullptr).c_str(), code,
1042 clangArgs, context().llvmContext(), res);
1043
1044 if (!res) { return res; }
1045
1046 auto llFunc = mod->getFunction(functionName.to_string());
1047
1048 if (llFunc == nullptr) {
1049 res.addEntry("EUKN", "Failed to find function in C code",
1050 {{"Function Name", functionName.to_string()}, {"C Code", code.to_string()}});
1051 return res;
1052 }
1053
1054 std::vector<NamedDataType> dInputs;
1055 for (const auto& argument : llFunc->args()) {
1056 DataType ty;
1057 context().typeFromModule("lang", stringifyLLVMType(argument.getType()), &ty);
1058 dInputs.emplace_back(argument.getName(), ty);
1059 }
1060
1061 DataType output;
1062 auto ret = llFunc->getReturnType();
1063
1064 if (!ret->isVoidTy()) { context().typeFromModule("lang", stringifyLLVMType(ret), &output); }
1065
1066 *toFill = std::make_unique<CFuncNode>(*this, code.to_string(), functionName.to_string(),
1067 clangArgs, dInputs, output);
1068
1069 return res;
1070}
1071
1072} // namespace chi

Callers

nothing calls this directly

Calls 6

compileCCodeFunction · 0.85
stringifyLLVMTypeFunction · 0.85
c_strMethod · 0.80
to_stringMethod · 0.80
addEntryMethod · 0.80
typeFromModuleMethod · 0.80

Tested by

no test coverage detected