blob: 632d71ae9b85063392300cb073c1c62ef4e2fcec [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// Copyright 2020 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_TINT_AST_STRUCT_MEMBER_OFFSET_ATTRIBUTE_H_
16#define SRC_TINT_AST_STRUCT_MEMBER_OFFSET_ATTRIBUTE_H_
17
18#include <string>
19
20#include "src/tint/ast/attribute.h"
dan sinclair93df9672022-09-09 14:49:09 +000021#include "src/tint/ast/expression.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000022
dan sinclair34323ac2022-04-07 18:39:35 +000023namespace tint::ast {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000024
25/// A struct member offset attribute
26/// @note The WGSL spec removed the `@offset(n)` attribute for `@size(n)`
27/// and `@align(n)` in https://github.com/gpuweb/gpuweb/pull/1447. However
28/// this attribute is kept because the SPIR-V reader has to deal with absolute
29/// offsets, and transforming these to size / align is complex and can be done
30/// in a number of ways. The Resolver is responsible for consuming the size and
31/// align attributes and transforming these into absolute offsets. It is
32/// trivial for the Resolver to handle `@offset(n)` or `@size(n)` /
33/// `@align(n)` attributes, so this is what we do, keeping all the layout
34/// logic in one place.
dan sinclair41e4d9a2022-05-01 14:40:55 +000035class StructMemberOffsetAttribute final : public Castable<StructMemberOffsetAttribute, Attribute> {
36 public:
37 /// constructor
38 /// @param pid the identifier of the program that owns this node
Ben Clayton4a92a3c2022-07-18 20:50:02 +000039 /// @param nid the unique node identifier
dan sinclair41e4d9a2022-05-01 14:40:55 +000040 /// @param src the source of this node
dan sinclair93df9672022-09-09 14:49:09 +000041 /// @param expr the offset expression
42 StructMemberOffsetAttribute(ProgramID pid,
43 NodeID nid,
44 const Source& src,
45 const ast::Expression* expr);
dan sinclair41e4d9a2022-05-01 14:40:55 +000046 ~StructMemberOffsetAttribute() override;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000047
dan sinclair41e4d9a2022-05-01 14:40:55 +000048 /// @returns the WGSL name for the attribute
49 std::string Name() const override;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000050
dan sinclair41e4d9a2022-05-01 14:40:55 +000051 /// Clones this node and all transitive child nodes using the `CloneContext`
52 /// `ctx`.
53 /// @param ctx the clone context
54 /// @return the newly cloned node
55 const StructMemberOffsetAttribute* Clone(CloneContext* ctx) const override;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000056
dan sinclair93df9672022-09-09 14:49:09 +000057 /// The offset expression
58 const ast::Expression* const expr;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000059};
60
dan sinclair34323ac2022-04-07 18:39:35 +000061} // namespace tint::ast
Ryan Harrisondbc13af2022-02-21 15:19:07 +000062
63#endif // SRC_TINT_AST_STRUCT_MEMBER_OFFSET_ATTRIBUTE_H_