[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
//===--- Pointer.h - Types for the constexpr 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 the classes responsible for pointer tracking.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_AST_INTERP_POINTER_H
|
|
|
|
#define LLVM_CLANG_AST_INTERP_POINTER_H
|
|
|
|
|
|
|
|
#include "Descriptor.h"
|
2020-02-20 14:54:22 -08:00
|
|
|
#include "InterpBlock.h"
|
|
|
|
#include "clang/AST/ComparisonCategories.h"
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "llvm/ADT/PointerUnion.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace interp {
|
|
|
|
class Block;
|
|
|
|
class DeadBlock;
|
|
|
|
class Pointer;
|
2023-02-21 09:36:37 +01:00
|
|
|
class Context;
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
enum PrimType : unsigned;
|
|
|
|
|
2023-08-15 17:14:47 +02:00
|
|
|
class Pointer;
|
|
|
|
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P);
|
|
|
|
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
/// A pointer to a memory block, live or dead.
|
|
|
|
///
|
|
|
|
/// This object can be allocated into interpreter stack frames. If pointing to
|
|
|
|
/// a live block, it is a link in the chain of pointers pointing to the block.
|
2022-12-26 09:29:04 +01:00
|
|
|
///
|
|
|
|
/// In the simplest form, a Pointer has a Block* (the pointee) and both Base
|
|
|
|
/// and Offset are 0, which means it will point to raw data.
|
|
|
|
///
|
|
|
|
/// The Base field is used to access metadata about the data. For primitive
|
|
|
|
/// arrays, the Base is followed by an InitMap. In a variety of cases, the
|
|
|
|
/// Base is preceded by an InlineDescriptor, which is used to track the
|
|
|
|
/// initialization state, among other things.
|
|
|
|
///
|
|
|
|
/// The Offset field is used to access the actual data. In other words, the
|
|
|
|
/// data the pointer decribes can be found at
|
|
|
|
/// Pointee->rawData() + Pointer.Offset.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Pointee Offset
|
|
|
|
/// │ │
|
|
|
|
/// │ │
|
|
|
|
/// ▼ ▼
|
|
|
|
/// ┌───────┬────────────┬─────────┬────────────────────────────┐
|
|
|
|
/// │ Block │ InlineDesc │ InitMap │ Actual Data │
|
|
|
|
/// └───────┴────────────┴─────────┴────────────────────────────┘
|
|
|
|
/// ▲
|
|
|
|
/// │
|
|
|
|
/// │
|
|
|
|
/// Base
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
class Pointer {
|
|
|
|
private:
|
2023-01-18 15:10:40 +01:00
|
|
|
static constexpr unsigned PastEndMark = ~0u;
|
|
|
|
static constexpr unsigned RootPtrMark = ~0u;
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
Pointer() {}
|
|
|
|
Pointer(Block *B);
|
2022-12-26 09:29:04 +01:00
|
|
|
Pointer(Block *B, unsigned BaseAndOffset);
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
Pointer(const Pointer &P);
|
|
|
|
Pointer(Pointer &&P);
|
|
|
|
~Pointer();
|
|
|
|
|
|
|
|
void operator=(const Pointer &P);
|
|
|
|
void operator=(Pointer &&P);
|
|
|
|
|
2023-08-15 20:35:21 +02:00
|
|
|
/// Equality operators are just for tests.
|
|
|
|
bool operator==(const Pointer &P) const {
|
|
|
|
return Pointee == P.Pointee && Base == P.Base && Offset == P.Offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Pointer &P) const {
|
|
|
|
return Pointee != P.Pointee || Base != P.Base || Offset != P.Offset;
|
|
|
|
}
|
|
|
|
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
/// Converts the pointer to an APValue.
|
|
|
|
APValue toAPValue() const;
|
|
|
|
|
2023-05-19 11:12:05 +02:00
|
|
|
/// Converts the pointer to a string usable in diagnostics.
|
|
|
|
std::string toDiagnosticString(const ASTContext &Ctx) const;
|
|
|
|
|
|
|
|
unsigned getIntegerRepresentation() const {
|
|
|
|
return reinterpret_cast<uintptr_t>(Pointee) + Offset;
|
|
|
|
}
|
|
|
|
|
2023-02-21 09:36:37 +01:00
|
|
|
/// Converts the pointer to an APValue that is an rvalue.
|
|
|
|
APValue toRValue(const Context &Ctx) const;
|
|
|
|
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
/// Offsets a pointer inside an array.
|
2023-08-15 17:15:43 +02:00
|
|
|
[[nodiscard]] Pointer atIndex(unsigned Idx) const {
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
if (Base == RootPtrMark)
|
|
|
|
return Pointer(Pointee, RootPtrMark, getDeclDesc()->getSize());
|
|
|
|
unsigned Off = Idx * elemSize();
|
|
|
|
if (getFieldDesc()->ElemDesc)
|
|
|
|
Off += sizeof(InlineDescriptor);
|
|
|
|
else
|
2023-10-24 06:49:11 +02:00
|
|
|
Off += sizeof(InitMapPtr);
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
return Pointer(Pointee, Base, Base + Off);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a pointer to a field.
|
2023-08-15 17:15:43 +02:00
|
|
|
[[nodiscard]] Pointer atField(unsigned Off) const {
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
unsigned Field = Offset + Off;
|
|
|
|
return Pointer(Pointee, Field, Field);
|
|
|
|
}
|
|
|
|
|
2023-04-24 16:45:16 +02:00
|
|
|
/// Subtract the given offset from the current Base and Offset
|
|
|
|
/// of the pointer.
|
2023-08-15 17:15:43 +02:00
|
|
|
[[nodiscard]] Pointer atFieldSub(unsigned Off) const {
|
2023-04-24 16:45:16 +02:00
|
|
|
assert(Offset >= Off);
|
|
|
|
unsigned O = Offset - Off;
|
|
|
|
return Pointer(Pointee, O, O);
|
|
|
|
}
|
|
|
|
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
/// Restricts the scope of an array element pointer.
|
2023-08-15 17:15:43 +02:00
|
|
|
[[nodiscard]] Pointer narrow() const {
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
// Null pointers cannot be narrowed.
|
|
|
|
if (isZero() || isUnknownSizeArray())
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
// Pointer to an array of base types - enter block.
|
|
|
|
if (Base == RootPtrMark)
|
|
|
|
return Pointer(Pointee, 0, Offset == 0 ? Offset : PastEndMark);
|
|
|
|
|
|
|
|
// Pointer is one past end - magic offset marks that.
|
|
|
|
if (isOnePastEnd())
|
|
|
|
return Pointer(Pointee, Base, PastEndMark);
|
|
|
|
|
|
|
|
// Primitive arrays are a bit special since they do not have inline
|
|
|
|
// descriptors. If Offset != Base, then the pointer already points to
|
|
|
|
// an element and there is nothing to do. Otherwise, the pointer is
|
|
|
|
// adjusted to the first element of the array.
|
|
|
|
if (inPrimitiveArray()) {
|
|
|
|
if (Offset != Base)
|
|
|
|
return *this;
|
2023-10-24 06:49:11 +02:00
|
|
|
return Pointer(Pointee, Base, Offset + sizeof(InitMapPtr));
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pointer is to a field or array element - enter it.
|
|
|
|
if (Offset != Base)
|
|
|
|
return Pointer(Pointee, Offset, Offset);
|
|
|
|
|
|
|
|
// Enter the first element of an array.
|
|
|
|
if (!getFieldDesc()->isArray())
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
const unsigned NewBase = Base + sizeof(InlineDescriptor);
|
|
|
|
return Pointer(Pointee, NewBase, NewBase);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Expands a pointer to the containing array, undoing narrowing.
|
2023-08-15 17:15:43 +02:00
|
|
|
[[nodiscard]] Pointer expand() const {
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
if (isElementPastEnd()) {
|
|
|
|
// Revert to an outer one-past-end pointer.
|
|
|
|
unsigned Adjust;
|
|
|
|
if (inPrimitiveArray())
|
2023-10-24 06:49:11 +02:00
|
|
|
Adjust = sizeof(InitMapPtr);
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
else
|
|
|
|
Adjust = sizeof(InlineDescriptor);
|
|
|
|
return Pointer(Pointee, Base, Base + getSize() + Adjust);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not step out of array elements.
|
|
|
|
if (Base != Offset)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
// If at base, point to an array of base types.
|
|
|
|
if (Base == 0)
|
|
|
|
return Pointer(Pointee, RootPtrMark, 0);
|
|
|
|
|
|
|
|
// Step into the containing array, if inside one.
|
|
|
|
unsigned Next = Base - getInlineDesc()->Offset;
|
2023-10-26 11:23:14 +02:00
|
|
|
const Descriptor *Desc =
|
|
|
|
Next == 0 ? getDeclDesc() : getDescriptor(Next)->Desc;
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
if (!Desc->IsArray)
|
|
|
|
return *this;
|
|
|
|
return Pointer(Pointee, Next, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if the pointer is null.
|
|
|
|
bool isZero() const { return Pointee == nullptr; }
|
|
|
|
/// Checks if the pointer is live.
|
|
|
|
bool isLive() const { return Pointee && !Pointee->IsDead; }
|
|
|
|
/// Checks if the item is a field in an object.
|
|
|
|
bool isField() const { return Base != 0 && Base != RootPtrMark; }
|
|
|
|
|
|
|
|
/// Accessor for information about the declaration site.
|
2023-10-26 11:23:14 +02:00
|
|
|
const Descriptor *getDeclDesc() const { return Pointee->Desc; }
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
SourceLocation getDeclLoc() const { return getDeclDesc()->getLocation(); }
|
|
|
|
|
|
|
|
/// Returns a pointer to the object of which this pointer is a field.
|
2023-08-15 17:15:43 +02:00
|
|
|
[[nodiscard]] Pointer getBase() const {
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
if (Base == RootPtrMark) {
|
|
|
|
assert(Offset == PastEndMark && "cannot get base of a block");
|
|
|
|
return Pointer(Pointee, Base, 0);
|
|
|
|
}
|
|
|
|
assert(Offset == Base && "not an inner field");
|
|
|
|
unsigned NewBase = Base - getInlineDesc()->Offset;
|
|
|
|
return Pointer(Pointee, NewBase, NewBase);
|
|
|
|
}
|
|
|
|
/// Returns the parent array.
|
2023-08-15 17:15:43 +02:00
|
|
|
[[nodiscard]] Pointer getArray() const {
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
if (Base == RootPtrMark) {
|
|
|
|
assert(Offset != 0 && Offset != PastEndMark && "not an array element");
|
|
|
|
return Pointer(Pointee, Base, 0);
|
|
|
|
}
|
|
|
|
assert(Offset != Base && "not an array element");
|
|
|
|
return Pointer(Pointee, Base, Base);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Accessors for information about the innermost field.
|
2023-10-26 11:23:14 +02:00
|
|
|
const Descriptor *getFieldDesc() const {
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
if (Base == 0 || Base == RootPtrMark)
|
|
|
|
return getDeclDesc();
|
|
|
|
return getInlineDesc()->Desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the type of the innermost field.
|
2023-07-04 11:01:51 +02:00
|
|
|
QualType getType() const {
|
|
|
|
if (inPrimitiveArray() && Offset != Base)
|
|
|
|
return getFieldDesc()->getType()->getAsArrayTypeUnsafe()->getElementType();
|
|
|
|
return getFieldDesc()->getType();
|
|
|
|
}
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
|
2023-08-15 17:15:43 +02:00
|
|
|
[[nodiscard]] Pointer getDeclPtr() const { return Pointer(Pointee); }
|
2023-01-26 16:25:52 +01:00
|
|
|
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
/// Returns the element size of the innermost field.
|
|
|
|
size_t elemSize() const {
|
|
|
|
if (Base == RootPtrMark)
|
|
|
|
return getDeclDesc()->getSize();
|
|
|
|
return getFieldDesc()->getElemSize();
|
|
|
|
}
|
|
|
|
/// Returns the total size of the innermost field.
|
|
|
|
size_t getSize() const { return getFieldDesc()->getSize(); }
|
|
|
|
|
|
|
|
/// Returns the offset into an array.
|
|
|
|
unsigned getOffset() const {
|
|
|
|
assert(Offset != PastEndMark && "invalid offset");
|
|
|
|
if (Base == RootPtrMark)
|
|
|
|
return Offset;
|
|
|
|
|
|
|
|
unsigned Adjust = 0;
|
|
|
|
if (Offset != Base) {
|
|
|
|
if (getFieldDesc()->ElemDesc)
|
|
|
|
Adjust = sizeof(InlineDescriptor);
|
|
|
|
else
|
2023-10-24 06:49:11 +02:00
|
|
|
Adjust = sizeof(InitMapPtr);
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
}
|
|
|
|
return Offset - Base - Adjust;
|
|
|
|
}
|
|
|
|
|
2022-10-31 15:48:15 +01:00
|
|
|
/// Whether this array refers to an array, but not
|
|
|
|
/// to the first element.
|
|
|
|
bool isArrayRoot() const { return inArray() && Offset == Base; }
|
|
|
|
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
/// Checks if the innermost field is an array.
|
|
|
|
bool inArray() const { return getFieldDesc()->IsArray; }
|
|
|
|
/// Checks if the structure is a primitive array.
|
|
|
|
bool inPrimitiveArray() const { return getFieldDesc()->isPrimitiveArray(); }
|
|
|
|
/// Checks if the structure is an array of unknown size.
|
|
|
|
bool isUnknownSizeArray() const {
|
|
|
|
return getFieldDesc()->isUnknownSizeArray();
|
|
|
|
}
|
|
|
|
/// Checks if the pointer points to an array.
|
|
|
|
bool isArrayElement() const { return Base != Offset; }
|
|
|
|
/// Pointer points directly to a block.
|
|
|
|
bool isRoot() const {
|
|
|
|
return (Base == 0 || Base == RootPtrMark) && Offset == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the record descriptor of a class.
|
2023-05-02 11:21:11 +02:00
|
|
|
const Record *getRecord() const { return getFieldDesc()->ElemRecord; }
|
2023-06-05 15:27:51 +02:00
|
|
|
/// Returns the element record type, if this is a non-primive array.
|
2023-05-02 11:21:11 +02:00
|
|
|
const Record *getElemRecord() const {
|
2023-08-15 20:35:21 +02:00
|
|
|
const Descriptor *ElemDesc = getFieldDesc()->ElemDesc;
|
|
|
|
return ElemDesc ? ElemDesc->ElemRecord : nullptr;
|
2023-05-02 11:21:11 +02:00
|
|
|
}
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
/// Returns the field information.
|
|
|
|
const FieldDecl *getField() const { return getFieldDesc()->asFieldDecl(); }
|
|
|
|
|
|
|
|
/// Checks if the object is a union.
|
|
|
|
bool isUnion() const;
|
|
|
|
|
|
|
|
/// Checks if the storage is extern.
|
|
|
|
bool isExtern() const { return Pointee->isExtern(); }
|
|
|
|
/// Checks if the storage is static.
|
|
|
|
bool isStatic() const { return Pointee->isStatic(); }
|
|
|
|
/// Checks if the storage is temporary.
|
|
|
|
bool isTemporary() const { return Pointee->isTemporary(); }
|
|
|
|
/// Checks if the storage is a static temporary.
|
|
|
|
bool isStaticTemporary() const { return isStatic() && isTemporary(); }
|
|
|
|
|
|
|
|
/// Checks if the field is mutable.
|
2023-01-01 13:22:59 +01:00
|
|
|
bool isMutable() const {
|
|
|
|
return Base != 0 && getInlineDesc()->IsFieldMutable;
|
|
|
|
}
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
/// Checks if an object was initialized.
|
|
|
|
bool isInitialized() const;
|
|
|
|
/// Checks if the object is active.
|
|
|
|
bool isActive() const { return Base == 0 || getInlineDesc()->IsActive; }
|
|
|
|
/// Checks if a structure is a base class.
|
|
|
|
bool isBaseClass() const { return isField() && getInlineDesc()->IsBase; }
|
2023-10-26 15:15:25 +02:00
|
|
|
/// Checks if the pointer pointers to a dummy value.
|
|
|
|
bool isDummy() const { return getDeclDesc()->isDummy(); }
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
|
|
|
|
/// Checks if an object or a subfield is mutable.
|
|
|
|
bool isConst() const {
|
|
|
|
return Base == 0 ? getDeclDesc()->IsConst : getInlineDesc()->IsConst;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the declaration ID.
|
2022-12-05 17:30:24 +01:00
|
|
|
std::optional<unsigned> getDeclID() const { return Pointee->getDeclID(); }
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
|
|
|
|
/// Returns the byte offset from the start.
|
|
|
|
unsigned getByteOffset() const {
|
|
|
|
return Offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of elements.
|
|
|
|
unsigned getNumElems() const { return getSize() / elemSize(); }
|
|
|
|
|
2023-05-02 08:47:36 +02:00
|
|
|
const Block *block() const { return Pointee; }
|
2022-10-26 11:20:13 +02:00
|
|
|
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
/// Returns the index into an array.
|
|
|
|
int64_t getIndex() const {
|
|
|
|
if (isElementPastEnd())
|
|
|
|
return 1;
|
2023-08-15 20:35:21 +02:00
|
|
|
|
|
|
|
// narrow()ed element in a composite array.
|
|
|
|
if (Base > 0 && Base == Offset)
|
|
|
|
return 0;
|
|
|
|
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
if (auto ElemSize = elemSize())
|
|
|
|
return getOffset() / ElemSize;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if the index is one past end.
|
|
|
|
bool isOnePastEnd() const {
|
|
|
|
return isElementPastEnd() || getSize() == getOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if the pointer is an out-of-bounds element pointer.
|
|
|
|
bool isElementPastEnd() const { return Offset == PastEndMark; }
|
|
|
|
|
|
|
|
/// Dereferences the pointer, if it's live.
|
|
|
|
template <typename T> T &deref() const {
|
|
|
|
assert(isLive() && "Invalid pointer");
|
2022-10-31 15:48:15 +01:00
|
|
|
if (isArrayRoot())
|
|
|
|
return *reinterpret_cast<T *>(Pointee->rawData() + Base +
|
2023-10-24 06:49:11 +02:00
|
|
|
sizeof(InitMapPtr));
|
2022-10-31 15:48:15 +01:00
|
|
|
|
2022-12-26 09:29:04 +01:00
|
|
|
return *reinterpret_cast<T *>(Pointee->rawData() + Offset);
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Dereferences a primitive element.
|
|
|
|
template <typename T> T &elem(unsigned I) const {
|
2023-05-04 07:29:57 +02:00
|
|
|
assert(I < getNumElems());
|
2023-10-24 06:49:11 +02:00
|
|
|
return reinterpret_cast<T *>(Pointee->data() + sizeof(InitMapPtr))[I];
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Initializes a field.
|
|
|
|
void initialize() const;
|
|
|
|
/// Activats a field.
|
|
|
|
void activate() const;
|
|
|
|
/// Deactivates an entire strurcutre.
|
|
|
|
void deactivate() const;
|
|
|
|
|
2023-09-29 09:41:58 +02:00
|
|
|
/// Compare two pointers.
|
|
|
|
ComparisonCategoryResult compare(const Pointer &Other) const {
|
|
|
|
if (!hasSameBase(*this, Other))
|
|
|
|
return ComparisonCategoryResult::Unordered;
|
|
|
|
|
|
|
|
if (Offset < Other.Offset)
|
|
|
|
return ComparisonCategoryResult::Less;
|
|
|
|
else if (Offset > Other.Offset)
|
|
|
|
return ComparisonCategoryResult::Greater;
|
|
|
|
|
|
|
|
return ComparisonCategoryResult::Equal;
|
|
|
|
}
|
|
|
|
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
/// Checks if two pointers are comparable.
|
|
|
|
static bool hasSameBase(const Pointer &A, const Pointer &B);
|
|
|
|
/// Checks if two pointers can be subtracted.
|
|
|
|
static bool hasSameArray(const Pointer &A, const Pointer &B);
|
|
|
|
|
|
|
|
/// Prints the pointer.
|
|
|
|
void print(llvm::raw_ostream &OS) const {
|
2023-08-15 17:07:34 +02:00
|
|
|
OS << Pointee << " {";
|
|
|
|
if (Base == RootPtrMark)
|
|
|
|
OS << "rootptr, ";
|
|
|
|
else
|
|
|
|
OS << Base << ", ";
|
|
|
|
|
|
|
|
if (Offset == PastEndMark)
|
|
|
|
OS << "pastend, ";
|
|
|
|
else
|
|
|
|
OS << Offset << ", ";
|
|
|
|
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
if (Pointee)
|
|
|
|
OS << Pointee->getSize();
|
|
|
|
else
|
|
|
|
OS << "nullptr";
|
|
|
|
OS << "}";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class Block;
|
|
|
|
friend class DeadBlock;
|
2023-10-24 06:49:11 +02:00
|
|
|
friend struct InitMap;
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
|
|
|
|
Pointer(Block *Pointee, unsigned Base, unsigned Offset);
|
|
|
|
|
|
|
|
/// Returns the embedded descriptor preceding a field.
|
|
|
|
InlineDescriptor *getInlineDesc() const { return getDescriptor(Base); }
|
|
|
|
|
|
|
|
/// Returns a descriptor at a given offset.
|
|
|
|
InlineDescriptor *getDescriptor(unsigned Offset) const {
|
|
|
|
assert(Offset != 0 && "Not a nested pointer");
|
2022-12-26 09:29:04 +01:00
|
|
|
return reinterpret_cast<InlineDescriptor *>(Pointee->rawData() + Offset) -
|
|
|
|
1;
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 06:49:11 +02:00
|
|
|
/// Returns a reference to the InitMapPtr which stores the initialization map.
|
|
|
|
InitMapPtr &getInitMap() const {
|
|
|
|
return *reinterpret_cast<InitMapPtr *>(Pointee->rawData() + Base);
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The block the pointer is pointing to.
|
|
|
|
Block *Pointee = nullptr;
|
|
|
|
/// Start of the current subfield.
|
|
|
|
unsigned Base = 0;
|
|
|
|
/// Offset into the block.
|
|
|
|
unsigned Offset = 0;
|
|
|
|
|
|
|
|
/// Previous link in the pointer chain.
|
|
|
|
Pointer *Prev = nullptr;
|
|
|
|
/// Next link in the pointer chain.
|
|
|
|
Pointer *Next = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P) {
|
|
|
|
P.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace interp
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|