================
@@ -0,0 +1,274 @@
+#include "../../../lib/AST/ByteCode/Context.h"
+#include "../../../lib/AST/ByteCode/Descriptor.h"
+#include "../../../lib/AST/ByteCode/Integral.h"
+#include "../../../lib/AST/ByteCode/Program.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Tooling/Tooling.h"
+#include "gtest/gtest.h"
+
+using namespace clang;
+using namespace clang::interp;
+using namespace clang::ast_matchers;
+
+TEST(Pointer, TypesRecord) {
+ constexpr char Code[] = "struct A { bool a; bool b; };\n"
+ "constexpr A arr[3][2] = {\n"
+ " {{ false, false }, {false, true} },\n"
+ " {{ false, false }, {false, true} },\n"
+ " {{ false, false }, {false, true} },\n"
+ "};\n";
+
+ auto AST = tooling::buildASTFromCodeWithArgs(
+ Code, {"-fexperimental-new-constant-interpreter"});
+ ASTContext &ASTCtx = AST->getASTContext();
+ const VarDecl *D =
+ selectFirst<VarDecl>("arr", match(varDecl().bind("arr"), ASTCtx));
+ ASSERT_NE(D, nullptr);
+
+ const auto &Ctx = AST->getASTContext().getInterpContext();
+ Program &Prog = Ctx.getProgram();
+ // Global is registered.
+ ASSERT_TRUE(Prog.getGlobal(D));
+
+ // Get a Pointer to the global.
+ const Pointer &GlobalPtr = Prog.getPtrGlobal(*Prog.getGlobal(D));
+
+ // Type of the global ptr should be A[][]
+ {
+ QualType T = GlobalPtr.getType();
+ ASSERT_TRUE(T->isArrayType());
+ const auto *ArrTy = cast<ConstantArrayType>(T->getAsArrayTypeUnsafe());
+ ASSERT_NE(ArrTy, nullptr);
+ ASSERT_EQ(ArrTy->getZExtSize(), (uint64_t)3);
+
+ QualType ElemTy = ArrTy->getElementType();
+ const auto *ElemArrTy =
+ cast<ConstantArrayType>(ElemTy->getAsArrayTypeUnsafe());
+ ASSERT_NE(ElemArrTy, nullptr);
+ ASSERT_EQ(ElemArrTy->getZExtSize(), (uint64_t)2);
+ }
+
+ // This is still A[][] because we didn't narrow().
+ {
+ Pointer Elem = GlobalPtr.atIndex(0);
----------------
efriedma-quic wrote:
Let me see if I understand this correctly... atIndex constructs an "array
element pointer". This is something which shouldn't naturally exist in C++ for
an array of non-primitive type: a pointer operation in C++ will always return a
narrowed pointer.
For an array of primitive type, we don't narrow; we just keep the pointer in
"array element" form.
https://github.com/llvm/llvm-project/pull/200342
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits