From a7230f06ff67d804f5d6cefad175cabc1f066e57 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 11 Apr 2022 14:37:21 +0000 Subject: [PATCH] tint: Standardize the way we forward-declare Style guide has been updated to describe the style in use. Change-Id: I3fc08e3440566106582695f4dc149fa67d8b8dc8 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/86303 Reviewed-by: Dan Sinclair Commit-Queue: Ben Clayton --- docs/tint/style_guide.md | 18 ++++++++++++++++++ src/tint/ast/array.h | 6 ++++-- src/tint/ast/bitcast_expression.h | 6 ++++-- src/tint/ast/call_expression.h | 6 ++++-- src/tint/ast/node.h | 13 +++++++------ src/tint/ast/struct_member.h | 6 ++++-- src/tint/ast/variable.h | 6 ++++-- src/tint/ast/workgroup_attribute.h | 6 ++++-- src/tint/builtin_table.h | 6 ++++-- src/tint/castable.h | 9 +++++---- src/tint/clone_context.h | 10 +++++----- src/tint/program.h | 6 +++--- src/tint/program_builder.h | 4 +++- src/tint/reader/spirv/parser_type.h | 8 ++++---- src/tint/resolver/resolver.h | 10 ++++------ src/tint/sem/call_target.h | 2 +- src/tint/sem/for_loop_statement.h | 12 +++++------- src/tint/sem/function.h | 10 ++++------ src/tint/sem/if_statement.h | 10 ++++------ src/tint/sem/info.h | 2 +- src/tint/sem/loop_statement.h | 2 +- src/tint/sem/matrix_type.h | 4 ++-- src/tint/sem/member_accessor_expression.h | 12 +++++------- src/tint/sem/statement.h | 10 ++++------ src/tint/sem/storage_texture_type.h | 4 ++-- src/tint/sem/struct.h | 10 ++++------ src/tint/sem/type_mappings.h | 10 ++++------ src/tint/sem/variable.h | 10 ++++------ src/tint/transform/array_length_from_uniform.h | 2 +- src/tint/transform/calculate_array_length.h | 2 +- src/tint/transform/decompose_memory_access.h | 2 +- src/tint/transform/glsl.h | 2 +- .../transform/num_workgroups_from_uniform.h | 2 +- src/tint/writer/glsl/generator.h | 6 +++--- src/tint/writer/hlsl/generator.h | 6 +++--- src/tint/writer/msl/generator.h | 6 +++--- src/tint/writer/spirv/generator.h | 6 +++--- src/tint/writer/wgsl/generator.h | 9 ++++----- 38 files changed, 139 insertions(+), 122 deletions(-) diff --git a/docs/tint/style_guide.md b/docs/tint/style_guide.md index 52b08c5e72..25e91467c8 100644 --- a/docs/tint/style_guide.md +++ b/docs/tint/style_guide.md @@ -27,6 +27,24 @@ in WGSL or another shader language processed by Tint. If the concept you are trying to name is about distinguishing between alternatives, use `kind` instead. +* Forward declarations: + * Use forward declarations where possible, instead of using `#include`'s. + * Place forward declarations in their own **un-nested** namespace declarations. \ + Example: \ + to forward-declare `struct X` in namespace `A` and `struct Y` + in namespace `A::B`, you'd write: + ```c++ + // Forward declarations + namespace A { + struct X; + } // namespace A + namespace A::B { + struct Y; + } // namespace A::B + + // rest of the header code is declared below ... + ``` + ## Compiler support Tint requires C++17. diff --git a/src/tint/ast/array.h b/src/tint/ast/array.h index 05c2f4d87e..0867c6d35e 100644 --- a/src/tint/ast/array.h +++ b/src/tint/ast/array.h @@ -20,10 +20,12 @@ #include "src/tint/ast/attribute.h" #include "src/tint/ast/type.h" +// Forward declarations namespace tint::ast { - -// Forward declarations. class Expression; +} // namespace tint::ast + +namespace tint::ast { /// An array type. If size is zero then it is a runtime array. class Array final : public Castable { diff --git a/src/tint/ast/bitcast_expression.h b/src/tint/ast/bitcast_expression.h index 11407d629b..8eacf7156a 100644 --- a/src/tint/ast/bitcast_expression.h +++ b/src/tint/ast/bitcast_expression.h @@ -17,10 +17,12 @@ #include "src/tint/ast/expression.h" +// Forward declarations namespace tint::ast { - -// Forward declaration class Type; +} // namespace tint::ast + +namespace tint::ast { /// A bitcast expression class BitcastExpression final : public Castable { diff --git a/src/tint/ast/call_expression.h b/src/tint/ast/call_expression.h index 5c6f7ba2e9..aab2b812bd 100644 --- a/src/tint/ast/call_expression.h +++ b/src/tint/ast/call_expression.h @@ -17,11 +17,13 @@ #include "src/tint/ast/expression.h" +// Forward declarations namespace tint::ast { - -// Forward declarations. class Type; class IdentifierExpression; +} // namespace tint::ast + +namespace tint::ast { /// A call expression - represents either a: /// * sem::Function diff --git a/src/tint/ast/node.h b/src/tint/ast/node.h index 49a917c37f..90699adb26 100644 --- a/src/tint/ast/node.h +++ b/src/tint/ast/node.h @@ -22,14 +22,13 @@ // Forward declarations namespace tint { class CloneContext; -namespace sem { +} // namespace tint +namespace tint::sem { class Type; class Info; -} -} // namespace tint +} // namespace tint::sem -namespace tint { -namespace ast { +namespace tint::ast { /// AST base class node class Node : public Castable { @@ -54,7 +53,9 @@ class Node : public Castable { Node(const Node&) = delete; }; -} // namespace ast +} // namespace tint::ast + +namespace tint { /// @param node a pointer to an AST node /// @returns the ProgramID of the given AST node. diff --git a/src/tint/ast/struct_member.h b/src/tint/ast/struct_member.h index 4d3bb026e9..7d21c9d323 100644 --- a/src/tint/ast/struct_member.h +++ b/src/tint/ast/struct_member.h @@ -20,10 +20,12 @@ #include "src/tint/ast/attribute.h" +// Forward declarations namespace tint::ast { - -// Forward declaration class Type; +} // namespace tint::ast + +namespace tint::ast { /// A struct member statement. class StructMember final : public Castable { diff --git a/src/tint/ast/variable.h b/src/tint/ast/variable.h index 27a55644b8..208f0b138d 100644 --- a/src/tint/ast/variable.h +++ b/src/tint/ast/variable.h @@ -23,13 +23,15 @@ #include "src/tint/ast/expression.h" #include "src/tint/ast/storage_class.h" -namespace tint::ast { - // Forward declarations +namespace tint::ast { class BindingAttribute; class GroupAttribute; class LocationAttribute; class Type; +} // namespace tint::ast + +namespace tint::ast { /// VariableBindingPoint holds a group and binding attribute. struct VariableBindingPoint { diff --git a/src/tint/ast/workgroup_attribute.h b/src/tint/ast/workgroup_attribute.h index 116c731b44..232201b8d8 100644 --- a/src/tint/ast/workgroup_attribute.h +++ b/src/tint/ast/workgroup_attribute.h @@ -20,10 +20,12 @@ #include "src/tint/ast/attribute.h" +// Forward declarations namespace tint::ast { - -// Forward declaration class Expression; +} // namespace tint::ast + +namespace tint::ast { /// A workgroup attribute class WorkgroupAttribute final diff --git a/src/tint/builtin_table.h b/src/tint/builtin_table.h index 246eed2800..4b1b5dc9b4 100644 --- a/src/tint/builtin_table.h +++ b/src/tint/builtin_table.h @@ -21,10 +21,12 @@ #include "src/tint/sem/builtin.h" -namespace tint { - // Forward declarations +namespace tint { class ProgramBuilder; +} // namespace tint + +namespace tint { /// BuiltinTable is a lookup table of all the WGSL builtin functions class BuiltinTable { diff --git a/src/tint/castable.h b/src/tint/castable.h index f5d2cb2c6d..211f2fb8d6 100644 --- a/src/tint/castable.h +++ b/src/tint/castable.h @@ -43,20 +43,21 @@ TINT_CASTABLE_PUSH_DISABLE_WARNINGS(); +// Forward declarations namespace tint { - -// Forward declaration class CastableBase; /// Ignore is used as a special type used for skipping over types for trait /// helper functions. class Ignore {}; +} // namespace tint -namespace detail { +namespace tint::detail { template struct TypeInfoOf; +} // namespace tint::detail -} // namespace detail +namespace tint { /// True if all template types that are not Ignore derive from CastableBase template diff --git a/src/tint/clone_context.h b/src/tint/clone_context.h index 0c52f3b8c6..35bb231afe 100644 --- a/src/tint/clone_context.h +++ b/src/tint/clone_context.h @@ -29,6 +29,11 @@ #include "src/tint/traits.h" // Forward declarations +namespace tint { +class CloneContext; +class Program; +class ProgramBuilder; +} // namespace tint namespace tint::ast { class FunctionList; class Node; @@ -36,11 +41,6 @@ class Node; namespace tint { -// Forward Declarations -class CloneContext; -class Program; -class ProgramBuilder; - ProgramID ProgramIDOf(const Program*); ProgramID ProgramIDOf(const ProgramBuilder*); diff --git a/src/tint/program.h b/src/tint/program.h index cb3c98808e..37b462e55f 100644 --- a/src/tint/program.h +++ b/src/tint/program.h @@ -25,15 +25,15 @@ #include "src/tint/symbol_table.h" // Forward Declarations +namespace tint { +class CloneContext; +} // namespace tint namespace tint::ast { class Module; } // namespace tint::ast namespace tint { -// Forward declarations -class CloneContext; - /// Program holds the AST, Type information and SymbolTable for a tint program. class Program { public: diff --git a/src/tint/program_builder.h b/src/tint/program_builder.h index 13e7a05d2e..2f7449ebaa 100644 --- a/src/tint/program_builder.h +++ b/src/tint/program_builder.h @@ -99,12 +99,14 @@ #endif // Forward declarations +namespace tint { +class CloneContext; +} // namespace tint namespace tint::ast { class VariableDeclStatement; } // namespace tint::ast namespace tint { -class CloneContext; /// ProgramBuilder is a mutable builder for a Program. /// To construct a Program, populate the builder and then `std::move` it to a diff --git a/src/tint/reader/spirv/parser_type.h b/src/tint/reader/spirv/parser_type.h index 578f89fb3c..491b765a1e 100644 --- a/src/tint/reader/spirv/parser_type.h +++ b/src/tint/reader/spirv/parser_type.h @@ -27,13 +27,13 @@ #include "src/tint/castable.h" #include "src/tint/utils/block_allocator.h" -// Forward Declarations +// Forward declarations namespace tint { class ProgramBuilder; -namespace ast { -class Type; -} } // namespace tint +namespace tint::ast { +class Type; +} // namespace tint::ast namespace tint::reader::spirv { diff --git a/src/tint/resolver/resolver.h b/src/tint/resolver/resolver.h index 850eb957ee..ff879b08ce 100644 --- a/src/tint/resolver/resolver.h +++ b/src/tint/resolver/resolver.h @@ -36,8 +36,7 @@ #include "src/tint/utils/unique_vector.h" // Forward declarations -namespace tint { -namespace ast { +namespace tint::ast { class IndexAccessorExpression; class BinaryExpression; class BitcastExpression; @@ -53,8 +52,8 @@ class ReturnStatement; class SwitchStatement; class UnaryOpExpression; class Variable; -} // namespace ast -namespace sem { +} // namespace tint::ast +namespace tint::sem { class Array; class Atomic; class BlockStatement; @@ -67,8 +66,7 @@ class LoopStatement; class Statement; class SwitchStatement; class TypeConstructor; -} // namespace sem -} // namespace tint +} // namespace tint::sem namespace tint::resolver { diff --git a/src/tint/sem/call_target.h b/src/tint/sem/call_target.h index 1671899d2b..01650eb3de 100644 --- a/src/tint/sem/call_target.h +++ b/src/tint/sem/call_target.h @@ -25,7 +25,7 @@ // Forward declarations namespace tint::sem { class Type; -} +} // namespace tint::sem namespace tint::sem { diff --git a/src/tint/sem/for_loop_statement.h b/src/tint/sem/for_loop_statement.h index 0142d282ff..cd7b7614ec 100644 --- a/src/tint/sem/for_loop_statement.h +++ b/src/tint/sem/for_loop_statement.h @@ -17,15 +17,13 @@ #include "src/tint/sem/statement.h" -// Forward Declarations -namespace tint { -namespace ast { +// Forward declarations +namespace tint::ast { class ForLoopStatement; -} -namespace sem { +} // namespace tint::ast +namespace tint::sem { class Expression; -} -} // namespace tint +} // namespace tint::sem namespace tint::sem { diff --git a/src/tint/sem/function.h b/src/tint/sem/function.h index f8854e0c02..68136765fb 100644 --- a/src/tint/sem/function.h +++ b/src/tint/sem/function.h @@ -24,18 +24,16 @@ #include "src/tint/utils/unique_vector.h" // Forward declarations -namespace tint { -namespace ast { +namespace tint::ast { class BuiltinAttribute; class Function; class LocationAttribute; class ReturnStatement; -} // namespace ast -namespace sem { +} // namespace tint::ast +namespace tint::sem { class Builtin; class Variable; -} // namespace sem -} // namespace tint +} // namespace tint::sem namespace tint::sem { diff --git a/src/tint/sem/if_statement.h b/src/tint/sem/if_statement.h index 6e11d52b79..8e4fb27621 100644 --- a/src/tint/sem/if_statement.h +++ b/src/tint/sem/if_statement.h @@ -18,15 +18,13 @@ #include "src/tint/sem/statement.h" // Forward declarations -namespace tint { -namespace ast { +namespace tint::ast { class IfStatement; class ElseStatement; -} // namespace ast -namespace sem { +} // namespace tint::ast +namespace tint::sem { class Expression; -} -} // namespace tint +} // namespace tint::sem namespace tint::sem { diff --git a/src/tint/sem/info.h b/src/tint/sem/info.h index 8502269726..353f67fcfb 100644 --- a/src/tint/sem/info.h +++ b/src/tint/sem/info.h @@ -25,7 +25,7 @@ // Forward declarations namespace tint::sem { class Module; -} +} // namespace tint::sem namespace tint::sem { diff --git a/src/tint/sem/loop_statement.h b/src/tint/sem/loop_statement.h index 0c8edd982a..ccf3be0965 100644 --- a/src/tint/sem/loop_statement.h +++ b/src/tint/sem/loop_statement.h @@ -20,7 +20,7 @@ // Forward declarations namespace tint::ast { class LoopStatement; -} +} // namespace tint::ast namespace tint::sem { diff --git a/src/tint/sem/matrix_type.h b/src/tint/sem/matrix_type.h index 809c6f3125..b5b96699a3 100644 --- a/src/tint/sem/matrix_type.h +++ b/src/tint/sem/matrix_type.h @@ -19,10 +19,10 @@ #include "src/tint/sem/type.h" -// Forward declaration +// Forward declarations namespace tint::sem { class Vector; -} +} // namespace tint::sem namespace tint::sem { diff --git a/src/tint/sem/member_accessor_expression.h b/src/tint/sem/member_accessor_expression.h index cca1a43d87..342acbe323 100644 --- a/src/tint/sem/member_accessor_expression.h +++ b/src/tint/sem/member_accessor_expression.h @@ -19,16 +19,14 @@ #include "src/tint/sem/expression.h" -/// Forward declarations -namespace tint { -namespace ast { +// Forward declarations +namespace tint::ast { class MemberAccessorExpression; -} // namespace ast -namespace sem { +} // namespace tint::ast +namespace tint::sem { class Struct; class StructMember; -} // namespace sem -} // namespace tint +} // namespace tint::sem namespace tint::sem { diff --git a/src/tint/sem/statement.h b/src/tint/sem/statement.h index f7cf40cf72..76336d7b26 100644 --- a/src/tint/sem/statement.h +++ b/src/tint/sem/statement.h @@ -19,17 +19,15 @@ #include "src/tint/sem/node.h" // Forward declarations -namespace tint { -namespace ast { +namespace tint::ast { class Function; class Statement; -} // namespace ast -namespace sem { +} // namespace tint::ast +namespace tint::sem { class BlockStatement; class CompoundStatement; class Function; -} // namespace sem -} // namespace tint +} // namespace tint::sem namespace tint::sem { namespace detail { diff --git a/src/tint/sem/storage_texture_type.h b/src/tint/sem/storage_texture_type.h index 06d8dc5c5d..71569e7812 100644 --- a/src/tint/sem/storage_texture_type.h +++ b/src/tint/sem/storage_texture_type.h @@ -21,10 +21,10 @@ #include "src/tint/ast/storage_texture.h" #include "src/tint/sem/texture_type.h" -// Forward Declarations +// Forward declarations namespace tint::sem { class Manager; -} +} // namespace tint::sem namespace tint::sem { diff --git a/src/tint/sem/struct.h b/src/tint/sem/struct.h index 425bbc138e..f5e8081dec 100644 --- a/src/tint/sem/struct.h +++ b/src/tint/sem/struct.h @@ -28,15 +28,13 @@ #include "src/tint/symbol.h" // Forward declarations -namespace tint { -namespace ast { +namespace tint::ast { class StructMember; -} // namespace ast -namespace sem { +} // namespace tint::ast +namespace tint::sem { class StructMember; class Type; -} // namespace sem -} // namespace tint +} // namespace tint::sem namespace tint::sem { diff --git a/src/tint/sem/type_mappings.h b/src/tint/sem/type_mappings.h index 123b8cc544..1bbc46f5f1 100644 --- a/src/tint/sem/type_mappings.h +++ b/src/tint/sem/type_mappings.h @@ -18,8 +18,7 @@ #include // Forward declarations -namespace tint { -namespace ast { +namespace tint::ast { class Array; class CallExpression; class Expression; @@ -35,8 +34,8 @@ class StructMember; class Type; class TypeDecl; class Variable; -} // namespace ast -namespace sem { +} // namespace tint::ast +namespace tint::sem { class Array; class Call; class Expression; @@ -51,8 +50,7 @@ class Struct; class StructMember; class Type; class Variable; -} // namespace sem -} // namespace tint +} // namespace tint::sem namespace tint::sem { diff --git a/src/tint/sem/variable.h b/src/tint/sem/variable.h index 4c89f506ca..ca97d64b19 100644 --- a/src/tint/sem/variable.h +++ b/src/tint/sem/variable.h @@ -25,17 +25,15 @@ #include "src/tint/sem/parameter_usage.h" // Forward declarations -namespace tint { -namespace ast { +namespace tint::ast { class IdentifierExpression; class Variable; -} // namespace ast -namespace sem { +} // namespace tint::ast +namespace tint::sem { class CallTarget; class Type; class VariableUser; -} // namespace sem -} // namespace tint +} // namespace tint::sem namespace tint::sem { diff --git a/src/tint/transform/array_length_from_uniform.h b/src/tint/transform/array_length_from_uniform.h index aea7710e5d..9a3a5d5270 100644 --- a/src/tint/transform/array_length_from_uniform.h +++ b/src/tint/transform/array_length_from_uniform.h @@ -24,7 +24,7 @@ // Forward declarations namespace tint { class CloneContext; -} +} // namespace tint namespace tint::transform { diff --git a/src/tint/transform/calculate_array_length.h b/src/tint/transform/calculate_array_length.h index 0ed7a5d42a..344f6f078e 100644 --- a/src/tint/transform/calculate_array_length.h +++ b/src/tint/transform/calculate_array_length.h @@ -23,7 +23,7 @@ // Forward declarations namespace tint { class CloneContext; -} +} // namespace tint namespace tint::transform { diff --git a/src/tint/transform/decompose_memory_access.h b/src/tint/transform/decompose_memory_access.h index efcd39b118..9aa0eb5e7b 100644 --- a/src/tint/transform/decompose_memory_access.h +++ b/src/tint/transform/decompose_memory_access.h @@ -23,7 +23,7 @@ // Forward declarations namespace tint { class CloneContext; -} +} // namespace tint namespace tint::transform { diff --git a/src/tint/transform/glsl.h b/src/tint/transform/glsl.h index 73bc76b801..c84ff2958d 100644 --- a/src/tint/transform/glsl.h +++ b/src/tint/transform/glsl.h @@ -22,7 +22,7 @@ // Forward declarations namespace tint { class CloneContext; -} +} // namespace tint namespace tint::transform { diff --git a/src/tint/transform/num_workgroups_from_uniform.h b/src/tint/transform/num_workgroups_from_uniform.h index 4cbecb6880..e4cf20ed97 100644 --- a/src/tint/transform/num_workgroups_from_uniform.h +++ b/src/tint/transform/num_workgroups_from_uniform.h @@ -21,7 +21,7 @@ // Forward declarations namespace tint { class CloneContext; -} +} // namespace tint namespace tint::transform { diff --git a/src/tint/writer/glsl/generator.h b/src/tint/writer/glsl/generator.h index 19c3d8d99a..ab5c9bb8d7 100644 --- a/src/tint/writer/glsl/generator.h +++ b/src/tint/writer/glsl/generator.h @@ -31,10 +31,10 @@ // Forward declarations namespace tint { class Program; -namespace writer::glsl { -class GeneratorImpl; -} // namespace writer::glsl } // namespace tint +namespace tint::writer::glsl { +class GeneratorImpl; +} // namespace tint::writer::glsl namespace tint::writer::glsl { diff --git a/src/tint/writer/hlsl/generator.h b/src/tint/writer/hlsl/generator.h index a2dcd0b04a..beb2a886b9 100644 --- a/src/tint/writer/hlsl/generator.h +++ b/src/tint/writer/hlsl/generator.h @@ -29,10 +29,10 @@ // Forward declarations namespace tint { class Program; -namespace writer::hlsl { -class GeneratorImpl; -} // namespace writer::hlsl } // namespace tint +namespace tint::writer::hlsl { +class GeneratorImpl; +} // namespace tint::writer::hlsl namespace tint::writer::hlsl { diff --git a/src/tint/writer/msl/generator.h b/src/tint/writer/msl/generator.h index 21f14e0c8c..1415e6becb 100644 --- a/src/tint/writer/msl/generator.h +++ b/src/tint/writer/msl/generator.h @@ -27,10 +27,10 @@ // Forward declarations namespace tint { class Program; -namespace writer::msl { -class GeneratorImpl; -} // namespace writer::msl } // namespace tint +namespace tint::writer::msl { +class GeneratorImpl; +} // namespace tint::writer::msl namespace tint::writer::msl { diff --git a/src/tint/writer/spirv/generator.h b/src/tint/writer/spirv/generator.h index 3d6f43215d..81428b42c7 100644 --- a/src/tint/writer/spirv/generator.h +++ b/src/tint/writer/spirv/generator.h @@ -24,11 +24,11 @@ // Forward declarations namespace tint { class Program; -namespace writer::spirv { +} +namespace tint::writer::spirv { class Builder; class BinaryWriter; -} // namespace writer::spirv -} // namespace tint +} // namespace tint::writer::spirv namespace tint::writer::spirv { diff --git a/src/tint/writer/wgsl/generator.h b/src/tint/writer/wgsl/generator.h index df9bc714d7..62fe2e1b75 100644 --- a/src/tint/writer/wgsl/generator.h +++ b/src/tint/writer/wgsl/generator.h @@ -20,12 +20,12 @@ #include "src/tint/writer/text.h" -namespace tint { - // Forward declarations +namespace tint { class Program; +} // namespace tint -namespace writer::wgsl { +namespace tint::writer::wgsl { class GeneratorImpl; @@ -61,7 +61,6 @@ struct Result { /// @returns the resulting WGSL and supplementary information Result Generate(const Program* program, const Options& options); -} // namespace writer::wgsl -} // namespace tint +} // namespace tint::writer::wgsl #endif // SRC_TINT_WRITER_WGSL_GENERATOR_H_