mirror of https://github.com/tstack/lnav
[prql] use cxx for rust ffi stuff
parent
1025e466a2
commit
2cb227e14f
@ -0,0 +1,4 @@
|
||||
|
||||
PRQL_FILES = \
|
||||
$(srcdir)/%reldir%/stats.prql \
|
||||
$()
|
@ -1,5 +1,3 @@
|
||||
let json_each = func input -> s"SELECT * FROM json_each({input})"
|
||||
|
||||
let count_by = func column rel <relation> -> <relation> (
|
||||
rel
|
||||
group {column} (aggregate {total = count this})
|
@ -0,0 +1,891 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
#include <new>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace rust {
|
||||
inline namespace cxxbridge1 {
|
||||
// #include "rust/cxx.h"
|
||||
|
||||
#ifndef CXXBRIDGE1_PANIC
|
||||
#define CXXBRIDGE1_PANIC
|
||||
template <typename Exception>
|
||||
void panic [[noreturn]] (const char *msg);
|
||||
#endif // CXXBRIDGE1_PANIC
|
||||
|
||||
struct unsafe_bitcopy_t;
|
||||
|
||||
namespace {
|
||||
template <typename T>
|
||||
class impl;
|
||||
} // namespace
|
||||
|
||||
class Opaque;
|
||||
|
||||
template <typename T>
|
||||
::std::size_t size_of();
|
||||
template <typename T>
|
||||
::std::size_t align_of();
|
||||
|
||||
#ifndef CXXBRIDGE1_RUST_STRING
|
||||
#define CXXBRIDGE1_RUST_STRING
|
||||
class String final {
|
||||
public:
|
||||
String() noexcept;
|
||||
String(const String &) noexcept;
|
||||
String(String &&) noexcept;
|
||||
~String() noexcept;
|
||||
|
||||
String(const std::string &);
|
||||
String(const char *);
|
||||
String(const char *, std::size_t);
|
||||
String(const char16_t *);
|
||||
String(const char16_t *, std::size_t);
|
||||
|
||||
static String lossy(const std::string &) noexcept;
|
||||
static String lossy(const char *) noexcept;
|
||||
static String lossy(const char *, std::size_t) noexcept;
|
||||
static String lossy(const char16_t *) noexcept;
|
||||
static String lossy(const char16_t *, std::size_t) noexcept;
|
||||
|
||||
String &operator=(const String &) &noexcept;
|
||||
String &operator=(String &&) &noexcept;
|
||||
|
||||
explicit operator std::string() const;
|
||||
|
||||
const char *data() const noexcept;
|
||||
std::size_t size() const noexcept;
|
||||
std::size_t length() const noexcept;
|
||||
bool empty() const noexcept;
|
||||
|
||||
const char *c_str() noexcept;
|
||||
|
||||
std::size_t capacity() const noexcept;
|
||||
void reserve(size_t new_cap) noexcept;
|
||||
|
||||
using iterator = char *;
|
||||
iterator begin() noexcept;
|
||||
iterator end() noexcept;
|
||||
|
||||
using const_iterator = const char *;
|
||||
const_iterator begin() const noexcept;
|
||||
const_iterator end() const noexcept;
|
||||
const_iterator cbegin() const noexcept;
|
||||
const_iterator cend() const noexcept;
|
||||
|
||||
bool operator==(const String &) const noexcept;
|
||||
bool operator!=(const String &) const noexcept;
|
||||
bool operator<(const String &) const noexcept;
|
||||
bool operator<=(const String &) const noexcept;
|
||||
bool operator>(const String &) const noexcept;
|
||||
bool operator>=(const String &) const noexcept;
|
||||
|
||||
void swap(String &) noexcept;
|
||||
|
||||
String(unsafe_bitcopy_t, const String &) noexcept;
|
||||
|
||||
private:
|
||||
struct lossy_t;
|
||||
String(lossy_t, const char *, std::size_t) noexcept;
|
||||
String(lossy_t, const char16_t *, std::size_t) noexcept;
|
||||
friend void swap(String &lhs, String &rhs) noexcept { lhs.swap(rhs); }
|
||||
|
||||
std::array<std::uintptr_t, 3> repr;
|
||||
};
|
||||
#endif // CXXBRIDGE1_RUST_STRING
|
||||
|
||||
#ifndef CXXBRIDGE1_RUST_SLICE
|
||||
#define CXXBRIDGE1_RUST_SLICE
|
||||
namespace detail {
|
||||
template <bool>
|
||||
struct copy_assignable_if {};
|
||||
|
||||
template <>
|
||||
struct copy_assignable_if<false> {
|
||||
copy_assignable_if() noexcept = default;
|
||||
copy_assignable_if(const copy_assignable_if &) noexcept = default;
|
||||
copy_assignable_if &operator=(const copy_assignable_if &) &noexcept = delete;
|
||||
copy_assignable_if &operator=(copy_assignable_if &&) &noexcept = default;
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
template <typename T>
|
||||
class Slice final
|
||||
: private detail::copy_assignable_if<std::is_const<T>::value> {
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
Slice() noexcept;
|
||||
Slice(T *, std::size_t count) noexcept;
|
||||
|
||||
Slice &operator=(const Slice<T> &) &noexcept = default;
|
||||
Slice &operator=(Slice<T> &&) &noexcept = default;
|
||||
|
||||
T *data() const noexcept;
|
||||
std::size_t size() const noexcept;
|
||||
std::size_t length() const noexcept;
|
||||
bool empty() const noexcept;
|
||||
|
||||
T &operator[](std::size_t n) const noexcept;
|
||||
T &at(std::size_t n) const;
|
||||
T &front() const noexcept;
|
||||
T &back() const noexcept;
|
||||
|
||||
Slice(const Slice<T> &) noexcept = default;
|
||||
~Slice() noexcept = default;
|
||||
|
||||
class iterator;
|
||||
iterator begin() const noexcept;
|
||||
iterator end() const noexcept;
|
||||
|
||||
void swap(Slice &) noexcept;
|
||||
|
||||
private:
|
||||
class uninit;
|
||||
Slice(uninit) noexcept;
|
||||
friend impl<Slice>;
|
||||
friend void sliceInit(void *, const void *, std::size_t) noexcept;
|
||||
friend void *slicePtr(const void *) noexcept;
|
||||
friend std::size_t sliceLen(const void *) noexcept;
|
||||
|
||||
std::array<std::uintptr_t, 2> repr;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class Slice<T>::iterator final {
|
||||
public:
|
||||
using iterator_category = std::random_access_iterator_tag;
|
||||
using value_type = T;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = typename std::add_pointer<T>::type;
|
||||
using reference = typename std::add_lvalue_reference<T>::type;
|
||||
|
||||
reference operator*() const noexcept;
|
||||
pointer operator->() const noexcept;
|
||||
reference operator[](difference_type) const noexcept;
|
||||
|
||||
iterator &operator++() noexcept;
|
||||
iterator operator++(int) noexcept;
|
||||
iterator &operator--() noexcept;
|
||||
iterator operator--(int) noexcept;
|
||||
|
||||
iterator &operator+=(difference_type) noexcept;
|
||||
iterator &operator-=(difference_type) noexcept;
|
||||
iterator operator+(difference_type) const noexcept;
|
||||
iterator operator-(difference_type) const noexcept;
|
||||
difference_type operator-(const iterator &) const noexcept;
|
||||
|
||||
bool operator==(const iterator &) const noexcept;
|
||||
bool operator!=(const iterator &) const noexcept;
|
||||
bool operator<(const iterator &) const noexcept;
|
||||
bool operator<=(const iterator &) const noexcept;
|
||||
bool operator>(const iterator &) const noexcept;
|
||||
bool operator>=(const iterator &) const noexcept;
|
||||
|
||||
private:
|
||||
friend class Slice;
|
||||
void *pos;
|
||||
std::size_t stride;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
Slice<T>::Slice() noexcept {
|
||||
sliceInit(this, reinterpret_cast<void *>(align_of<T>()), 0);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Slice<T>::Slice(T *s, std::size_t count) noexcept {
|
||||
assert(s != nullptr || count == 0);
|
||||
sliceInit(this,
|
||||
s == nullptr && count == 0
|
||||
? reinterpret_cast<void *>(align_of<T>())
|
||||
: const_cast<typename std::remove_const<T>::type *>(s),
|
||||
count);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T *Slice<T>::data() const noexcept {
|
||||
return reinterpret_cast<T *>(slicePtr(this));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::size_t Slice<T>::size() const noexcept {
|
||||
return sliceLen(this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::size_t Slice<T>::length() const noexcept {
|
||||
return this->size();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::empty() const noexcept {
|
||||
return this->size() == 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Slice<T>::operator[](std::size_t n) const noexcept {
|
||||
assert(n < this->size());
|
||||
auto ptr = static_cast<char *>(slicePtr(this)) + size_of<T>() * n;
|
||||
return *reinterpret_cast<T *>(ptr);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Slice<T>::at(std::size_t n) const {
|
||||
if (n >= this->size()) {
|
||||
panic<std::out_of_range>("rust::Slice index out of range");
|
||||
}
|
||||
return (*this)[n];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Slice<T>::front() const noexcept {
|
||||
assert(!this->empty());
|
||||
return (*this)[0];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Slice<T>::back() const noexcept {
|
||||
assert(!this->empty());
|
||||
return (*this)[this->size() - 1];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator::reference
|
||||
Slice<T>::iterator::operator*() const noexcept {
|
||||
return *static_cast<T *>(this->pos);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator::pointer
|
||||
Slice<T>::iterator::operator->() const noexcept {
|
||||
return static_cast<T *>(this->pos);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator::reference Slice<T>::iterator::operator[](
|
||||
typename Slice<T>::iterator::difference_type n) const noexcept {
|
||||
auto ptr = static_cast<char *>(this->pos) + this->stride * n;
|
||||
return *reinterpret_cast<T *>(ptr);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
|
||||
this->pos = static_cast<char *>(this->pos) + this->stride;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
|
||||
auto ret = iterator(*this);
|
||||
this->pos = static_cast<char *>(this->pos) + this->stride;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator &Slice<T>::iterator::operator--() noexcept {
|
||||
this->pos = static_cast<char *>(this->pos) - this->stride;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::iterator::operator--(int) noexcept {
|
||||
auto ret = iterator(*this);
|
||||
this->pos = static_cast<char *>(this->pos) - this->stride;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator &Slice<T>::iterator::operator+=(
|
||||
typename Slice<T>::iterator::difference_type n) noexcept {
|
||||
this->pos = static_cast<char *>(this->pos) + this->stride * n;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator &Slice<T>::iterator::operator-=(
|
||||
typename Slice<T>::iterator::difference_type n) noexcept {
|
||||
this->pos = static_cast<char *>(this->pos) - this->stride * n;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::iterator::operator+(
|
||||
typename Slice<T>::iterator::difference_type n) const noexcept {
|
||||
auto ret = iterator(*this);
|
||||
ret.pos = static_cast<char *>(this->pos) + this->stride * n;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::iterator::operator-(
|
||||
typename Slice<T>::iterator::difference_type n) const noexcept {
|
||||
auto ret = iterator(*this);
|
||||
ret.pos = static_cast<char *>(this->pos) - this->stride * n;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator::difference_type
|
||||
Slice<T>::iterator::operator-(const iterator &other) const noexcept {
|
||||
auto diff = std::distance(static_cast<char *>(other.pos),
|
||||
static_cast<char *>(this->pos));
|
||||
return diff / static_cast<typename Slice<T>::iterator::difference_type>(
|
||||
this->stride);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
|
||||
return this->pos == other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
|
||||
return this->pos != other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator<(const iterator &other) const noexcept {
|
||||
return this->pos < other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator<=(const iterator &other) const noexcept {
|
||||
return this->pos <= other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator>(const iterator &other) const noexcept {
|
||||
return this->pos > other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator>=(const iterator &other) const noexcept {
|
||||
return this->pos >= other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::begin() const noexcept {
|
||||
iterator it;
|
||||
it.pos = slicePtr(this);
|
||||
it.stride = size_of<T>();
|
||||
return it;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::end() const noexcept {
|
||||
iterator it = this->begin();
|
||||
it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
|
||||
return it;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Slice<T>::swap(Slice &rhs) noexcept {
|
||||
std::swap(*this, rhs);
|
||||
}
|
||||
#endif // CXXBRIDGE1_RUST_SLICE
|
||||
|
||||
#ifndef CXXBRIDGE1_RUST_BITCOPY_T
|
||||
#define CXXBRIDGE1_RUST_BITCOPY_T
|
||||
struct unsafe_bitcopy_t final {
|
||||
explicit unsafe_bitcopy_t() = default;
|
||||
};
|
||||
#endif // CXXBRIDGE1_RUST_BITCOPY_T
|
||||
|
||||
#ifndef CXXBRIDGE1_RUST_VEC
|
||||
#define CXXBRIDGE1_RUST_VEC
|
||||
template <typename T>
|
||||
class Vec final {
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
Vec() noexcept;
|
||||
Vec(std::initializer_list<T>);
|
||||
Vec(const Vec &);
|
||||
Vec(Vec &&) noexcept;
|
||||
~Vec() noexcept;
|
||||
|
||||
Vec &operator=(Vec &&) &noexcept;
|
||||
Vec &operator=(const Vec &) &;
|
||||
|
||||
std::size_t size() const noexcept;
|
||||
bool empty() const noexcept;
|
||||
const T *data() const noexcept;
|
||||
T *data() noexcept;
|
||||
std::size_t capacity() const noexcept;
|
||||
|
||||
const T &operator[](std::size_t n) const noexcept;
|
||||
const T &at(std::size_t n) const;
|
||||
const T &front() const noexcept;
|
||||
const T &back() const noexcept;
|
||||
|
||||
T &operator[](std::size_t n) noexcept;
|
||||
T &at(std::size_t n);
|
||||
T &front() noexcept;
|
||||
T &back() noexcept;
|
||||
|
||||
void reserve(std::size_t new_cap);
|
||||
void push_back(const T &value);
|
||||
void push_back(T &&value);
|
||||
template <typename... Args>
|
||||
void emplace_back(Args &&...args);
|
||||
void truncate(std::size_t len);
|
||||
void clear();
|
||||
|
||||
using iterator = typename Slice<T>::iterator;
|
||||
iterator begin() noexcept;
|
||||
iterator end() noexcept;
|
||||
|
||||
using const_iterator = typename Slice<const T>::iterator;
|
||||
const_iterator begin() const noexcept;
|
||||
const_iterator end() const noexcept;
|
||||
const_iterator cbegin() const noexcept;
|
||||
const_iterator cend() const noexcept;
|
||||
|
||||
void swap(Vec &) noexcept;
|
||||
|
||||
Vec(unsafe_bitcopy_t, const Vec &) noexcept;
|
||||
|
||||
private:
|
||||
void reserve_total(std::size_t new_cap) noexcept;
|
||||
void set_len(std::size_t len) noexcept;
|
||||
void drop() noexcept;
|
||||
|
||||
friend void swap(Vec &lhs, Vec &rhs) noexcept { lhs.swap(rhs); }
|
||||
|
||||
std::array<std::uintptr_t, 3> repr;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
|
||||
this->reserve_total(init.size());
|
||||
std::move(init.begin(), init.end(), std::back_inserter(*this));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T>::Vec(const Vec &other) : Vec() {
|
||||
this->reserve_total(other.size());
|
||||
std::copy(other.begin(), other.end(), std::back_inserter(*this));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
|
||||
new (&other) Vec();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T>::~Vec() noexcept {
|
||||
this->drop();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T> &Vec<T>::operator=(Vec &&other) &noexcept {
|
||||
this->drop();
|
||||
this->repr = other.repr;
|
||||
new (&other) Vec();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T> &Vec<T>::operator=(const Vec &other) & {
|
||||
if (this != &other) {
|
||||
this->drop();
|
||||
new (this) Vec(other);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Vec<T>::empty() const noexcept {
|
||||
return this->size() == 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T *Vec<T>::data() noexcept {
|
||||
return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T &Vec<T>::operator[](std::size_t n) const noexcept {
|
||||
assert(n < this->size());
|
||||
auto data = reinterpret_cast<const char *>(this->data());
|
||||
return *reinterpret_cast<const T *>(data + n * size_of<T>());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T &Vec<T>::at(std::size_t n) const {
|
||||
if (n >= this->size()) {
|
||||
panic<std::out_of_range>("rust::Vec index out of range");
|
||||
}
|
||||
return (*this)[n];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T &Vec<T>::front() const noexcept {
|
||||
assert(!this->empty());
|
||||
return (*this)[0];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T &Vec<T>::back() const noexcept {
|
||||
assert(!this->empty());
|
||||
return (*this)[this->size() - 1];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Vec<T>::operator[](std::size_t n) noexcept {
|
||||
assert(n < this->size());
|
||||
auto data = reinterpret_cast<char *>(this->data());
|
||||
return *reinterpret_cast<T *>(data + n * size_of<T>());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Vec<T>::at(std::size_t n) {
|
||||
if (n >= this->size()) {
|
||||
panic<std::out_of_range>("rust::Vec index out of range");
|
||||
}
|
||||
return (*this)[n];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Vec<T>::front() noexcept {
|
||||
assert(!this->empty());
|
||||
return (*this)[0];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Vec<T>::back() noexcept {
|
||||
assert(!this->empty());
|
||||
return (*this)[this->size() - 1];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vec<T>::reserve(std::size_t new_cap) {
|
||||
this->reserve_total(new_cap);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vec<T>::push_back(const T &value) {
|
||||
this->emplace_back(value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vec<T>::push_back(T &&value) {
|
||||
this->emplace_back(std::move(value));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename... Args>
|
||||
void Vec<T>::emplace_back(Args &&...args) {
|
||||
auto size = this->size();
|
||||
this->reserve_total(size + 1);
|
||||
::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
|
||||
size * size_of<T>()))
|
||||
T(std::forward<Args>(args)...);
|
||||
this->set_len(size + 1);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vec<T>::clear() {
|
||||
this->truncate(0);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vec<T>::iterator Vec<T>::begin() noexcept {
|
||||
return Slice<T>(this->data(), this->size()).begin();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vec<T>::iterator Vec<T>::end() noexcept {
|
||||
return Slice<T>(this->data(), this->size()).end();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
|
||||
return this->cbegin();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
|
||||
return this->cend();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
|
||||
return Slice<const T>(this->data(), this->size()).begin();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
|
||||
return Slice<const T>(this->data(), this->size()).end();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vec<T>::swap(Vec &rhs) noexcept {
|
||||
using std::swap;
|
||||
swap(this->repr, rhs.repr);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
|
||||
#endif // CXXBRIDGE1_RUST_VEC
|
||||
|
||||
#ifndef CXXBRIDGE1_IS_COMPLETE
|
||||
#define CXXBRIDGE1_IS_COMPLETE
|
||||
namespace detail {
|
||||
namespace {
|
||||
template <typename T, typename = std::size_t>
|
||||
struct is_complete : std::false_type {};
|
||||
template <typename T>
|
||||
struct is_complete<T, decltype(sizeof(T))> : std::true_type {};
|
||||
} // namespace
|
||||
} // namespace detail
|
||||
#endif // CXXBRIDGE1_IS_COMPLETE
|
||||
|
||||
#ifndef CXXBRIDGE1_LAYOUT
|
||||
#define CXXBRIDGE1_LAYOUT
|
||||
class layout {
|
||||
template <typename T>
|
||||
friend std::size_t size_of();
|
||||
template <typename T>
|
||||
friend std::size_t align_of();
|
||||
template <typename T>
|
||||
static typename std::enable_if<std::is_base_of<Opaque, T>::value,
|
||||
std::size_t>::type
|
||||
do_size_of() {
|
||||
return T::layout::size();
|
||||
}
|
||||
template <typename T>
|
||||
static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
|
||||
std::size_t>::type
|
||||
do_size_of() {
|
||||
return sizeof(T);
|
||||
}
|
||||
template <typename T>
|
||||
static
|
||||
typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
|
||||
size_of() {
|
||||
return do_size_of<T>();
|
||||
}
|
||||
template <typename T>
|
||||
static typename std::enable_if<std::is_base_of<Opaque, T>::value,
|
||||
std::size_t>::type
|
||||
do_align_of() {
|
||||
return T::layout::align();
|
||||
}
|
||||
template <typename T>
|
||||
static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
|
||||
std::size_t>::type
|
||||
do_align_of() {
|
||||
return alignof(T);
|
||||
}
|
||||
template <typename T>
|
||||
static
|
||||
typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
|
||||
align_of() {
|
||||
return do_align_of<T>();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::size_t size_of() {
|
||||
return layout::size_of<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::size_t align_of() {
|
||||
return layout::align_of<T>();
|
||||
}
|
||||
#endif // CXXBRIDGE1_LAYOUT
|
||||
|
||||
namespace detail {
|
||||
template <typename T, typename = void *>
|
||||
struct operator_new {
|
||||
void *operator()(::std::size_t sz) { return ::operator new(sz); }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct operator_new<T, decltype(T::operator new(sizeof(T)))> {
|
||||
void *operator()(::std::size_t sz) { return T::operator new(sz); }
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
template <typename T>
|
||||
union MaybeUninit {
|
||||
T value;
|
||||
void *operator new(::std::size_t sz) { return detail::operator_new<T>{}(sz); }
|
||||
MaybeUninit() {}
|
||||
~MaybeUninit() {}
|
||||
};
|
||||
} // namespace cxxbridge1
|
||||
} // namespace rust
|
||||
|
||||
namespace prqlc {
|
||||
struct Options;
|
||||
struct SourceTreeElement;
|
||||
enum class MessageKind : ::std::uint8_t;
|
||||
struct Message;
|
||||
struct CompileResult2;
|
||||
}
|
||||
|
||||
namespace prqlc {
|
||||
#ifndef CXXBRIDGE1_STRUCT_prqlc$Options
|
||||
#define CXXBRIDGE1_STRUCT_prqlc$Options
|
||||
struct Options final {
|
||||
bool format;
|
||||
::rust::String target;
|
||||
bool signature_comment;
|
||||
|
||||
using IsRelocatable = ::std::true_type;
|
||||
};
|
||||
#endif // CXXBRIDGE1_STRUCT_prqlc$Options
|
||||
|
||||
#ifndef CXXBRIDGE1_STRUCT_prqlc$SourceTreeElement
|
||||
#define CXXBRIDGE1_STRUCT_prqlc$SourceTreeElement
|
||||
struct SourceTreeElement final {
|
||||
::rust::String path;
|
||||
::rust::String content;
|
||||
|
||||
using IsRelocatable = ::std::true_type;
|
||||
};
|
||||
#endif // CXXBRIDGE1_STRUCT_prqlc$SourceTreeElement
|
||||
|
||||
#ifndef CXXBRIDGE1_ENUM_prqlc$MessageKind
|
||||
#define CXXBRIDGE1_ENUM_prqlc$MessageKind
|
||||
enum class MessageKind : ::std::uint8_t {
|
||||
Error = 0,
|
||||
Warning = 1,
|
||||
Lint = 2,
|
||||
};
|
||||
#endif // CXXBRIDGE1_ENUM_prqlc$MessageKind
|
||||
|
||||
#ifndef CXXBRIDGE1_STRUCT_prqlc$Message
|
||||
#define CXXBRIDGE1_STRUCT_prqlc$Message
|
||||
struct Message final {
|
||||
::prqlc::MessageKind kind;
|
||||
::rust::String code;
|
||||
::rust::String reason;
|
||||
::rust::Vec<::rust::String> hints;
|
||||
::rust::String display;
|
||||
|
||||
using IsRelocatable = ::std::true_type;
|
||||
};
|
||||
#endif // CXXBRIDGE1_STRUCT_prqlc$Message
|
||||
|
||||
#ifndef CXXBRIDGE1_STRUCT_prqlc$CompileResult2
|
||||
#define CXXBRIDGE1_STRUCT_prqlc$CompileResult2
|
||||
struct CompileResult2 final {
|
||||
::rust::String output;
|
||||
::rust::Vec<::prqlc::Message> messages;
|
||||
|
||||
using IsRelocatable = ::std::true_type;
|
||||
};
|
||||
#endif // CXXBRIDGE1_STRUCT_prqlc$CompileResult2
|
||||
|
||||
extern "C" {
|
||||
void prqlc$cxxbridge1$compile_tree(::rust::Vec<::prqlc::SourceTreeElement> const &tree, ::prqlc::Options const &options, ::prqlc::CompileResult2 *return$) noexcept;
|
||||
} // extern "C"
|
||||
|
||||
::prqlc::CompileResult2 compile_tree(::rust::Vec<::prqlc::SourceTreeElement> const &tree, ::prqlc::Options const &options) noexcept {
|
||||
::rust::MaybeUninit<::prqlc::CompileResult2> return$;
|
||||
prqlc$cxxbridge1$compile_tree(tree, options, &return$.value);
|
||||
return ::std::move(return$.value);
|
||||
}
|
||||
} // namespace prqlc
|
||||
|
||||
extern "C" {
|
||||
void cxxbridge1$rust_vec$prqlc$Message$new(::rust::Vec<::prqlc::Message> const *ptr) noexcept;
|
||||
void cxxbridge1$rust_vec$prqlc$Message$drop(::rust::Vec<::prqlc::Message> *ptr) noexcept;
|
||||
::std::size_t cxxbridge1$rust_vec$prqlc$Message$len(::rust::Vec<::prqlc::Message> const *ptr) noexcept;
|
||||
::std::size_t cxxbridge1$rust_vec$prqlc$Message$capacity(::rust::Vec<::prqlc::Message> const *ptr) noexcept;
|
||||
::prqlc::Message const *cxxbridge1$rust_vec$prqlc$Message$data(::rust::Vec<::prqlc::Message> const *ptr) noexcept;
|
||||
void cxxbridge1$rust_vec$prqlc$Message$reserve_total(::rust::Vec<::prqlc::Message> *ptr, ::std::size_t new_cap) noexcept;
|
||||
void cxxbridge1$rust_vec$prqlc$Message$set_len(::rust::Vec<::prqlc::Message> *ptr, ::std::size_t len) noexcept;
|
||||
void cxxbridge1$rust_vec$prqlc$Message$truncate(::rust::Vec<::prqlc::Message> *ptr, ::std::size_t len) noexcept;
|
||||
|
||||
void cxxbridge1$rust_vec$prqlc$SourceTreeElement$new(::rust::Vec<::prqlc::SourceTreeElement> const *ptr) noexcept;
|
||||
void cxxbridge1$rust_vec$prqlc$SourceTreeElement$drop(::rust::Vec<::prqlc::SourceTreeElement> *ptr) noexcept;
|
||||
::std::size_t cxxbridge1$rust_vec$prqlc$SourceTreeElement$len(::rust::Vec<::prqlc::SourceTreeElement> const *ptr) noexcept;
|
||||
::std::size_t cxxbridge1$rust_vec$prqlc$SourceTreeElement$capacity(::rust::Vec<::prqlc::SourceTreeElement> const *ptr) noexcept;
|
||||
::prqlc::SourceTreeElement const *cxxbridge1$rust_vec$prqlc$SourceTreeElement$data(::rust::Vec<::prqlc::SourceTreeElement> const *ptr) noexcept;
|
||||
void cxxbridge1$rust_vec$prqlc$SourceTreeElement$reserve_total(::rust::Vec<::prqlc::SourceTreeElement> *ptr, ::std::size_t new_cap) noexcept;
|
||||
void cxxbridge1$rust_vec$prqlc$SourceTreeElement$set_len(::rust::Vec<::prqlc::SourceTreeElement> *ptr, ::std::size_t len) noexcept;
|
||||
void cxxbridge1$rust_vec$prqlc$SourceTreeElement$truncate(::rust::Vec<::prqlc::SourceTreeElement> *ptr, ::std::size_t len) noexcept;
|
||||
} // extern "C"
|
||||
|
||||
namespace rust {
|
||||
inline namespace cxxbridge1 {
|
||||
template <>
|
||||
Vec<::prqlc::Message>::Vec() noexcept {
|
||||
cxxbridge1$rust_vec$prqlc$Message$new(this);
|
||||
}
|
||||
template <>
|
||||
void Vec<::prqlc::Message>::drop() noexcept {
|
||||
return cxxbridge1$rust_vec$prqlc$Message$drop(this);
|
||||
}
|
||||
template <>
|
||||
::std::size_t Vec<::prqlc::Message>::size() const noexcept {
|
||||
return cxxbridge1$rust_vec$prqlc$Message$len(this);
|
||||
}
|
||||
template <>
|
||||
::std::size_t Vec<::prqlc::Message>::capacity() const noexcept {
|
||||
return cxxbridge1$rust_vec$prqlc$Message$capacity(this);
|
||||
}
|
||||
template <>
|
||||
::prqlc::Message const *Vec<::prqlc::Message>::data() const noexcept {
|
||||
return cxxbridge1$rust_vec$prqlc$Message$data(this);
|
||||
}
|
||||
template <>
|
||||
void Vec<::prqlc::Message>::reserve_total(::std::size_t new_cap) noexcept {
|
||||
return cxxbridge1$rust_vec$prqlc$Message$reserve_total(this, new_cap);
|
||||
}
|
||||
template <>
|
||||
void Vec<::prqlc::Message>::set_len(::std::size_t len) noexcept {
|
||||
return cxxbridge1$rust_vec$prqlc$Message$set_len(this, len);
|
||||
}
|
||||
template <>
|
||||
void Vec<::prqlc::Message>::truncate(::std::size_t len) {
|
||||
return cxxbridge1$rust_vec$prqlc$Message$truncate(this, len);
|
||||
}
|
||||
template <>
|
||||
Vec<::prqlc::SourceTreeElement>::Vec() noexcept {
|
||||
cxxbridge1$rust_vec$prqlc$SourceTreeElement$new(this);
|
||||
}
|
||||
template <>
|
||||
void Vec<::prqlc::SourceTreeElement>::drop() noexcept {
|
||||
return cxxbridge1$rust_vec$prqlc$SourceTreeElement$drop(this);
|
||||
}
|
||||
template <>
|
||||
::std::size_t Vec<::prqlc::SourceTreeElement>::size() const noexcept {
|
||||
return cxxbridge1$rust_vec$prqlc$SourceTreeElement$len(this);
|
||||
}
|
||||
template <>
|
||||
::std::size_t Vec<::prqlc::SourceTreeElement>::capacity() const noexcept {
|
||||
return cxxbridge1$rust_vec$prqlc$SourceTreeElement$capacity(this);
|
||||
}
|
||||
template <>
|
||||
::prqlc::SourceTreeElement const *Vec<::prqlc::SourceTreeElement>::data() const noexcept {
|
||||
return cxxbridge1$rust_vec$prqlc$SourceTreeElement$data(this);
|
||||
}
|
||||
template <>
|
||||
void Vec<::prqlc::SourceTreeElement>::reserve_total(::std::size_t new_cap) noexcept {
|
||||
return cxxbridge1$rust_vec$prqlc$SourceTreeElement$reserve_total(this, new_cap);
|
||||
}
|
||||
template <>
|
||||
void Vec<::prqlc::SourceTreeElement>::set_len(::std::size_t len) noexcept {
|
||||
return cxxbridge1$rust_vec$prqlc$SourceTreeElement$set_len(this, len);
|
||||
}
|
||||
template <>
|
||||
void Vec<::prqlc::SourceTreeElement>::truncate(::std::size_t len) {
|
||||
return cxxbridge1$rust_vec$prqlc$SourceTreeElement$truncate(this, len);
|
||||
}
|
||||
} // namespace cxxbridge1
|
||||
} // namespace rust
|
@ -0,0 +1,775 @@
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
#include <new>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace rust {
|
||||
inline namespace cxxbridge1 {
|
||||
// #include "rust/cxx.h"
|
||||
|
||||
#ifndef CXXBRIDGE1_PANIC
|
||||
#define CXXBRIDGE1_PANIC
|
||||
template <typename Exception>
|
||||
void panic [[noreturn]] (const char *msg);
|
||||
#endif // CXXBRIDGE1_PANIC
|
||||
|
||||
struct unsafe_bitcopy_t;
|
||||
|
||||
namespace {
|
||||
template <typename T>
|
||||
class impl;
|
||||
} // namespace
|
||||
|
||||
class Opaque;
|
||||
|
||||
template <typename T>
|
||||
::std::size_t size_of();
|
||||
template <typename T>
|
||||
::std::size_t align_of();
|
||||
|
||||
#ifndef CXXBRIDGE1_RUST_STRING
|
||||
#define CXXBRIDGE1_RUST_STRING
|
||||
class String final {
|
||||
public:
|
||||
String() noexcept;
|
||||
String(const String &) noexcept;
|
||||
String(String &&) noexcept;
|
||||
~String() noexcept;
|
||||
|
||||
String(const std::string &);
|
||||
String(const char *);
|
||||
String(const char *, std::size_t);
|
||||
String(const char16_t *);
|
||||
String(const char16_t *, std::size_t);
|
||||
|
||||
static String lossy(const std::string &) noexcept;
|
||||
static String lossy(const char *) noexcept;
|
||||
static String lossy(const char *, std::size_t) noexcept;
|
||||
static String lossy(const char16_t *) noexcept;
|
||||
static String lossy(const char16_t *, std::size_t) noexcept;
|
||||
|
||||
String &operator=(const String &) &noexcept;
|
||||
String &operator=(String &&) &noexcept;
|
||||
|
||||
explicit operator std::string() const;
|
||||
|
||||
const char *data() const noexcept;
|
||||
std::size_t size() const noexcept;
|
||||
std::size_t length() const noexcept;
|
||||
bool empty() const noexcept;
|
||||
|
||||
const char *c_str() noexcept;
|
||||
|
||||
std::size_t capacity() const noexcept;
|
||||
void reserve(size_t new_cap) noexcept;
|
||||
|
||||
using iterator = char *;
|
||||
iterator begin() noexcept;
|
||||
iterator end() noexcept;
|
||||
|
||||
using const_iterator = const char *;
|
||||
const_iterator begin() const noexcept;
|
||||
const_iterator end() const noexcept;
|
||||
const_iterator cbegin() const noexcept;
|
||||
const_iterator cend() const noexcept;
|
||||
|
||||
bool operator==(const String &) const noexcept;
|
||||
bool operator!=(const String &) const noexcept;
|
||||
bool operator<(const String &) const noexcept;
|
||||
bool operator<=(const String &) const noexcept;
|
||||
bool operator>(const String &) const noexcept;
|
||||
bool operator>=(const String &) const noexcept;
|
||||
|
||||
void swap(String &) noexcept;
|
||||
|
||||
String(unsafe_bitcopy_t, const String &) noexcept;
|
||||
|
||||
private:
|
||||
struct lossy_t;
|
||||
String(lossy_t, const char *, std::size_t) noexcept;
|
||||
String(lossy_t, const char16_t *, std::size_t) noexcept;
|
||||
friend void swap(String &lhs, String &rhs) noexcept { lhs.swap(rhs); }
|
||||
|
||||
std::array<std::uintptr_t, 3> repr;
|
||||
};
|
||||
#endif // CXXBRIDGE1_RUST_STRING
|
||||
|
||||
#ifndef CXXBRIDGE1_RUST_SLICE
|
||||
#define CXXBRIDGE1_RUST_SLICE
|
||||
namespace detail {
|
||||
template <bool>
|
||||
struct copy_assignable_if {};
|
||||
|
||||
template <>
|
||||
struct copy_assignable_if<false> {
|
||||
copy_assignable_if() noexcept = default;
|
||||
copy_assignable_if(const copy_assignable_if &) noexcept = default;
|
||||
copy_assignable_if &operator=(const copy_assignable_if &) &noexcept = delete;
|
||||
copy_assignable_if &operator=(copy_assignable_if &&) &noexcept = default;
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
template <typename T>
|
||||
class Slice final
|
||||
: private detail::copy_assignable_if<std::is_const<T>::value> {
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
Slice() noexcept;
|
||||
Slice(T *, std::size_t count) noexcept;
|
||||
|
||||
Slice &operator=(const Slice<T> &) &noexcept = default;
|
||||
Slice &operator=(Slice<T> &&) &noexcept = default;
|
||||
|
||||
T *data() const noexcept;
|
||||
std::size_t size() const noexcept;
|
||||
std::size_t length() const noexcept;
|
||||
bool empty() const noexcept;
|
||||
|
||||
T &operator[](std::size_t n) const noexcept;
|
||||
T &at(std::size_t n) const;
|
||||
T &front() const noexcept;
|
||||
T &back() const noexcept;
|
||||
|
||||
Slice(const Slice<T> &) noexcept = default;
|
||||
~Slice() noexcept = default;
|
||||
|
||||
class iterator;
|
||||
iterator begin() const noexcept;
|
||||
iterator end() const noexcept;
|
||||
|
||||
void swap(Slice &) noexcept;
|
||||
|
||||
private:
|
||||
class uninit;
|
||||
Slice(uninit) noexcept;
|
||||
friend impl<Slice>;
|
||||
friend void sliceInit(void *, const void *, std::size_t) noexcept;
|
||||
friend void *slicePtr(const void *) noexcept;
|
||||
friend std::size_t sliceLen(const void *) noexcept;
|
||||
|
||||
std::array<std::uintptr_t, 2> repr;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class Slice<T>::iterator final {
|
||||
public:
|
||||
using iterator_category = std::random_access_iterator_tag;
|
||||
using value_type = T;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = typename std::add_pointer<T>::type;
|
||||
using reference = typename std::add_lvalue_reference<T>::type;
|
||||
|
||||
reference operator*() const noexcept;
|
||||
pointer operator->() const noexcept;
|
||||
reference operator[](difference_type) const noexcept;
|
||||
|
||||
iterator &operator++() noexcept;
|
||||
iterator operator++(int) noexcept;
|
||||
iterator &operator--() noexcept;
|
||||
iterator operator--(int) noexcept;
|
||||
|
||||
iterator &operator+=(difference_type) noexcept;
|
||||
iterator &operator-=(difference_type) noexcept;
|
||||
iterator operator+(difference_type) const noexcept;
|
||||
iterator operator-(difference_type) const noexcept;
|
||||
difference_type operator-(const iterator &) const noexcept;
|
||||
|
||||
bool operator==(const iterator &) const noexcept;
|
||||
bool operator!=(const iterator &) const noexcept;
|
||||
bool operator<(const iterator &) const noexcept;
|
||||
bool operator<=(const iterator &) const noexcept;
|
||||
bool operator>(const iterator &) const noexcept;
|
||||
bool operator>=(const iterator &) const noexcept;
|
||||
|
||||
private:
|
||||
friend class Slice;
|
||||
void *pos;
|
||||
std::size_t stride;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
Slice<T>::Slice() noexcept {
|
||||
sliceInit(this, reinterpret_cast<void *>(align_of<T>()), 0);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Slice<T>::Slice(T *s, std::size_t count) noexcept {
|
||||
assert(s != nullptr || count == 0);
|
||||
sliceInit(this,
|
||||
s == nullptr && count == 0
|
||||
? reinterpret_cast<void *>(align_of<T>())
|
||||
: const_cast<typename std::remove_const<T>::type *>(s),
|
||||
count);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T *Slice<T>::data() const noexcept {
|
||||
return reinterpret_cast<T *>(slicePtr(this));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::size_t Slice<T>::size() const noexcept {
|
||||
return sliceLen(this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::size_t Slice<T>::length() const noexcept {
|
||||
return this->size();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::empty() const noexcept {
|
||||
return this->size() == 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Slice<T>::operator[](std::size_t n) const noexcept {
|
||||
assert(n < this->size());
|
||||
auto ptr = static_cast<char *>(slicePtr(this)) + size_of<T>() * n;
|
||||
return *reinterpret_cast<T *>(ptr);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Slice<T>::at(std::size_t n) const {
|
||||
if (n >= this->size()) {
|
||||
panic<std::out_of_range>("rust::Slice index out of range");
|
||||
}
|
||||
return (*this)[n];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Slice<T>::front() const noexcept {
|
||||
assert(!this->empty());
|
||||
return (*this)[0];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Slice<T>::back() const noexcept {
|
||||
assert(!this->empty());
|
||||
return (*this)[this->size() - 1];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator::reference
|
||||
Slice<T>::iterator::operator*() const noexcept {
|
||||
return *static_cast<T *>(this->pos);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator::pointer
|
||||
Slice<T>::iterator::operator->() const noexcept {
|
||||
return static_cast<T *>(this->pos);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator::reference Slice<T>::iterator::operator[](
|
||||
typename Slice<T>::iterator::difference_type n) const noexcept {
|
||||
auto ptr = static_cast<char *>(this->pos) + this->stride * n;
|
||||
return *reinterpret_cast<T *>(ptr);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
|
||||
this->pos = static_cast<char *>(this->pos) + this->stride;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
|
||||
auto ret = iterator(*this);
|
||||
this->pos = static_cast<char *>(this->pos) + this->stride;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator &Slice<T>::iterator::operator--() noexcept {
|
||||
this->pos = static_cast<char *>(this->pos) - this->stride;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::iterator::operator--(int) noexcept {
|
||||
auto ret = iterator(*this);
|
||||
this->pos = static_cast<char *>(this->pos) - this->stride;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator &Slice<T>::iterator::operator+=(
|
||||
typename Slice<T>::iterator::difference_type n) noexcept {
|
||||
this->pos = static_cast<char *>(this->pos) + this->stride * n;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator &Slice<T>::iterator::operator-=(
|
||||
typename Slice<T>::iterator::difference_type n) noexcept {
|
||||
this->pos = static_cast<char *>(this->pos) - this->stride * n;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::iterator::operator+(
|
||||
typename Slice<T>::iterator::difference_type n) const noexcept {
|
||||
auto ret = iterator(*this);
|
||||
ret.pos = static_cast<char *>(this->pos) + this->stride * n;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::iterator::operator-(
|
||||
typename Slice<T>::iterator::difference_type n) const noexcept {
|
||||
auto ret = iterator(*this);
|
||||
ret.pos = static_cast<char *>(this->pos) - this->stride * n;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator::difference_type
|
||||
Slice<T>::iterator::operator-(const iterator &other) const noexcept {
|
||||
auto diff = std::distance(static_cast<char *>(other.pos),
|
||||
static_cast<char *>(this->pos));
|
||||
return diff / static_cast<typename Slice<T>::iterator::difference_type>(
|
||||
this->stride);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
|
||||
return this->pos == other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
|
||||
return this->pos != other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator<(const iterator &other) const noexcept {
|
||||
return this->pos < other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator<=(const iterator &other) const noexcept {
|
||||
return this->pos <= other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator>(const iterator &other) const noexcept {
|
||||
return this->pos > other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator>=(const iterator &other) const noexcept {
|
||||
return this->pos >= other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::begin() const noexcept {
|
||||
iterator it;
|
||||
it.pos = slicePtr(this);
|
||||
it.stride = size_of<T>();
|
||||
return it;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::end() const noexcept {
|
||||
iterator it = this->begin();
|
||||
it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
|
||||
return it;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Slice<T>::swap(Slice &rhs) noexcept {
|
||||
std::swap(*this, rhs);
|
||||
}
|
||||
#endif // CXXBRIDGE1_RUST_SLICE
|
||||
|
||||
#ifndef CXXBRIDGE1_RUST_BITCOPY_T
|
||||
#define CXXBRIDGE1_RUST_BITCOPY_T
|
||||
struct unsafe_bitcopy_t final {
|
||||
explicit unsafe_bitcopy_t() = default;
|
||||
};
|
||||
#endif // CXXBRIDGE1_RUST_BITCOPY_T
|
||||
|
||||
#ifndef CXXBRIDGE1_RUST_VEC
|
||||
#define CXXBRIDGE1_RUST_VEC
|
||||
template <typename T>
|
||||
class Vec final {
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
Vec() noexcept;
|
||||
Vec(std::initializer_list<T>);
|
||||
Vec(const Vec &);
|
||||
Vec(Vec &&) noexcept;
|
||||
~Vec() noexcept;
|
||||
|
||||
Vec &operator=(Vec &&) &noexcept;
|
||||
Vec &operator=(const Vec &) &;
|
||||
|
||||
std::size_t size() const noexcept;
|
||||
bool empty() const noexcept;
|
||||
const T *data() const noexcept;
|
||||
T *data() noexcept;
|
||||
std::size_t capacity() const noexcept;
|
||||
|
||||
const T &operator[](std::size_t n) const noexcept;
|
||||
const T &at(std::size_t n) const;
|
||||
const T &front() const noexcept;
|
||||
const T &back() const noexcept;
|
||||
|
||||
T &operator[](std::size_t n) noexcept;
|
||||
T &at(std::size_t n);
|
||||
T &front() noexcept;
|
||||
T &back() noexcept;
|
||||
|
||||
void reserve(std::size_t new_cap);
|
||||
void push_back(const T &value);
|
||||
void push_back(T &&value);
|
||||
template <typename... Args>
|
||||
void emplace_back(Args &&...args);
|
||||
void truncate(std::size_t len);
|
||||
void clear();
|
||||
|
||||
using iterator = typename Slice<T>::iterator;
|
||||
iterator begin() noexcept;
|
||||
iterator end() noexcept;
|
||||
|
||||
using const_iterator = typename Slice<const T>::iterator;
|
||||
const_iterator begin() const noexcept;
|
||||
const_iterator end() const noexcept;
|
||||
const_iterator cbegin() const noexcept;
|
||||
const_iterator cend() const noexcept;
|
||||
|
||||
void swap(Vec &) noexcept;
|
||||
|
||||
Vec(unsafe_bitcopy_t, const Vec &) noexcept;
|
||||
|
||||
private:
|
||||
void reserve_total(std::size_t new_cap) noexcept;
|
||||
void set_len(std::size_t len) noexcept;
|
||||
void drop() noexcept;
|
||||
|
||||
friend void swap(Vec &lhs, Vec &rhs) noexcept { lhs.swap(rhs); }
|
||||
|
||||
std::array<std::uintptr_t, 3> repr;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
|
||||
this->reserve_total(init.size());
|
||||
std::move(init.begin(), init.end(), std::back_inserter(*this));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T>::Vec(const Vec &other) : Vec() {
|
||||
this->reserve_total(other.size());
|
||||
std::copy(other.begin(), other.end(), std::back_inserter(*this));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
|
||||
new (&other) Vec();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T>::~Vec() noexcept {
|
||||
this->drop();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T> &Vec<T>::operator=(Vec &&other) &noexcept {
|
||||
this->drop();
|
||||
this->repr = other.repr;
|
||||
new (&other) Vec();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T> &Vec<T>::operator=(const Vec &other) & {
|
||||
if (this != &other) {
|
||||
this->drop();
|
||||
new (this) Vec(other);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Vec<T>::empty() const noexcept {
|
||||
return this->size() == 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T *Vec<T>::data() noexcept {
|
||||
return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T &Vec<T>::operator[](std::size_t n) const noexcept {
|
||||
assert(n < this->size());
|
||||
auto data = reinterpret_cast<const char *>(this->data());
|
||||
return *reinterpret_cast<const T *>(data + n * size_of<T>());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T &Vec<T>::at(std::size_t n) const {
|
||||
if (n >= this->size()) {
|
||||
panic<std::out_of_range>("rust::Vec index out of range");
|
||||
}
|
||||
return (*this)[n];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T &Vec<T>::front() const noexcept {
|
||||
assert(!this->empty());
|
||||
return (*this)[0];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T &Vec<T>::back() const noexcept {
|
||||
assert(!this->empty());
|
||||
return (*this)[this->size() - 1];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Vec<T>::operator[](std::size_t n) noexcept {
|
||||
assert(n < this->size());
|
||||
auto data = reinterpret_cast<char *>(this->data());
|
||||
return *reinterpret_cast<T *>(data + n * size_of<T>());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Vec<T>::at(std::size_t n) {
|
||||
if (n >= this->size()) {
|
||||
panic<std::out_of_range>("rust::Vec index out of range");
|
||||
}
|
||||
return (*this)[n];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Vec<T>::front() noexcept {
|
||||
assert(!this->empty());
|
||||
return (*this)[0];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Vec<T>::back() noexcept {
|
||||
assert(!this->empty());
|
||||
return (*this)[this->size() - 1];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vec<T>::reserve(std::size_t new_cap) {
|
||||
this->reserve_total(new_cap);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vec<T>::push_back(const T &value) {
|
||||
this->emplace_back(value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vec<T>::push_back(T &&value) {
|
||||
this->emplace_back(std::move(value));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename... Args>
|
||||
void Vec<T>::emplace_back(Args &&...args) {
|
||||
auto size = this->size();
|
||||
this->reserve_total(size + 1);
|
||||
::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
|
||||
size * size_of<T>()))
|
||||
T(std::forward<Args>(args)...);
|
||||
this->set_len(size + 1);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vec<T>::clear() {
|
||||
this->truncate(0);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vec<T>::iterator Vec<T>::begin() noexcept {
|
||||
return Slice<T>(this->data(), this->size()).begin();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vec<T>::iterator Vec<T>::end() noexcept {
|
||||
return Slice<T>(this->data(), this->size()).end();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
|
||||
return this->cbegin();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
|
||||
return this->cend();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
|
||||
return Slice<const T>(this->data(), this->size()).begin();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
|
||||
return Slice<const T>(this->data(), this->size()).end();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vec<T>::swap(Vec &rhs) noexcept {
|
||||
using std::swap;
|
||||
swap(this->repr, rhs.repr);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
|
||||
#endif // CXXBRIDGE1_RUST_VEC
|
||||
|
||||
#ifndef CXXBRIDGE1_IS_COMPLETE
|
||||
#define CXXBRIDGE1_IS_COMPLETE
|
||||
namespace detail {
|
||||
namespace {
|
||||
template <typename T, typename = std::size_t>
|
||||
struct is_complete : std::false_type {};
|
||||
template <typename T>
|
||||
struct is_complete<T, decltype(sizeof(T))> : std::true_type {};
|
||||
} // namespace
|
||||
} // namespace detail
|
||||
#endif // CXXBRIDGE1_IS_COMPLETE
|
||||
|
||||
#ifndef CXXBRIDGE1_LAYOUT
|
||||
#define CXXBRIDGE1_LAYOUT
|
||||
class layout {
|
||||
template <typename T>
|
||||
friend std::size_t size_of();
|
||||
template <typename T>
|
||||
friend std::size_t align_of();
|
||||
template <typename T>
|
||||
static typename std::enable_if<std::is_base_of<Opaque, T>::value,
|
||||
std::size_t>::type
|
||||
do_size_of() {
|
||||
return T::layout::size();
|
||||
}
|
||||
template <typename T>
|
||||
static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
|
||||
std::size_t>::type
|
||||
do_size_of() {
|
||||
return sizeof(T);
|
||||
}
|
||||
template <typename T>
|
||||
static
|
||||
typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
|
||||
size_of() {
|
||||
return do_size_of<T>();
|
||||
}
|
||||
template <typename T>
|
||||
static typename std::enable_if<std::is_base_of<Opaque, T>::value,
|
||||
std::size_t>::type
|
||||
do_align_of() {
|
||||
return T::layout::align();
|
||||
}
|
||||
template <typename T>
|
||||
static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
|
||||
std::size_t>::type
|
||||
do_align_of() {
|
||||
return alignof(T);
|
||||
}
|
||||
template <typename T>
|
||||
static
|
||||
typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
|
||||
align_of() {
|
||||
return do_align_of<T>();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::size_t size_of() {
|
||||
return layout::size_of<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::size_t align_of() {
|
||||
return layout::align_of<T>();
|
||||
}
|
||||
#endif // CXXBRIDGE1_LAYOUT
|
||||
} // namespace cxxbridge1
|
||||
} // namespace rust
|
||||
|
||||
namespace prqlc {
|
||||
struct Options;
|
||||
struct SourceTreeElement;
|
||||
enum class MessageKind : ::std::uint8_t;
|
||||
struct Message;
|
||||
struct CompileResult2;
|
||||
}
|
||||
|
||||
namespace prqlc {
|
||||
#ifndef CXXBRIDGE1_STRUCT_prqlc$Options
|
||||
#define CXXBRIDGE1_STRUCT_prqlc$Options
|
||||
struct Options final {
|
||||
bool format;
|
||||
::rust::String target;
|
||||
bool signature_comment;
|
||||
|
||||
using IsRelocatable = ::std::true_type;
|
||||
};
|
||||
#endif // CXXBRIDGE1_STRUCT_prqlc$Options
|
||||
|
||||
#ifndef CXXBRIDGE1_STRUCT_prqlc$SourceTreeElement
|
||||
#define CXXBRIDGE1_STRUCT_prqlc$SourceTreeElement
|
||||
struct SourceTreeElement final {
|
||||
::rust::String path;
|
||||
::rust::String content;
|
||||
|
||||
using IsRelocatable = ::std::true_type;
|
||||
};
|
||||
#endif // CXXBRIDGE1_STRUCT_prqlc$SourceTreeElement
|
||||
|
||||
#ifndef CXXBRIDGE1_ENUM_prqlc$MessageKind
|
||||
#define CXXBRIDGE1_ENUM_prqlc$MessageKind
|
||||
enum class MessageKind : ::std::uint8_t {
|
||||
Error = 0,
|
||||
Warning = 1,
|
||||
Lint = 2,
|
||||
};
|
||||
#endif // CXXBRIDGE1_ENUM_prqlc$MessageKind
|
||||
|
||||
#ifndef CXXBRIDGE1_STRUCT_prqlc$Message
|
||||
#define CXXBRIDGE1_STRUCT_prqlc$Message
|
||||
struct Message final {
|
||||
::prqlc::MessageKind kind;
|
||||
::rust::String code;
|
||||
::rust::String reason;
|
||||
::rust::Vec<::rust::String> hints;
|
||||
::rust::String display;
|
||||
|
||||
using IsRelocatable = ::std::true_type;
|
||||
};
|
||||
#endif // CXXBRIDGE1_STRUCT_prqlc$Message
|
||||
|
||||
#ifndef CXXBRIDGE1_STRUCT_prqlc$CompileResult2
|
||||
#define CXXBRIDGE1_STRUCT_prqlc$CompileResult2
|
||||
struct CompileResult2 final {
|
||||
::rust::String output;
|
||||
::rust::Vec<::prqlc::Message> messages;
|
||||
|
||||
using IsRelocatable = ::std::true_type;
|
||||
};
|
||||
#endif // CXXBRIDGE1_STRUCT_prqlc$CompileResult2
|
||||
|
||||
::prqlc::CompileResult2 compile_tree(::rust::Vec<::prqlc::SourceTreeElement> const &tree, ::prqlc::Options const &options) noexcept;
|
||||
} // namespace prqlc
|
@ -1,346 +1,126 @@
|
||||
#![cfg(not(target_family = "wasm"))]
|
||||
|
||||
extern crate libc;
|
||||
|
||||
use libc::{c_char, size_t};
|
||||
use prqlc::{ErrorMessage, ErrorMessages};
|
||||
use prqlc::Target;
|
||||
use std::ffi::CStr;
|
||||
use std::ffi::CString;
|
||||
use prqlc::{ErrorMessage, ErrorMessages};
|
||||
use std::panic;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
/// Compile a PRQL string into a SQL string.
|
||||
///
|
||||
/// This is a wrapper for: `prql_to_pl`, `pl_to_rq` and `rq_to_sql` without converting to JSON
|
||||
/// between each of the functions.
|
||||
///
|
||||
/// See `Options` struct for available compilation options.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function assumes zero-terminated input strings.
|
||||
/// Calling code is responsible for freeing memory allocated for `CompileResult`
|
||||
/// by calling `result_destroy`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn compile(
|
||||
prql_query: *const c_char,
|
||||
options: *const Options,
|
||||
) -> CompileResult {
|
||||
let prql_query: String = c_str_to_string(prql_query);
|
||||
|
||||
let options = options.as_ref().map(convert_options).transpose();
|
||||
|
||||
let result = options
|
||||
.and_then(|opts| {
|
||||
panic::catch_unwind(|| {
|
||||
Ok(prql_query.as_str())
|
||||
.and_then(prqlc::prql_to_pl)
|
||||
.and_then(prqlc::pl_to_rq)
|
||||
.and_then(|rq| prqlc::rq_to_sql(rq, &opts.unwrap_or_default()))
|
||||
}).map_err(|p| ErrorMessages {
|
||||
inner: vec![ErrorMessage {
|
||||
kind: prqlc::MessageKind::Error,
|
||||
code: None,
|
||||
reason: format!("internal error: {:#?}", p),
|
||||
hints: vec![],
|
||||
span: None,
|
||||
display: None,
|
||||
location: None,
|
||||
}]
|
||||
})?
|
||||
})
|
||||
.map_err(|e| e.composed(&prql_query.into()));
|
||||
|
||||
result_into_c_str(result)
|
||||
}
|
||||
|
||||
/// Build PL AST from a PRQL string. PL in documented in the
|
||||
/// [prqlc Rust crate](https://docs.rs/prqlc/latest/prqlc/ir/pl).
|
||||
///
|
||||
/// Takes PRQL source buffer and writes PL serialized as JSON to `out` buffer.
|
||||
///
|
||||
/// Returns 0 on success and a negative number -1 on failure.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function assumes zero-terminated input strings.
|
||||
/// Calling code is responsible for freeing memory allocated for `CompileResult`
|
||||
/// by calling `result_destroy`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn prql_to_pl(prql_query: *const c_char) -> CompileResult {
|
||||
let prql_query: String = c_str_to_string(prql_query);
|
||||
|
||||
let result = Ok(prql_query.as_str())
|
||||
.and_then(prqlc::prql_to_pl)
|
||||
.and_then(|x| prqlc::json::from_pl(&x));
|
||||
result_into_c_str(result)
|
||||
}
|
||||
|
||||
/// Finds variable references, validates functions calls, determines frames and converts PL to RQ.
|
||||
/// PL and RQ are documented in the
|
||||
/// [prqlc Rust crate](https://docs.rs/prqlc/latest/prqlc/ast).
|
||||
///
|
||||
/// Takes PL serialized as JSON buffer and writes RQ serialized as JSON to `out` buffer.
|
||||
///
|
||||
/// Returns 0 on success and a negative number -1 on failure.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function assumes zero-terminated input strings.
|
||||
/// Calling code is responsible for freeing memory allocated for `CompileResult`
|
||||
/// by calling `result_destroy`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn pl_to_rq(pl_json: *const c_char) -> CompileResult {
|
||||
let pl_json: String = c_str_to_string(pl_json);
|
||||
|
||||
let result = Ok(pl_json.as_str())
|
||||
.and_then(prqlc::json::to_pl)
|
||||
.and_then(prqlc::pl_to_rq)
|
||||
.and_then(|x| prqlc::json::from_rq(&x));
|
||||
result_into_c_str(result)
|
||||
}
|
||||
|
||||
/// Convert RQ AST into an SQL string. RQ is documented in the
|
||||
/// [prqlc Rust crate](https://docs.rs/prqlc/latest/prqlc/ir/rq).
|
||||
///
|
||||
/// Takes RQ serialized as JSON buffer and writes SQL source to `out` buffer.
|
||||
///
|
||||
/// Returns 0 on success and a negative number -1 on failure.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function assumes zero-terminated input strings.
|
||||
/// Calling code is responsible for freeing memory allocated for `CompileResult`
|
||||
/// by calling `result_destroy`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn rq_to_sql(
|
||||
rq_json: *const c_char,
|
||||
options: *const Options,
|
||||
) -> CompileResult {
|
||||
let rq_json: String = c_str_to_string(rq_json);
|
||||
|
||||
let options = options.as_ref().map(convert_options).transpose();
|
||||
|
||||
let result = options.and_then(|options| {
|
||||
Ok(rq_json.as_str())
|
||||
.and_then(prqlc::json::to_rq)
|
||||
.and_then(|x| prqlc::rq_to_sql(x, &options.unwrap_or_default()))
|
||||
});
|
||||
result_into_c_str(result)
|
||||
}
|
||||
|
||||
/// Compilation options
|
||||
#[repr(C)]
|
||||
pub struct Options {
|
||||
/// Pass generated SQL string trough a formatter that splits it
|
||||
/// into multiple lines and prettifies indentation and spacing.
|
||||
///
|
||||
/// Defaults to true.
|
||||
pub format: bool,
|
||||
|
||||
/// Target and dialect to compile to.
|
||||
///
|
||||
/// Defaults to `sql.any`, which uses `target` argument from the query header to determine
|
||||
/// the SQL dialect.
|
||||
pub target: *mut c_char,
|
||||
|
||||
/// Emits the compiler signature as a comment after generated SQL
|
||||
///
|
||||
/// Defaults to true.
|
||||
pub signature_comment: bool,
|
||||
}
|
||||
|
||||
/// Result of compilation.
|
||||
#[repr(C)]
|
||||
pub struct CompileResult {
|
||||
pub output: *const libc::c_char,
|
||||
pub messages: *const Message,
|
||||
pub messages_len: size_t,
|
||||
}
|
||||
|
||||
/// Compile message kind. Currently only Error is implemented.
|
||||
#[repr(C)]
|
||||
pub enum MessageKind {
|
||||
Error,
|
||||
Warning,
|
||||
Lint,
|
||||
}
|
||||
#[cxx::bridge(namespace = "prqlc")]
|
||||
mod ffi {
|
||||
struct Options {
|
||||
pub format: bool,
|
||||
pub target: String,
|
||||
pub signature_comment: bool,
|
||||
}
|
||||
|
||||
/// Compile result message.
|
||||
///
|
||||
/// Calling code is responsible for freeing all memory allocated
|
||||
/// for fields as well as strings.
|
||||
// Make sure to keep in sync with prqlc::ErrorMessage
|
||||
#[repr(C)]
|
||||
pub struct Message {
|
||||
/// Message kind. Currently only Error is implemented.
|
||||
pub kind: MessageKind,
|
||||
/// Machine-readable identifier of the error
|
||||
pub code: *const *const libc::c_char,
|
||||
/// Plain text of the error
|
||||
pub reason: *const libc::c_char,
|
||||
/// A list of suggestions of how to fix the error
|
||||
pub hint: *const *const libc::c_char,
|
||||
/// Character offset of error origin within a source file
|
||||
pub span: *const Span,
|
||||
struct SourceTreeElement {
|
||||
pub path: String,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
/// Annotated code, containing cause and hints.
|
||||
pub display: *const *const libc::c_char,
|
||||
/// Line and column number of error origin within a source file
|
||||
pub location: *const SourceLocation,
|
||||
}
|
||||
enum MessageKind {
|
||||
Error,
|
||||
Warning,
|
||||
Lint,
|
||||
}
|
||||
|
||||
/// Identifier of a location in source.
|
||||
/// Contains offsets in terms of chars.
|
||||
// Make sure to keep in sync with prqlc::Span
|
||||
#[repr(C)]
|
||||
pub struct Span {
|
||||
pub start: size_t,
|
||||
pub end: size_t,
|
||||
}
|
||||
struct Message {
|
||||
pub kind: MessageKind,
|
||||
pub code: String,
|
||||
pub reason: String,
|
||||
pub hints: Vec<String>,
|
||||
pub display: String,
|
||||
}
|
||||
|
||||
/// Location within a source file.
|
||||
// Make sure to keep in sync with prqlc::SourceLocation
|
||||
#[repr(C)]
|
||||
pub struct SourceLocation {
|
||||
pub start_line: size_t,
|
||||
pub start_col: size_t,
|
||||
#[derive(Default)]
|
||||
struct CompileResult2 {
|
||||
pub output: String,
|
||||
pub messages: Vec<Message>,
|
||||
}
|
||||
|
||||
pub end_line: size_t,
|
||||
pub end_col: size_t,
|
||||
extern "Rust" {
|
||||
fn compile_tree(tree: &Vec<SourceTreeElement>, options: &Options) -> CompileResult2;
|
||||
}
|
||||
}
|
||||
|
||||
/// Destroy a `CompileResult` once you are done with it.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function expects to be called exactly once after the call of any the functions
|
||||
/// that return `CompileResult`. No fields should be freed manually.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn result_destroy(res: CompileResult) {
|
||||
// This is required because we are allocating memory for
|
||||
// strings, vectors and options.
|
||||
// For strings and vectors this is required, but options may be
|
||||
// able to live entirely within the struct, instead of the heap.
|
||||
|
||||
for i in 0..res.messages_len {
|
||||
let e = &*res.messages.add(i);
|
||||
impl TryFrom<&ffi::Options> for prqlc::Options {
|
||||
type Error = ErrorMessages;
|
||||
|
||||
if !e.code.is_null() {
|
||||
drop(CString::from_raw(*e.code as *mut libc::c_char));
|
||||
drop(Box::from_raw(e.code as *mut *const libc::c_char));
|
||||
}
|
||||
drop(CString::from_raw(e.reason as *mut libc::c_char));
|
||||
if !e.hint.is_null() {
|
||||
drop(CString::from_raw(*e.hint as *mut libc::c_char));
|
||||
drop(Box::from_raw(e.hint as *mut *const libc::c_char));
|
||||
}
|
||||
if !e.span.is_null() {
|
||||
drop(Box::from_raw(e.span as *mut Span));
|
||||
}
|
||||
if !e.display.is_null() {
|
||||
drop(CString::from_raw(*e.display as *mut libc::c_char));
|
||||
drop(Box::from_raw(e.display as *mut *const libc::c_char));
|
||||
}
|
||||
if !e.location.is_null() {
|
||||
drop(Box::from_raw(e.location as *mut SourceLocation));
|
||||
}
|
||||
fn try_from(value: &ffi::Options) -> Result<Self, Self::Error> {
|
||||
Ok(prqlc::Options {
|
||||
format: value.format,
|
||||
target: Target::from_str(value.target.as_str()).map_err(prqlc::ErrorMessages::from)?,
|
||||
signature_comment: value.signature_comment,
|
||||
color: false,
|
||||
})
|
||||
}
|
||||
drop(Vec::from_raw_parts(
|
||||
res.messages as *mut i8,
|
||||
res.messages_len,
|
||||
res.messages_len,
|
||||
));
|
||||
drop(CString::from_raw(res.output as *mut libc::c_char));
|
||||
}
|
||||
|
||||
unsafe fn result_into_c_str(result: Result<String, ErrorMessages>) -> CompileResult {
|
||||
match result {
|
||||
Ok(output) => CompileResult {
|
||||
output: convert_string(output),
|
||||
messages: ::std::ptr::null_mut(),
|
||||
messages_len: 0,
|
||||
},
|
||||
Err(err) => {
|
||||
let mut errors = Vec::with_capacity(err.inner.len());
|
||||
errors.extend(err.inner.into_iter().map(|e| Message {
|
||||
kind: MessageKind::Error,
|
||||
code: option_to_ptr(e.code.map(convert_string)),
|
||||
reason: convert_string(e.reason),
|
||||
hint: option_to_ptr(if e.hints.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(convert_string(e.hints.join("\n")))
|
||||
}),
|
||||
span: option_to_ptr(e.span.map(convert_span)),
|
||||
display: option_to_ptr(e.display.map(convert_string)),
|
||||
location: option_to_ptr(e.location.map(convert_source_location)),
|
||||
}));
|
||||
CompileResult {
|
||||
output: CString::default().into_raw(),
|
||||
messages_len: errors.len(),
|
||||
messages: errors.leak().as_ptr(),
|
||||
}
|
||||
impl From<prqlc::MessageKind> for ffi::MessageKind {
|
||||
fn from(value: prqlc::MessageKind) -> Self {
|
||||
match value {
|
||||
prqlc::MessageKind::Error => ffi::MessageKind::Error,
|
||||
prqlc::MessageKind::Warning => ffi::MessageKind::Warning,
|
||||
prqlc::MessageKind::Lint => ffi::MessageKind::Lint,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Allocates the value on the heap and returns a pointer to it.
|
||||
/// If the input is None, it returns null pointer.
|
||||
fn option_to_ptr<T>(o: Option<T>) -> *const T {
|
||||
match o {
|
||||
Some(x) => {
|
||||
let b = Box::new(x);
|
||||
Box::into_raw(b)
|
||||
impl From<ErrorMessage> for ffi::Message {
|
||||
fn from(value: ErrorMessage) -> Self {
|
||||
ffi::Message {
|
||||
kind: value.kind.into(),
|
||||
code: value.code.unwrap_or(String::new()),
|
||||
reason: value.reason,
|
||||
hints: value.hints,
|
||||
display: value.display.unwrap_or(String::new()),
|
||||
}
|
||||
None => ::std::ptr::null(),
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_string(x: String) -> *const libc::c_char {
|
||||
CString::new(x).unwrap_or_default().into_raw()
|
||||
fn compile_tree_int(
|
||||
tree: &Vec<ffi::SourceTreeElement>,
|
||||
options: &ffi::Options,
|
||||
) -> Result<String, ErrorMessages> {
|
||||
let tree = prqlc::SourceTree::new(
|
||||
tree.iter()
|
||||
.map(|ste| (PathBuf::from(ste.path.clone()), ste.content.clone())),
|
||||
None,
|
||||
);
|
||||
|
||||
let options: prqlc::Options = options.try_into()?;
|
||||
|
||||
panic::catch_unwind(|| {
|
||||
Ok(prqlc::prql_to_pl_tree(&tree)
|
||||
.and_then(prqlc::pl_to_rq)
|
||||
.map_err(|e: ErrorMessages| ErrorMessages::from(e).composed(&tree))
|
||||
.and_then(|rq| prqlc::rq_to_sql(rq, &options))?)
|
||||
.map_err(|e: ErrorMessages| ErrorMessages::from(e).composed(&tree))
|
||||
})
|
||||
.map_err(|p| {
|
||||
ErrorMessages::from(ErrorMessage {
|
||||
kind: prqlc::MessageKind::Error,
|
||||
code: None,
|
||||
reason: format!("internal error: {:#?}", p),
|
||||
hints: vec![],
|
||||
span: None,
|
||||
display: None,
|
||||
location: None,
|
||||
})
|
||||
})?
|
||||
}
|
||||
|
||||
fn convert_span(x: prqlc::Span) -> Span {
|
||||
Span {
|
||||
start: x.start,
|
||||
end: x.end,
|
||||
}
|
||||
}
|
||||
pub fn compile_tree(
|
||||
tree: &Vec<ffi::SourceTreeElement>,
|
||||
options: &ffi::Options,
|
||||
) -> ffi::CompileResult2 {
|
||||
let mut retval = ffi::CompileResult2::default();
|
||||
|
||||
fn convert_source_location(x: prqlc::SourceLocation) -> SourceLocation {
|
||||
SourceLocation {
|
||||
start_line: x.start.0,
|
||||
start_col: x.start.1,
|
||||
end_line: x.end.0,
|
||||
end_col: x.end.1,
|
||||
match compile_tree_int(tree, options) {
|
||||
Ok(output) => retval.output = output,
|
||||
Err(errors) => retval.messages = errors.inner.into_iter().map(|x| x.into()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn c_str_to_string(c_str: *const c_char) -> String {
|
||||
// inefficient, but simple
|
||||
CStr::from_ptr(c_str).to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
fn convert_options(o: &Options) -> Result<prqlc::Options, prqlc::ErrorMessages> {
|
||||
let target = if !o.target.is_null() {
|
||||
Some(unsafe { c_str_to_string(o.target) })
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let target = target
|
||||
.as_deref()
|
||||
.filter(|x| !x.is_empty())
|
||||
.unwrap_or("sql.any");
|
||||
|
||||
let target = Target::from_str(target).map_err(prqlc::ErrorMessages::from)?;
|
||||
|
||||
Ok(prqlc::Options {
|
||||
format: o.format,
|
||||
target,
|
||||
signature_comment: o.signature_comment,
|
||||
// TODO: add support for this
|
||||
color: false,
|
||||
})
|
||||
retval
|
||||
}
|
||||
|
Loading…
Reference in New Issue