#ifndef LLVM_CLANG_AST_INTERP_SOURCE_H
#define LLVM_CLANG_AST_INTERP_SOURCE_H
#include "PrimType.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/Stmt.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/Support/Endian.h"
namespace clang {
class Expr;
class SourceLocation;
class SourceRange;
namespace interp {
class Function;
class CodePtr final {
public:
CodePtr() : Ptr(nullptr) {}
CodePtr &operator+=(int32_t Offset) {
Ptr += Offset;
return *this;
}
int32_t operator-(const CodePtr &RHS) const {
assert(Ptr != nullptr && RHS.Ptr != nullptr && "Invalid code pointer");
return Ptr - RHS.Ptr;
}
CodePtr operator-(size_t RHS) const {
assert(Ptr != nullptr && "Invalid code pointer");
return CodePtr(Ptr - RHS);
}
bool operator!=(const CodePtr &RHS) const { return Ptr != RHS.Ptr; }
const std::byte *operator*() const { return Ptr; }
operator bool() const { return Ptr; }
template <typename T> std::enable_if_t<!std::is_pointer<T>::value, T> read() {
assert(aligned(Ptr));
using namespace llvm::support;
T Value = endian::read<T, llvm::endianness::native>(Ptr);
Ptr += align(sizeof(T));
return Value;
}
private:
friend class Function;
CodePtr(const std::byte *Ptr) : Ptr(Ptr) {}
const std::byte *Ptr;
};
class SourceInfo final {
public:
SourceInfo() {}
SourceInfo(const Stmt *E) : Source(E) {}
SourceInfo(const Decl *D) : Source(D) {}
SourceLocation getLoc() const;
SourceRange getRange() const;
const Stmt *asStmt() const { return Source.dyn_cast<const Stmt *>(); }
const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
const Expr *asExpr() const;
operator bool() const { return !Source.isNull(); }
private:
llvm::PointerUnion<const Decl *, const Stmt *> Source;
};
using SourceMap = std::vector<std::pair<unsigned, SourceInfo>>;
class SourceMapper {
public:
virtual ~SourceMapper() {}
virtual SourceInfo getSource(const Function *F, CodePtr PC) const = 0;
const Expr *getExpr(const Function *F, CodePtr PC) const;
SourceLocation getLocation(const Function *F, CodePtr PC) const;
SourceRange getRange(const Function *F, CodePtr PC) const;
};
}
}
#endif