1 //===--- Pointer.cpp - Types for the constexpr VM ---------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 #include "InterpBlock.h"
14 using namespace clang;
15 using namespace clang::interp;
17 Pointer::Pointer(Block *Pointee) : Pointer(Pointee, 0, 0) {}
19 Pointer::Pointer(const Pointer &P) : Pointer(P.Pointee, P.Base, P.Offset) {}
21 Pointer::Pointer(Pointer &&P)
22 : Pointee(P.Pointee), Base(P.Base), Offset(P.Offset) {
24 Pointee->movePointer(&P, this);
27 Pointer::Pointer(Block *Pointee, unsigned Base, unsigned Offset)
28 : Pointee(Pointee), Base(Base), Offset(Offset) {
29 assert((Base == RootPtrMark || Base % alignof(void *) == 0) && "wrong base");
31 Pointee->addPointer(this);
36 Pointee->removePointer(this);
41 void Pointer::operator=(const Pointer &P) {
45 Pointee->removePointer(this);
52 Pointee->addPointer(this);
58 void Pointer::operator=(Pointer &&P) {
62 Pointee->removePointer(this);
69 Pointee->movePointer(&P, this);
75 APValue Pointer::toAPValue() const {
76 APValue::LValueBase Base;
77 llvm::SmallVector<APValue::LValuePathEntry, 5> Path;
83 Base = static_cast<const Expr *>(nullptr);
86 Offset = CharUnits::Zero();
88 // Build the lvalue base from the block.
89 Descriptor *Desc = getDeclDesc();
90 if (auto *VD = Desc->asValueDecl())
92 else if (auto *E = Desc->asExpr())
95 llvm_unreachable("Invalid allocation type");
97 // Not a null pointer.
100 if (isUnknownSizeArray()) {
101 IsOnePastEnd = false;
102 Offset = CharUnits::Zero();
104 // TODO: compute the offset into the object.
105 Offset = CharUnits::Zero();
107 // Build the path into the object.
109 while (Ptr.isField()) {
110 if (Ptr.isArrayElement()) {
111 Path.push_back(APValue::LValuePathEntry::ArrayIndex(Ptr.getIndex()));
112 Ptr = Ptr.getArray();
114 // TODO: figure out if base is virtual
115 bool IsVirtual = false;
117 // Create a path entry for the field.
118 Descriptor *Desc = Ptr.getFieldDesc();
119 if (auto *BaseOrMember = Desc->asDecl()) {
120 Path.push_back(APValue::LValuePathEntry({BaseOrMember, IsVirtual}));
124 llvm_unreachable("Invalid field type");
128 IsOnePastEnd = isOnePastEnd();
132 return APValue(Base, Offset, Path, IsOnePastEnd, IsNullPtr);
135 bool Pointer::isInitialized() const {
136 assert(Pointee && "Cannot check if null pointer was initialized");
137 Descriptor *Desc = getFieldDesc();
138 if (Desc->isPrimitiveArray()) {
139 if (Pointee->IsStatic)
141 // Primitive array field are stored in a bitset.
142 InitMap *Map = getInitMap();
145 if (Map == (InitMap *)-1)
147 return Map->isInitialized(getIndex());
149 // Field has its bit in an inline descriptor.
150 return Base == 0 || getInlineDesc()->IsInitialized;
154 void Pointer::initialize() const {
155 assert(Pointee && "Cannot initialize null pointer");
156 Descriptor *Desc = getFieldDesc();
157 if (Desc->isPrimitiveArray()) {
158 if (!Pointee->IsStatic) {
159 // Primitive array initializer.
160 InitMap *&Map = getInitMap();
161 if (Map == (InitMap *)-1)
164 Map = InitMap::allocate(Desc->getNumElems());
165 if (Map->initialize(getIndex())) {
171 // Field has its bit in an inline descriptor.
172 assert(Base != 0 && "Only composite fields can be initialised");
173 getInlineDesc()->IsInitialized = true;
177 void Pointer::activate() const {
178 // Field has its bit in an inline descriptor.
179 assert(Base != 0 && "Only composite fields can be initialised");
180 getInlineDesc()->IsActive = true;
183 void Pointer::deactivate() const {
184 // TODO: this only appears in constructors, so nothing to deactivate.
187 bool Pointer::hasSameBase(const Pointer &A, const Pointer &B) {
188 return A.Pointee == B.Pointee;
191 bool Pointer::hasSameArray(const Pointer &A, const Pointer &B) {
192 return A.Base == B.Base && A.getFieldDesc()->IsArray;