Add semantic::Variable, use it.

Pull the mutable semantic field from ast::Variable and into a new semantic::Variable node.
Have the TypeDeterminer create these semantic::Variable nodes.

Bug: tint:390
Change-Id: Ia13f5e7b065941ed66ea5a86c6ccb288071feff3
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/40063
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton
2021-02-03 17:51:09 +00:00
committed by Commit Bot service account
parent 401b96b9bb
commit b17aea159c
26 changed files with 485 additions and 295 deletions

View File

@@ -28,7 +28,6 @@ namespace tint {
namespace ast {
class BindingDecoration;
class GroupDecoration;
class Variable;
class LocationDecoration;
class BuiltinDecoration;
} // namespace ast
@@ -38,6 +37,8 @@ class Type;
namespace semantic {
class Variable;
/// Function holds the semantic information for function nodes.
class Function : public Castable<Function, Node> {
public:
@@ -54,8 +55,8 @@ class Function : public Castable<Function, Node> {
/// @param local_referenced_module_vars the locally referenced module
/// variables
/// @param ancestor_entry_points the ancestor entry points
explicit Function(std::vector<ast::Variable*> referenced_module_vars,
std::vector<ast::Variable*> local_referenced_module_vars,
explicit Function(std::vector<const Variable*> referenced_module_vars,
std::vector<const Variable*> local_referenced_module_vars,
std::vector<Symbol> ancestor_entry_points);
/// Destructor
@@ -64,11 +65,11 @@ class Function : public Castable<Function, Node> {
/// Note: If this function calls other functions, the return will also include
/// all of the referenced variables from the callees.
/// @returns the referenced module variables
const std::vector<ast::Variable*>& ReferencedModuleVariables() const {
const std::vector<const Variable*>& ReferencedModuleVariables() const {
return referenced_module_vars_;
}
/// @returns the locally referenced module variables
const std::vector<ast::Variable*>& LocalReferencedModuleVariables() const {
const std::vector<const Variable*>& LocalReferencedModuleVariables() const {
return local_referenced_module_vars_;
}
/// @returns the ancestor entry points
@@ -77,53 +78,53 @@ class Function : public Castable<Function, Node> {
}
/// Retrieves any referenced location variables
/// @returns the <variable, decoration> pair.
const std::vector<std::pair<ast::Variable*, ast::LocationDecoration*>>
const std::vector<std::pair<const Variable*, ast::LocationDecoration*>>
ReferencedLocationVariables() const;
/// Retrieves any referenced builtin variables
/// @returns the <variable, decoration> pair.
const std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>>
const std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>>
ReferencedBuiltinVariables() const;
/// Retrieves any referenced uniform variables. Note, the variables must be
/// decorated with both binding and group decorations.
/// @returns the referenced uniforms
const std::vector<std::pair<ast::Variable*, BindingInfo>>
const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedUniformVariables() const;
/// Retrieves any referenced storagebuffer variables. Note, the variables
/// must be decorated with both binding and group decorations.
/// @returns the referenced storagebuffers
const std::vector<std::pair<ast::Variable*, BindingInfo>>
const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedStoragebufferVariables() const;
/// Retrieves any referenced regular Sampler variables. Note, the
/// variables must be decorated with both binding and group decorations.
/// @returns the referenced storagebuffers
const std::vector<std::pair<ast::Variable*, BindingInfo>>
const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedSamplerVariables() const;
/// Retrieves any referenced comparison Sampler variables. Note, the
/// variables must be decorated with both binding and group decorations.
/// @returns the referenced storagebuffers
const std::vector<std::pair<ast::Variable*, BindingInfo>>
const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedComparisonSamplerVariables() const;
/// Retrieves any referenced sampled textures variables. Note, the
/// variables must be decorated with both binding and group decorations.
/// @returns the referenced sampled textures
const std::vector<std::pair<ast::Variable*, BindingInfo>>
const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedSampledTextureVariables() const;
/// Retrieves any referenced multisampled textures variables. Note, the
/// variables must be decorated with both binding and group decorations.
/// @returns the referenced sampled textures
const std::vector<std::pair<ast::Variable*, BindingInfo>>
const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedMultisampledTextureVariables() const;
/// Retrieves any locally referenced builtin variables
/// @returns the <variable, decoration> pairs.
const std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>>
const std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>>
LocalReferencedBuiltinVariables() const;
/// Checks if the given entry point is an ancestor
@@ -132,13 +133,13 @@ class Function : public Castable<Function, Node> {
bool HasAncestorEntryPoint(Symbol sym) const;
private:
const std::vector<std::pair<ast::Variable*, BindingInfo>>
const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedSamplerVariablesImpl(type::SamplerKind kind) const;
const std::vector<std::pair<ast::Variable*, BindingInfo>>
const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedSampledTextureVariablesImpl(bool multisampled) const;
std::vector<ast::Variable*> const referenced_module_vars_;
std::vector<ast::Variable*> const local_referenced_module_vars_;
std::vector<const Variable*> const referenced_module_vars_;
std::vector<const Variable*> const local_referenced_module_vars_;
std::vector<Symbol> const ancestor_entry_points_;
};

View File

@@ -20,6 +20,7 @@
#include "src/ast/location_decoration.h"
#include "src/ast/variable.h"
#include "src/ast/variable_decoration.h"
#include "src/semantic/variable.h"
#include "src/type/multisampled_texture_type.h"
#include "src/type/sampled_texture_type.h"
#include "src/type/texture_type.h"
@@ -29,8 +30,8 @@ TINT_INSTANTIATE_CLASS_ID(tint::semantic::Function);
namespace tint {
namespace semantic {
Function::Function(std::vector<ast::Variable*> referenced_module_vars,
std::vector<ast::Variable*> local_referenced_module_vars,
Function::Function(std::vector<const Variable*> referenced_module_vars,
std::vector<const Variable*> local_referenced_module_vars,
std::vector<Symbol> ancestor_entry_points)
: referenced_module_vars_(std::move(referenced_module_vars)),
local_referenced_module_vars_(std::move(local_referenced_module_vars)),
@@ -38,12 +39,12 @@ Function::Function(std::vector<ast::Variable*> referenced_module_vars,
Function::~Function() = default;
const std::vector<std::pair<ast::Variable*, ast::LocationDecoration*>>
const std::vector<std::pair<const Variable*, ast::LocationDecoration*>>
Function::ReferencedLocationVariables() const {
std::vector<std::pair<ast::Variable*, ast::LocationDecoration*>> ret;
std::vector<std::pair<const Variable*, ast::LocationDecoration*>> ret;
for (auto* var : ReferencedModuleVariables()) {
for (auto* deco : var->decorations()) {
for (auto* deco : var->Declaration()->decorations()) {
if (auto* location = deco->As<ast::LocationDecoration>()) {
ret.push_back({var, location});
break;
@@ -53,18 +54,18 @@ Function::ReferencedLocationVariables() const {
return ret;
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedUniformVariables() const {
std::vector<std::pair<ast::Variable*, Function::BindingInfo>> ret;
std::vector<std::pair<const Variable*, Function::BindingInfo>> ret;
for (auto* var : ReferencedModuleVariables()) {
if (var->storage_class() != ast::StorageClass::kUniform) {
if (var->StorageClass() != ast::StorageClass::kUniform) {
continue;
}
ast::BindingDecoration* binding = nullptr;
ast::GroupDecoration* group = nullptr;
for (auto* deco : var->decorations()) {
for (auto* deco : var->Declaration()->decorations()) {
if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b;
} else if (auto* g = deco->As<ast::GroupDecoration>()) {
@@ -80,18 +81,18 @@ Function::ReferencedUniformVariables() const {
return ret;
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedStoragebufferVariables() const {
std::vector<std::pair<ast::Variable*, Function::BindingInfo>> ret;
std::vector<std::pair<const Variable*, Function::BindingInfo>> ret;
for (auto* var : ReferencedModuleVariables()) {
if (var->storage_class() != ast::StorageClass::kStorage) {
if (var->StorageClass() != ast::StorageClass::kStorage) {
continue;
}
ast::BindingDecoration* binding = nullptr;
ast::GroupDecoration* group = nullptr;
for (auto* deco : var->decorations()) {
for (auto* deco : var->Declaration()->decorations()) {
if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b;
} else if (auto* s = deco->As<ast::GroupDecoration>()) {
@@ -107,12 +108,12 @@ Function::ReferencedStoragebufferVariables() const {
return ret;
}
const std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>>
const std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>>
Function::ReferencedBuiltinVariables() const {
std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>> ret;
std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>> ret;
for (auto* var : ReferencedModuleVariables()) {
for (auto* deco : var->decorations()) {
for (auto* deco : var->Declaration()->decorations()) {
if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
ret.push_back({var, builtin});
break;
@@ -122,32 +123,32 @@ Function::ReferencedBuiltinVariables() const {
return ret;
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedSamplerVariables() const {
return ReferencedSamplerVariablesImpl(type::SamplerKind::kSampler);
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedComparisonSamplerVariables() const {
return ReferencedSamplerVariablesImpl(type::SamplerKind::kComparisonSampler);
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedSampledTextureVariables() const {
return ReferencedSampledTextureVariablesImpl(false);
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedMultisampledTextureVariables() const {
return ReferencedSampledTextureVariablesImpl(true);
}
const std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>>
const std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>>
Function::LocalReferencedBuiltinVariables() const {
std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>> ret;
std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>> ret;
for (auto* var : LocalReferencedModuleVariables()) {
for (auto* deco : var->decorations()) {
for (auto* deco : var->Declaration()->decorations()) {
if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
ret.push_back({var, builtin});
break;
@@ -166,12 +167,12 @@ bool Function::HasAncestorEntryPoint(Symbol symbol) const {
return false;
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedSamplerVariablesImpl(type::SamplerKind kind) const {
std::vector<std::pair<ast::Variable*, Function::BindingInfo>> ret;
std::vector<std::pair<const Variable*, Function::BindingInfo>> ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->type()->UnwrapIfNeeded();
auto* unwrapped_type = var->Declaration()->type()->UnwrapIfNeeded();
auto* sampler = unwrapped_type->As<type::Sampler>();
if (sampler == nullptr || sampler->kind() != kind) {
continue;
@@ -179,7 +180,7 @@ Function::ReferencedSamplerVariablesImpl(type::SamplerKind kind) const {
ast::BindingDecoration* binding = nullptr;
ast::GroupDecoration* group = nullptr;
for (auto* deco : var->decorations()) {
for (auto* deco : var->Declaration()->decorations()) {
if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b;
}
@@ -196,12 +197,12 @@ Function::ReferencedSamplerVariablesImpl(type::SamplerKind kind) const {
return ret;
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedSampledTextureVariablesImpl(bool multisampled) const {
std::vector<std::pair<ast::Variable*, Function::BindingInfo>> ret;
std::vector<std::pair<const Variable*, Function::BindingInfo>> ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->type()->UnwrapIfNeeded();
auto* unwrapped_type = var->Declaration()->type()->UnwrapIfNeeded();
auto* texture = unwrapped_type->As<type::Texture>();
if (texture == nullptr) {
continue;
@@ -216,7 +217,7 @@ Function::ReferencedSampledTextureVariablesImpl(bool multisampled) const {
ast::BindingDecoration* binding = nullptr;
ast::GroupDecoration* group = nullptr;
for (auto* deco : var->decorations()) {
for (auto* deco : var->Declaration()->decorations()) {
if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b;
} else if (auto* s = deco->As<ast::GroupDecoration>()) {

View File

@@ -0,0 +1,28 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/semantic/variable.h"
TINT_INSTANTIATE_CLASS_ID(tint::semantic::Variable);
namespace tint {
namespace semantic {
Variable::Variable(ast::Variable* declaration, ast::StorageClass storage_class)
: declaration_(declaration), storage_class_(storage_class) {}
Variable::~Variable() = default;
} // namespace semantic
} // namespace tint

View File

@@ -24,6 +24,7 @@ namespace ast {
class Expression;
class Function;
class Variable;
} // namespace ast
@@ -31,6 +32,7 @@ namespace semantic {
class Expression;
class Function;
class Variable;
/// TypeMappings is a struct that holds dummy `operator()` methods that's used
/// by SemanticNodeTypeFor to map AST node types to their corresponding semantic
@@ -41,6 +43,7 @@ struct TypeMappings {
//! @cond Doxygen_Suppress
semantic::Expression* operator()(ast::Expression*);
semantic::Function* operator()(ast::Function*);
semantic::Variable* operator()(ast::Variable*);
//! @endcond
};

64
src/semantic/variable.h Normal file
View File

@@ -0,0 +1,64 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0(the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_SEMANTIC_VARIABLE_H_
#define SRC_SEMANTIC_VARIABLE_H_
#include <utility>
#include <vector>
#include "src/ast/storage_class.h"
#include "src/semantic/node.h"
#include "src/type/sampler_type.h"
namespace tint {
// Forward declarations
namespace ast {
class Variable;
} // namespace ast
namespace type {
class Type;
} // namespace type
namespace semantic {
/// Variable holds the semantic information for variables.
class Variable : public Castable<Variable, Node> {
public:
/// Constructor
/// @param declaration the AST declaration node
/// @param storage_class the variable storage class
explicit Variable(ast::Variable* declaration,
ast::StorageClass storage_class);
/// Destructor
~Variable() override;
/// @returns the AST declaration node
ast::Variable* Declaration() const { return declaration_; }
/// @returns the storage class for the variable
ast::StorageClass StorageClass() const { return storage_class_; }
private:
ast::Variable* const declaration_;
ast::StorageClass const storage_class_;
};
} // namespace semantic
} // namespace tint
#endif // SRC_SEMANTIC_VARIABLE_H_