blob: d0e78deb325633be45fac2b4146bc0ae4d0adc5a [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2020 The Dawn & Tint Authors
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ryan Harrisondbc13af2022-02-21 15:19:07 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ryan Harrisondbc13af2022-02-21 15:19:07 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027
dan sinclair99181d82023-07-20 01:14:15 +000028#include "src/tint/lang/wgsl/ast/struct.h"
dan sinclair99181d82023-07-20 01:14:15 +000029#include "src/tint/lang/wgsl/ast/alias.h"
Ben Claytond368f2c2023-08-01 00:37:35 +000030#include "src/tint/lang/wgsl/ast/helper_test.h"
dan sinclair99181d82023-07-20 01:14:15 +000031#include "src/tint/lang/wgsl/ast/transform/add_block_attribute.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000032
dan sinclair34323ac2022-04-07 18:39:35 +000033namespace tint::ast {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000034namespace {
35
36using AstStructTest = TestHelper;
Stephen White863d9ed2022-09-02 19:19:10 +000037using BlockAttribute = transform::AddBlockAttribute::BlockAttribute;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000038
39TEST_F(AstStructTest, Creation) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000040 auto name = Sym("s");
dan sinclairbae54e72023-07-28 15:01:54 +000041 auto* s = Structure(name, tint::Vector{Member("a", ty.i32())});
Ben Claytonb75252b2023-02-09 10:34:14 +000042 EXPECT_EQ(s->name->symbol, name);
Ben Clayton783b1692022-08-02 17:03:35 +000043 EXPECT_EQ(s->members.Length(), 1u);
44 EXPECT_TRUE(s->attributes.IsEmpty());
dan sinclair41e4d9a2022-05-01 14:40:55 +000045 EXPECT_EQ(s->source.range.begin.line, 0u);
46 EXPECT_EQ(s->source.range.begin.column, 0u);
47 EXPECT_EQ(s->source.range.end.line, 0u);
48 EXPECT_EQ(s->source.range.end.column, 0u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000049}
50
51TEST_F(AstStructTest, Creation_WithAttributes) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000052 auto name = Sym("s");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000053
dan sinclairbae54e72023-07-28 15:01:54 +000054 auto* s = Structure(name, tint::Vector{Member("a", ty.i32())},
55 tint::Vector{
Ben Claytonb75252b2023-02-09 10:34:14 +000056 ASTNodes().Create<BlockAttribute>(ID(), AllocateNodeID()),
57 });
58 EXPECT_EQ(s->name->symbol, name);
Ben Clayton783b1692022-08-02 17:03:35 +000059 EXPECT_EQ(s->members.Length(), 1u);
60 ASSERT_EQ(s->attributes.Length(), 1u);
Stephen White863d9ed2022-09-02 19:19:10 +000061 EXPECT_TRUE(s->attributes[0]->Is<BlockAttribute>());
dan sinclair41e4d9a2022-05-01 14:40:55 +000062 EXPECT_EQ(s->source.range.begin.line, 0u);
63 EXPECT_EQ(s->source.range.begin.column, 0u);
64 EXPECT_EQ(s->source.range.end.line, 0u);
65 EXPECT_EQ(s->source.range.end.column, 0u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000066}
67
68TEST_F(AstStructTest, CreationWithSourceAndAttributes) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000069 auto name = Sym("s");
Ben Claytonb75252b2023-02-09 10:34:14 +000070 auto* s = Structure(Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 8}}},
dan sinclairbae54e72023-07-28 15:01:54 +000071 name, tint::Vector{Member("a", ty.i32())},
72 tint::Vector{ASTNodes().Create<BlockAttribute>(ID(), AllocateNodeID())});
Ben Claytonb75252b2023-02-09 10:34:14 +000073 EXPECT_EQ(s->name->symbol, name);
Ben Clayton783b1692022-08-02 17:03:35 +000074 EXPECT_EQ(s->members.Length(), 1u);
75 ASSERT_EQ(s->attributes.Length(), 1u);
Stephen White863d9ed2022-09-02 19:19:10 +000076 EXPECT_TRUE(s->attributes[0]->Is<BlockAttribute>());
dan sinclair41e4d9a2022-05-01 14:40:55 +000077 EXPECT_EQ(s->source.range.begin.line, 27u);
78 EXPECT_EQ(s->source.range.begin.column, 4u);
79 EXPECT_EQ(s->source.range.end.line, 27u);
80 EXPECT_EQ(s->source.range.end.column, 8u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000081}
82
83TEST_F(AstStructTest, Assert_Null_StructMember) {
Ben Clayton7cdaffe2024-05-08 16:24:07 +000084 EXPECT_DEATH(
dan sinclair41e4d9a2022-05-01 14:40:55 +000085 {
86 ProgramBuilder b;
dan sinclairbae54e72023-07-28 15:01:54 +000087 b.Structure(b.Sym("S"), tint::Vector{b.Member("a", b.ty.i32()), nullptr}, tint::Empty);
dan sinclair41e4d9a2022-05-01 14:40:55 +000088 },
89 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000090}
91
92TEST_F(AstStructTest, Assert_Null_Attribute) {
Ben Clayton7cdaffe2024-05-08 16:24:07 +000093 EXPECT_DEATH(
dan sinclair41e4d9a2022-05-01 14:40:55 +000094 {
95 ProgramBuilder b;
dan sinclairbae54e72023-07-28 15:01:54 +000096 b.Structure(b.Sym("S"), tint::Vector{b.Member("a", b.ty.i32())},
97 tint::Vector<const Attribute*, 1>{nullptr});
dan sinclair41e4d9a2022-05-01 14:40:55 +000098 },
99 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000100}
101
dan sinclair637a2fe2023-07-24 21:11:41 +0000102TEST_F(AstStructTest, Assert_DifferentGenerationID_StructMember) {
Ben Clayton7cdaffe2024-05-08 16:24:07 +0000103 EXPECT_DEATH(
dan sinclair41e4d9a2022-05-01 14:40:55 +0000104 {
105 ProgramBuilder b1;
106 ProgramBuilder b2;
dan sinclairbae54e72023-07-28 15:01:54 +0000107 b1.Structure(b1.Sym("S"), tint::Vector{b2.Member("a", b2.ty.i32())});
dan sinclair41e4d9a2022-05-01 14:40:55 +0000108 },
109 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000110}
111
dan sinclair637a2fe2023-07-24 21:11:41 +0000112TEST_F(AstStructTest, Assert_DifferentGenerationID_Attribute) {
Ben Clayton7cdaffe2024-05-08 16:24:07 +0000113 EXPECT_DEATH(
dan sinclair41e4d9a2022-05-01 14:40:55 +0000114 {
115 ProgramBuilder b1;
116 ProgramBuilder b2;
Ben Claytonb75252b2023-02-09 10:34:14 +0000117 b1.Structure(
dan sinclairbae54e72023-07-28 15:01:54 +0000118 b1.Sym("S"), tint::Vector{b1.Member("a", b1.ty.i32())},
119 tint::Vector{b2.ASTNodes().Create<BlockAttribute>(b2.ID(), b2.AllocateNodeID())});
dan sinclair41e4d9a2022-05-01 14:40:55 +0000120 },
121 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000122}
123
124} // namespace
dan sinclair34323ac2022-04-07 18:39:35 +0000125} // namespace tint::ast