Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cpp/test/computed_fields_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ TEST(ComputedFieldsTest, SwitchExpression) {
r.optional_named_array = {};
ASSERT_EQ(r.OptionalNamedArrayLength(), 0);
ASSERT_EQ(r.OptionalNamedArrayLengthWithDiscard(), 0);
static_assert(std::is_same_v<decltype(r.OptionalNamedArrayLength()), size_t>);
static_assert(std::is_same_v<decltype(r.OptionalNamedArrayLengthWithDiscard()), size_t>);
static_assert(std::is_same_v<decltype(r.OptionalNamedArrayLength()), yardl::Size>);
static_assert(std::is_same_v<decltype(r.OptionalNamedArrayLengthWithDiscard()), yardl::Size>);

r.int_float_union = 42;
ASSERT_EQ(r.IntFloatUnionAsFloat(), 42.0f);
Expand Down
2 changes: 1 addition & 1 deletion cpp/test/generated/hdf5/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ namespace {

[[maybe_unused]] H5::EnumType GetSizeBasedEnumHdf5Ddl() {
H5::EnumType t(yardl::hdf5::SizeTypeDdl());
size_t i = 0ULL;
yardl::Size i = 0ULL;
t.insert("a", &i);
i = 1ULL;
t.insert("b", &i);
Expand Down
50 changes: 25 additions & 25 deletions cpp/test/generated/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct RecordWithPrimitives {
uint32_t uint32_field{};
int64_t int64_field{};
uint64_t uint64_field{};
size_t size_field{};
yardl::Size size_field{};
float float32_field{};
double float64_field{};
std::complex<float> complexfloat32_field{};
Expand Down Expand Up @@ -362,7 +362,7 @@ enum class Int64Enum : int64_t {
kB = -4611686018427387904LL,
};

enum class SizeBasedEnum : size_t {
enum class SizeBasedEnum : yardl::Size {
kA = 0ULL,
kB = 1ULL,
kC = 2ULL,
Expand Down Expand Up @@ -590,92 +590,92 @@ struct RecordWithComputedFields {
return const_cast<int32_t&>(std::as_const(*this).AccessVectorOfVectorsField());
}

size_t ArraySize() const {
yardl::Size ArraySize() const {
return array_field.size();
}

size_t ArrayXSize() const {
yardl::Size ArrayXSize() const {
return array_field.shape(0);
}

size_t ArrayYSize() const {
yardl::Size ArrayYSize() const {
return array_field.shape(1);
}

size_t Array0Size() const {
yardl::Size Array0Size() const {
return array_field.shape(0);
}

size_t Array1Size() const {
yardl::Size Array1Size() const {
return array_field.shape(1);
}

size_t ArraySizeFromIntField() const {
yardl::Size ArraySizeFromIntField() const {
return array_field.shape(int_field);
}

size_t ArraySizeFromStringField() const {
yardl::Size ArraySizeFromStringField() const {
return array_field.shape(([](std::string dim_name) {
if (dim_name == "x") return 0;
if (dim_name == "y") return 1;
throw std::invalid_argument("Unknown dimension name: " + dim_name);
})(string_field));
}

size_t ArraySizeFromNestedIntField() const {
yardl::Size ArraySizeFromNestedIntField() const {
return array_field.shape(tuple_field.v1);
}

size_t ArrayFieldMapDimensionsXSize() const {
yardl::Size ArrayFieldMapDimensionsXSize() const {
return array_field_map_dimensions.shape(0);
}

size_t FixedArraySize() const {
yardl::Size FixedArraySize() const {
return 12ULL;
}

size_t FixedArrayXSize() const {
yardl::Size FixedArrayXSize() const {
return 3ULL;
}

size_t FixedArray0Size() const {
yardl::Size FixedArray0Size() const {
return 3ULL;
}

size_t VectorSize() const {
yardl::Size VectorSize() const {
return vector_field.size();
}

size_t FixedVectorSize() const {
yardl::Size FixedVectorSize() const {
return 3ULL;
}

size_t ArrayDimensionXIndex() const {
yardl::Size ArrayDimensionXIndex() const {
return 0ULL;
}

size_t ArrayDimensionYIndex() const {
yardl::Size ArrayDimensionYIndex() const {
return 1ULL;
}

size_t ArrayDimensionIndexFromStringField() const {
yardl::Size ArrayDimensionIndexFromStringField() const {
return ([](std::string dim_name) {
if (dim_name == "x") return 0;
if (dim_name == "y") return 1;
throw std::invalid_argument("Unknown dimension name: " + dim_name);
})(string_field);
}

size_t ArrayDimensionCount() const {
yardl::Size ArrayDimensionCount() const {
return 2ULL;
}

size_t DynamicArrayDimensionCount() const {
yardl::Size DynamicArrayDimensionCount() const {
return dynamic_array_field.dimension();
}

size_t OptionalNamedArrayLength() const {
return [](auto&& __case_arg__) -> size_t {
yardl::Size OptionalNamedArrayLength() const {
return [](auto&& __case_arg__) -> yardl::Size {
if (__case_arg__.has_value()) {
test_model::NamedNDArray const& arr = __case_arg__.value();
return arr.size();
Expand All @@ -684,8 +684,8 @@ struct RecordWithComputedFields {
}(optional_named_array);
}

size_t OptionalNamedArrayLengthWithDiscard() const {
return [](auto&& __case_arg__) -> size_t {
yardl::Size OptionalNamedArrayLengthWithDiscard() const {
return [](auto&& __case_arg__) -> yardl::Size {
if (__case_arg__.has_value()) {
test_model::NamedNDArray const& arr = __case_arg__.value();
return arr.size();
Expand Down
4 changes: 2 additions & 2 deletions cpp/test/roundtrip_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST_P(RoundTripTests, Scalars) {
rec.uint32_field = 55;
rec.int64_field = -66;
rec.uint64_field = 66;
rec.size_field = sizeof(size_t);
rec.size_field = UINT64_MAX;
rec.float32_field = 4290.39;
rec.float64_field = 2234290.39;
rec.complexfloat32_field = {1.3, 2.2};
Expand Down Expand Up @@ -560,7 +560,7 @@ INSTANTIATE_TEST_SUITE_P(,
::testing::Values(
Format::kBinary,
Format::kHdf5),
[](const ::testing::TestParamInfo<Format>& info) {
[](::testing::TestParamInfo<Format> const& info) {
switch (info.param) {
case Format::kBinary:
return "Binary";
Expand Down
4 changes: 3 additions & 1 deletion tooling/internal/cpp/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,10 @@ func TypeDefinitionSyntax(t dsl.TypeDefinition) string {

func PrimitiveSyntax(p dsl.PrimitiveDefinition) string {
switch p {
case dsl.Int8, dsl.Uint8, dsl.Int16, dsl.Uint16, dsl.Int32, dsl.Uint32, dsl.Int64, dsl.Uint64, dsl.Size:
case dsl.Int8, dsl.Uint8, dsl.Int16, dsl.Uint16, dsl.Int32, dsl.Uint32, dsl.Int64, dsl.Uint64:
return string(p) + "_t"
case dsl.Size:
return "yardl::Size"
case dsl.Float32:
return "float"
case dsl.Float64:
Expand Down
2 changes: 1 addition & 1 deletion tooling/internal/cpp/include/detail/hdf5/ddl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace yardl::hdf5 {

/**
* @brief Returns the HDF5 type for size_t.
* @brief Returns the HDF5 type for yardl::Size.
*/
static inline H5::PredType const& SizeTypeDdl() {
static_assert(sizeof(hsize_t) == sizeof(size_t));
Expand Down
5 changes: 5 additions & 0 deletions tooling/internal/cpp/include/yardl.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ using Time = std::chrono::duration<int64_t, std::nano>;
using DateTime = std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<int64_t, std::nano>>;

/**
* @brief The same as size_t when it is 64 bits, otherwise uint64_t.
*/
using Size = std::conditional_t<sizeof(size_t) == sizeof(uint64_t), size_t, uint64_t>;

} // namespace yardl
16 changes: 8 additions & 8 deletions tooling/pkg/dsl/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (rewriter RewriterWithContext[T]) DefaultRewrite(node Node, context T) Node
rewrittenEnv.Namespaces = rewrittenNamespaces
return &rewrittenEnv
case *Namespace:
rewrittenTypes := rewriteIntefaceSlice(t.TypeDefinitions, context, rewriter)
rewrittenTypes := rewriteInterfaceSlice(t.TypeDefinitions, context, rewriter)
rewrittenProtocols := rewriteSlice(t.Protocols, context, rewriter)

if rewrittenTypes == nil && rewrittenProtocols == nil {
Expand Down Expand Up @@ -209,7 +209,7 @@ func (rewriter RewriterWithContext[T]) DefaultRewrite(node Node, context T) Node
case *GenericTypeParameter:
return t
case *SimpleType:
rewrittenTypeArguments := rewriteIntefaceSlice(t.TypeArguments, context, rewriter)
rewrittenTypeArguments := rewriteInterfaceSlice(t.TypeArguments, context, rewriter)
if rewrittenTypeArguments == nil {
return t
}
Expand All @@ -218,7 +218,7 @@ func (rewriter RewriterWithContext[T]) DefaultRewrite(node Node, context T) Node
rewrittenSimpleType.TypeArguments = rewrittenTypeArguments
return &rewrittenSimpleType
case *GeneralizedType:
rewrittenTypeCases := rewriteIntefaceSlice(t.Cases, context, rewriter)
rewrittenTypeCases := rewriteInterfaceSlice(t.Cases, context, rewriter)

var rewrittenDimensionality Dimensionality
if t.Dimensionality != nil {
Expand Down Expand Up @@ -283,7 +283,7 @@ func (rewriter RewriterWithContext[T]) DefaultRewrite(node Node, context T) Node
rewrittenTarget = rewriter.Rewrite(t.Target, context).(Expression)
}

rewrittenArguments := rewriteIntefaceSlice(t.Arguments, context, rewriter)
rewrittenArguments := rewriteInterfaceSlice(t.Arguments, context, rewriter)

if rewrittenTarget == t.Target && rewrittenArguments == nil {
return t
Expand All @@ -306,7 +306,7 @@ func (rewriter RewriterWithContext[T]) DefaultRewrite(node Node, context T) Node
return &rewrittenArgument

case *FunctionCallExpression:
rewrittenArguments := rewriteIntefaceSlice(t.Arguments, context, rewriter)
rewrittenArguments := rewriteInterfaceSlice(t.Arguments, context, rewriter)

if rewrittenArguments == nil {
return t
Expand Down Expand Up @@ -379,7 +379,7 @@ func (rewriter RewriterWithContext[T]) DefaultRewrite(node Node, context T) Node
}
}

// Rewites a slice of pointers to types that implement the Node interface, e.g, []*Field
// Rewrites a slice of pointers to types that implement the Node interface, e.g, []*Field
// Returns nil if no changes were made and the original slice should be used.
func rewriteSlice[TContext any, TElement any, T interface {
*TElement
Expand All @@ -402,9 +402,9 @@ func rewriteSlice[TContext any, TElement any, T interface {
return rewrittenSlice
}

// Rewites a slice of an interface that implements the Node interface, e.g, []Expression
// Rewrites a slice of an interface that implements the Node interface, e.g, []Expression
// Returns nil if no changes were made and the original slice should be used.
func rewriteIntefaceSlice[TContext any, T Node](slice []T, context TContext, rewriter RewriterWithContext[TContext]) []T {
func rewriteInterfaceSlice[TContext any, T Node](slice []T, context TContext, rewriter RewriterWithContext[TContext]) []T {
var rewrittenSlice []T
for i, element := range slice {
visited := rewriter.Rewrite(T(element), context)
Expand Down
9 changes: 9 additions & 0 deletions tooling/pkg/dsl/typefunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func TypeDefinitionsEqual(a, b TypeDefinition) bool {
aMeta := a.GetDefinitionMeta()
bMeta := b.GetDefinitionMeta()
if aMeta.Namespace != bMeta.Namespace || aMeta.Name != bMeta.Name {
if a == PrimitiveSize && b == PrimitiveUint64 || a == PrimitiveUint64 && b == PrimitiveSize {
// Special case: `size` and `uint64` are equivalent though not aliases
return true
}

return false
}

Expand Down Expand Up @@ -185,6 +190,10 @@ func TypesEqual(a, b Type) bool {
if b == nil {
return a == nil
}

a = GetUnderlyingType(a)
b = GetUnderlyingType(b)

switch ta := a.(type) {
case *SimpleType:
tb, ok := b.(*SimpleType)
Expand Down
13 changes: 9 additions & 4 deletions tooling/pkg/dsl/typeparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
)

type simpleTypeTree struct {
Name string `json:"name"`
TypeArguments []simpleTypeTree `json:"args,omitempty"`
Optional bool `json:"optional,omitempty"`
Name string `json:"name"`
TypeArguments []simpleTypeTree `json:"args,omitempty"`
Optional bool `json:"optional,omitempty"`
PositionOffset int `json:"positionOffset,omitempty"`
}

func (pt *simpleTypeTree) String() string {
Expand Down Expand Up @@ -67,6 +68,7 @@ func (tp *typeParser) consumeIdentifier() string {
func (tp *typeParser) parseTypeString() (simpleTypeTree, error) {
parsed := simpleTypeTree{}
tp.skipWhitespace()
parsed.PositionOffset = tp.position
parsed.Name = tp.consumeIdentifier()
tp.skipWhitespace()

Expand Down Expand Up @@ -154,7 +156,10 @@ func parseSimpleTypeStringAllowingRemaining(typeString string) (typeTree simpleT
}

func (tree simpleTypeTree) ToType(node NodeMeta) Type {
simpleType := SimpleType{NodeMeta: node, Name: tree.Name}
nodeWithPositionUpdated := node
nodeWithPositionUpdated.Column += tree.PositionOffset

simpleType := SimpleType{NodeMeta: nodeWithPositionUpdated, Name: tree.Name}
for _, typeArg := range tree.TypeArguments {
simpleType.TypeArguments = append(simpleType.TypeArguments, typeArg.ToType(node))
}
Expand Down
18 changes: 9 additions & 9 deletions tooling/pkg/dsl/typeparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ func TestTypeParsing_Valid(t *testing.T) {
}{
{input: "Foo", expected: `{"name":"Foo"}`},
{input: "Foo ", expected: `{"name":"Foo"}`},
{input: " Foo ", expected: `{"name":"Foo"}`},
{input: " Foo ", expected: `{"name":"Foo","positionOffset":1}`},
{input: "Foo?", expected: `{"name":"Foo","optional":true}`},
{input: "Foo ? ", expected: `{"name":"Foo","optional":true}`},
{input: "Foo<int>", expected: `{"name":"Foo","args":[{"name":"int"}]}`},
{input: "Foo< int >", expected: `{"name":"Foo","args":[{"name":"int"}]}`},
{input: "Foo<int?>", expected: `{"name":"Foo","args":[{"name":"int","optional":true}]}`},
{input: "Foo<int>?", expected: `{"name":"Foo","args":[{"name":"int"}],"optional":true}`},
{input: "Foo<int,float>", expected: `{"name":"Foo","args":[{"name":"int"},{"name":"float"}]}`},
{input: " Foo < int , float > ", expected: `{"name":"Foo","args":[{"name":"int"},{"name":"float"}]}`},
{input: "Foo<Bar<int>>", expected: `{"name":"Foo","args":[{"name":"Bar","args":[{"name":"int"}]}]}`},
{input: "Foo<Bar<int>,Baz<long>>", expected: `{"name":"Foo","args":[{"name":"Bar","args":[{"name":"int"}]},{"name":"Baz","args":[{"name":"long"}]}]}`},
{input: "Foo<int>", expected: `{"name":"Foo","args":[{"name":"int","positionOffset":4}]}`},
{input: "Foo< int >", expected: `{"name":"Foo","args":[{"name":"int","positionOffset":5}]}`},
{input: "Foo<int?>", expected: `{"name":"Foo","args":[{"name":"int","optional":true,"positionOffset":4}]}`},
{input: "Foo<int>?", expected: `{"name":"Foo","args":[{"name":"int","positionOffset":4}],"optional":true}`},
{input: "Foo<int,float>", expected: `{"name":"Foo","args":[{"name":"int","positionOffset":4},{"name":"float","positionOffset":8}]}`},
{input: " Foo < int , float > ", expected: `{"name":"Foo","args":[{"name":"int","positionOffset":7},{"name":"float","positionOffset":13}],"positionOffset":1}`},
{input: "Foo<Bar<int>>", expected: `{"name":"Foo","args":[{"name":"Bar","args":[{"name":"int","positionOffset":8}],"positionOffset":4}]}`},
{input: "Foo<Bar<int>,Baz<long>>", expected: `{"name":"Foo","args":[{"name":"Bar","args":[{"name":"int","positionOffset":8}],"positionOffset":4},{"name":"Baz","args":[{"name":"long","positionOffset":17}],"positionOffset":13}]}`},
}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
Expand Down
13 changes: 12 additions & 1 deletion tooling/pkg/dsl/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ type NodeMeta struct {
Column int `json:"-"`
}

func (n *NodeMeta) String() string {
return fmt.Sprintf("%s:%d:%d", n.File, n.Line, n.Column)
}

func (n *NodeMeta) Equals(other *NodeMeta) bool {
return n == other || (n != nil && other != nil &&
n.File == other.File &&
n.Line == other.Line &&
n.Column == other.Column)
}

func (n *NodeMeta) GetNodeMeta() *NodeMeta {
return n
}
Expand Down Expand Up @@ -605,7 +616,7 @@ var (

_ TypeDefinition = (*RecordDefinition)(nil)
_ TypeDefinition = (*EnumDefinition)(nil)
_ TypeDefinition = (*PrimitiveDefinition)(nil)
_ TypeDefinition = (PrimitiveDefinition)("")
_ TypeDefinition = (*NamedType)(nil)
_ TypeDefinition = (*ProtocolDefinition)(nil)
_ TypeDefinition = (*GenericTypeParameter)(nil)
Expand Down
Loading