Files
carbon-lang/common/struct_reflection_test.cpp
T
2e97f27b8d Typed wrappers around parse tree nodes (#3534)
These are intended to allow the structure of a parse tree node to be
described more precisely in code, to support these use cases:

- Automated checking that the parse tree conforms to the expected
structure. (Added to `Tree::Verify`.)
- Easier reading and understanding of the structure of the parse tree by
toolchain developers. (See `parse/typed_nodes.h`.)
- Easier navigation of the parse tree, for example for tooling uses and
for use when forming diagnostics.

On this last point, an object representing the file may be inspecting
using `Tree::ExtractFile`, as in:
```
auto file = tree->ExtractFile();
for (AnyDeclId decl_id : file.decls) {
  // `decl_id` is convertible to a `NodeId`.
  if (std::optional<FunctionDecl> fn_decl =
      tree->ExtractAs<FunctionDecl>(decl_id)) {
    // fn_decl->params is a `TuplePatternId` (which extends `NodeId`)
    // that is guaranteed to reference a `TuplePattern`.
    std::optional<TuplePattern> params = tree->Extract(fn_decl->params);
    // `params` has a value unless there was an error in that node.
  } else if (auto class_def = tree->ExtractAs<ClassDefinition>(decl_id)) {
    // ...
  }
}
```

The `Extract...` functions collect the child nodes into the typed parse
node's fields (internally using a `Tree::SiblingIterator`) for easy
access. However, this is not as fast as directly observing the tree
structure using the postorder strategy being used by the check stage.

These functions rely on using struct reflection on the typed parse node
definitions from `parse/typed_nodes.h` to get the expected structure of
child nodes and then populate them.

Note that validating these in `Tree::Verify` adds significant cost to
it, and is currently included in the parsing stage. Without this change,
a 10 mloc test case of lex & parse takes 4.129 s ± 0.041 s. With this
change, it takes 5.768 s ± 0.036 s.

This builds upon and completes #3393.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2023-12-22 22:14:11 +00:00

120 lines
3.2 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "common/struct_reflection.h"
#include <gtest/gtest.h>
namespace Carbon::StructReflection {
namespace {
struct ZeroFields {};
struct OneField {
int x;
};
struct TwoFields {
int x;
int y;
};
struct SixFields {
int one;
int two;
int three;
int four;
int five;
int six;
};
struct ReferenceField {
int& ref;
};
struct NoDefaultConstructor {
explicit NoDefaultConstructor(int n) : v(n) {}
int v;
};
struct OneFieldNoDefaultConstructor {
NoDefaultConstructor x;
};
struct TwoFieldsNoDefaultConstructor {
NoDefaultConstructor x;
NoDefaultConstructor y;
};
TEST(StructReflectionTest, CanListInitialize) {
{
using Type = OneField;
using Field = Internal::AnyField<Type>;
static_assert(Internal::CanListInitialize<Type>(nullptr));
static_assert(Internal::CanListInitialize<Type, Field>(nullptr));
static_assert(!Internal::CanListInitialize<Type, Field, Field>(0));
}
{
using Type = OneFieldNoDefaultConstructor;
using Field = Internal::AnyField<Type>;
static_assert(!Internal::CanListInitialize<Type>(0));
static_assert(Internal::CanListInitialize<Type, Field>(nullptr));
static_assert(!Internal::CanListInitialize<Type, Field, Field>(0));
}
}
TEST(StructReflectionTest, CountFields) {
static_assert(Internal::CountFields<ZeroFields>() == 0);
static_assert(Internal::CountFields<OneField>() == 1);
static_assert(Internal::CountFields<TwoFields>() == 2);
static_assert(Internal::CountFields<SixFields>() == 6);
static_assert(Internal::CountFields<ReferenceField>() == 1);
static_assert(Internal::CountFields<OneFieldNoDefaultConstructor>() == 1);
}
TEST(StructReflectionTest, EmptyStruct) {
std::tuple<> fields = AsTuple(ZeroFields());
static_cast<void>(fields);
}
TEST(StructReflectionTest, OneField) {
std::tuple<int> fields = AsTuple(OneField{.x = 1});
EXPECT_EQ(std::get<0>(fields), 1);
}
TEST(StructReflectionTest, TwoFields) {
std::tuple<int, int> fields = AsTuple(TwoFields{.x = 1, .y = 2});
EXPECT_EQ(std::get<0>(fields), 1);
EXPECT_EQ(std::get<1>(fields), 2);
}
TEST(StructReflectionTest, SixFields) {
std::tuple<int, int, int, int, int, int> fields = AsTuple(SixFields{
.one = 1, .two = 2, .three = 3, .four = 4, .five = 5, .six = 6});
EXPECT_EQ(std::get<0>(fields), 1);
EXPECT_EQ(std::get<1>(fields), 2);
EXPECT_EQ(std::get<2>(fields), 3);
EXPECT_EQ(std::get<3>(fields), 4);
EXPECT_EQ(std::get<4>(fields), 5);
EXPECT_EQ(std::get<5>(fields), 6);
}
TEST(StructReflectionTest, NoDefaultConstructor) {
std::tuple<NoDefaultConstructor, NoDefaultConstructor> fields =
AsTuple(TwoFieldsNoDefaultConstructor{.x = NoDefaultConstructor(1),
.y = NoDefaultConstructor(2)});
EXPECT_EQ(std::get<0>(fields).v, 1);
EXPECT_EQ(std::get<1>(fields).v, 2);
}
TEST(StructReflectionTest, ReferenceField) {
int n = 0;
std::tuple<int&> fields = AsTuple(ReferenceField{.ref = n});
EXPECT_EQ(&std::get<0>(fields), &n);
}
} // namespace
} // namespace Carbon::StructReflection