blob: e01d9e3c88fd9d97d2916f570b5693cb92c9e820 [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2023 The Dawn & Tint Authors
Ben Clayton3a3cb362023-08-16 01:05:21 +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:
Ben Clayton3a3cb362023-08-16 01:05:21 +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.
Ben Clayton3a3cb362023-08-16 01:05:21 +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.
Ben Clayton3a3cb362023-08-16 01:05:21 +000027
28#include "src/tint/utils/reflection/reflection.h"
29
Ben Claytonf1dc9e32024-02-08 17:47:19 +000030#include <memory>
31#include <string>
Ben Clayton7803cd72023-08-16 19:21:25 +000032
Ben Claytonf1dc9e32024-02-08 17:47:19 +000033#include "src/tint/utils/math/math.h"
34#include "src/tint/utils/text/string_stream.h"
35
36namespace tint::reflection::detail {
37
38Result<SuccessType> CheckAllFieldsReflected(VectorRef<ReflectedFieldInfo> fields,
39 std::string_view class_name,
40 size_t class_size,
41 size_t class_align,
42 bool class_is_castable,
43 std::string_view reflect_file,
44 uint32_t reflect_line) {
45 size_t calculated_offset = class_is_castable ? sizeof(CastableBase) : 0;
46 for (auto& field : fields) {
47 calculated_offset = RoundUp(field.align, calculated_offset);
48 if (calculated_offset < field.offset) {
49 StringStream msg;
50 msg << "TINT_REFLECT(" << class_name << ", ...) field mismatch at '" << field.name
51 << "'.\n"
52 "Expected field offset of "
53 << calculated_offset << " bytes, but field was at " << field.offset << " bytes";
54 diag::Diagnostic err;
55 err.message = msg.str();
56 err.owned_file = std::make_shared<Source::File>(std::string(reflect_file), "");
57 err.source.file = err.owned_file.get();
58 err.source.range.begin.line = reflect_line;
59 err.source.range.end.line = reflect_line;
60 return Failure{std::move(err)};
61 }
62 calculated_offset += field.size;
63 }
64 calculated_offset = RoundUp(class_align, calculated_offset);
65 if (calculated_offset != class_size) {
66 StringStream msg;
67 msg << "TINT_REFLECT(" << class_name
68 << ", ...) missing fields at end of class\n"
69 "Expected class size of "
70 << calculated_offset << " bytes, but class is " << class_size << " bytes";
71 diag::Diagnostic err;
72 err.message = msg.str();
73 err.owned_file = std::make_shared<Source::File>(std::string(reflect_file), "");
74 err.source.file = err.owned_file.get();
75 err.source.range.begin.line = reflect_line;
76 err.source.range.end.line = reflect_line;
77 return Failure{std::move(err)};
78 }
79 return Success;
80}
81
82} // namespace tint::reflection::detail