Files
clang-p2996/clang/lib/AST/Interp/Source.h
Sterling Augustine b9543f7de6 Revert "[clang][Interp][NFC] Trim Source.h includes"
This reverts commit 245d10b7a2 which causes
errors like the ones below on slightly older versions of clang:

```
In file included from .../clang/lib/AST/Interp/Source.h:17:
In file included from .../llvm/include/llvm/ADT/PointerUnion.h:19:
In file included from .../llvm/include/llvm/ADT/PointerIntPair.h:18:
.../llvm/include/llvm/Support/PointerLikeTypeTraits.h:61:28: error: invalid application of 'alignof' to an incomplete type 'clang::Decl'
   61 |       detail::ConstantLog2<alignof(T)>::value;
      |                            ^~~~~~~~~~
.../llvm/include/llvm/Support/PointerLikeTypeTraits.h:101:56: note: in instantiation of static data member 'llvm::PointerLikeTypeTraits<clang::Decl *>::NumLowBitsAvailable' requested here
  101 |   static constexpr int NumLowBitsAvailable = NonConst::NumLowBitsAvailable;
      |                                                        ^
.../llvm/include/llvm/ADT/PointerUnion.h:38:54: note: in instantiation of static data member 'llvm::PointerLikeTypeTraits<const clang::Decl *>::NumLowBitsAvailable' requested here
   38 |     return std::min<int>({PointerLikeTypeTraits<Ts>::NumLowBitsAvailable...});
      |                                                      ^
.../llvm/include/llvm/ADT/PointerUnion.h:52:48: note: in instantiation of function template specialization 'llvm::pointer_union_detail::lowBitsAvailable<const clang::Decl *, const clang::Stmt *>' requested here
   52 |     static constexpr int NumLowBitsAvailable = lowBitsAvailable<PTs...>();
      |                                                ^
.../llvm/include/llvm/ADT/PointerIntPair.h:169:28: note: in instantiation of static data member 'llvm::pointer_union_detail::PointerUnionUIntTraits<const clang::Decl *, const clang::Stmt *>::NumLowBitsAvailable' requested here
  169 |   static_assert(PtrTraits::NumLowBitsAvailable <
      |                            ^
.../llvm/include/llvm/ADT/PointerIntPair.h:111:13: note: in instantiation of template class 'llvm::PointerIntPairInfo<void *, 1, llvm::pointer_union_detail::PointerUnionUIntTraits<const clang::Decl *, const clang::Stmt *>>' requested here
  111 |     Value = Info::updateInt(Info::updatePointer(0, PtrVal),
      |             ^
.../llvm/include/llvm/ADT/PointerIntPair.h:89:5: note: in instantiation of member function 'llvm::PointerIntPair<void *, 1, int, llvm::pointer_union_detail::PointerUnionUIntTraits<const clang::Decl *, const clang::Stmt *>>::setPointerAndInt' requested here
   89 |     setPointerAndInt(PtrVal, IntVal);
      |     ^
.../llvm/include/llvm/ADT/PointerUnion.h:77:16: note: in instantiation of member function 'llvm::PointerIntPair<void *, 1, int, llvm::pointer_union_detail::PointerUnionUIntTraits<const clang::Decl *, const clang::Stmt *>>::PointerIntPair' requested here
   77 |         : Base(ValTy(const_cast<void *>(
      |                ^
.../clang/lib/AST/Interp/Source.h:76:31: note: in instantiation of member function 'llvm::pointer_union_detail::PointerUnionMembers<llvm::PointerUnion<const clang::Decl *, const clang::Stmt *>, llvm::PointerIntPair<void *, 1, int, llvm::pointer_union_detail::PointerUnionUIntTraits<const clang::Decl *, const clang::Stmt *>>, 1, const clang::Stmt *>::PointerUnionMembers' requested here
   76 |   SourceInfo(const Stmt *E) : Source(E) {}
      |                               ^
.../clang/lib/AST/Interp/Source.h:22:7: note: forward declaration of 'clang::Decl'
   22 | class Decl;
      |       ^
```
2023-07-14 13:34:26 -07:00

105 lines
3.0 KiB
C++

//===--- Source.h - Source location provider for the VM --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Defines a program which organises and links multiple bytecode functions.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_AST_INTERP_SOURCE_H
#define LLVM_CLANG_AST_INTERP_SOURCE_H
#include "PrimType.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Stmt.h"
#include "llvm/Support/Endian.h"
namespace clang {
namespace interp {
class Function;
/// Pointer into the code segment.
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; }
operator bool() const { return Ptr; }
/// Reads data and advances the pointer.
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, endianness::native, 1>(Ptr);
Ptr += align(sizeof(T));
return Value;
}
private:
friend class Function;
/// Constructor used by Function to generate pointers.
CodePtr(const std::byte *Ptr) : Ptr(Ptr) {}
/// Pointer into the code owned by a function.
const std::byte *Ptr;
};
/// Describes the statement/declaration an opcode was generated from.
class SourceInfo final {
public:
SourceInfo() {}
SourceInfo(const Stmt *E) : Source(E) {}
SourceInfo(const Decl *D) : Source(D) {}
SourceLocation getLoc() 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>>;
/// Interface for classes which map locations to sources.
class SourceMapper {
public:
virtual ~SourceMapper() {}
/// Returns source information for a given PC in a function.
virtual SourceInfo getSource(const Function *F, CodePtr PC) const = 0;
/// Returns the expression if an opcode belongs to one, null otherwise.
const Expr *getExpr(const Function *F, CodePtr PC) const;
/// Returns the location from which an opcode originates.
SourceLocation getLocation(const Function *F, CodePtr PC) const;
};
} // namespace interp
} // namespace clang
#endif