Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Dawn & Tint Authors |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 28 | #include "src/tint/lang/wgsl/ast/struct.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 29 | #include "src/tint/lang/wgsl/ast/alias.h" |
Ben Clayton | d368f2c | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 30 | #include "src/tint/lang/wgsl/ast/helper_test.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 31 | #include "src/tint/lang/wgsl/ast/transform/add_block_attribute.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 32 | |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 33 | namespace tint::ast { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | using AstStructTest = TestHelper; |
Stephen White | 863d9ed | 2022-09-02 19:19:10 +0000 | [diff] [blame] | 37 | using BlockAttribute = transform::AddBlockAttribute::BlockAttribute; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 38 | |
| 39 | TEST_F(AstStructTest, Creation) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 40 | auto name = Sym("s"); |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 41 | auto* s = Structure(name, tint::Vector{Member("a", ty.i32())}); |
Ben Clayton | b75252b | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 42 | EXPECT_EQ(s->name->symbol, name); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 43 | EXPECT_EQ(s->members.Length(), 1u); |
| 44 | EXPECT_TRUE(s->attributes.IsEmpty()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 45 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST_F(AstStructTest, Creation_WithAttributes) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 52 | auto name = Sym("s"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 53 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 54 | auto* s = Structure(name, tint::Vector{Member("a", ty.i32())}, |
| 55 | tint::Vector{ |
Ben Clayton | b75252b | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 56 | ASTNodes().Create<BlockAttribute>(ID(), AllocateNodeID()), |
| 57 | }); |
| 58 | EXPECT_EQ(s->name->symbol, name); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 59 | EXPECT_EQ(s->members.Length(), 1u); |
| 60 | ASSERT_EQ(s->attributes.Length(), 1u); |
Stephen White | 863d9ed | 2022-09-02 19:19:10 +0000 | [diff] [blame] | 61 | EXPECT_TRUE(s->attributes[0]->Is<BlockAttribute>()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 62 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | TEST_F(AstStructTest, CreationWithSourceAndAttributes) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 69 | auto name = Sym("s"); |
Ben Clayton | b75252b | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 70 | auto* s = Structure(Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 8}}}, |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 71 | name, tint::Vector{Member("a", ty.i32())}, |
| 72 | tint::Vector{ASTNodes().Create<BlockAttribute>(ID(), AllocateNodeID())}); |
Ben Clayton | b75252b | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 73 | EXPECT_EQ(s->name->symbol, name); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 74 | EXPECT_EQ(s->members.Length(), 1u); |
| 75 | ASSERT_EQ(s->attributes.Length(), 1u); |
Stephen White | 863d9ed | 2022-09-02 19:19:10 +0000 | [diff] [blame] | 76 | EXPECT_TRUE(s->attributes[0]->Is<BlockAttribute>()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 77 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | TEST_F(AstStructTest, Assert_Null_StructMember) { |
Ben Clayton | 7cdaffe | 2024-05-08 16:24:07 +0000 | [diff] [blame^] | 84 | EXPECT_DEATH( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 85 | { |
| 86 | ProgramBuilder b; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 87 | b.Structure(b.Sym("S"), tint::Vector{b.Member("a", b.ty.i32()), nullptr}, tint::Empty); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 88 | }, |
| 89 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST_F(AstStructTest, Assert_Null_Attribute) { |
Ben Clayton | 7cdaffe | 2024-05-08 16:24:07 +0000 | [diff] [blame^] | 93 | EXPECT_DEATH( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 94 | { |
| 95 | ProgramBuilder b; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 96 | b.Structure(b.Sym("S"), tint::Vector{b.Member("a", b.ty.i32())}, |
| 97 | tint::Vector<const Attribute*, 1>{nullptr}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 98 | }, |
| 99 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 100 | } |
| 101 | |
dan sinclair | 637a2fe | 2023-07-24 21:11:41 +0000 | [diff] [blame] | 102 | TEST_F(AstStructTest, Assert_DifferentGenerationID_StructMember) { |
Ben Clayton | 7cdaffe | 2024-05-08 16:24:07 +0000 | [diff] [blame^] | 103 | EXPECT_DEATH( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 104 | { |
| 105 | ProgramBuilder b1; |
| 106 | ProgramBuilder b2; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 107 | b1.Structure(b1.Sym("S"), tint::Vector{b2.Member("a", b2.ty.i32())}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 108 | }, |
| 109 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 110 | } |
| 111 | |
dan sinclair | 637a2fe | 2023-07-24 21:11:41 +0000 | [diff] [blame] | 112 | TEST_F(AstStructTest, Assert_DifferentGenerationID_Attribute) { |
Ben Clayton | 7cdaffe | 2024-05-08 16:24:07 +0000 | [diff] [blame^] | 113 | EXPECT_DEATH( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 114 | { |
| 115 | ProgramBuilder b1; |
| 116 | ProgramBuilder b2; |
Ben Clayton | b75252b | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 117 | b1.Structure( |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 118 | b1.Sym("S"), tint::Vector{b1.Member("a", b1.ty.i32())}, |
| 119 | tint::Vector{b2.ASTNodes().Create<BlockAttribute>(b2.ID(), b2.AllocateNodeID())}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 120 | }, |
| 121 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | } // namespace |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 125 | } // namespace tint::ast |