2020-03-02 20:47:43 +00:00
|
|
|
// Copyright 2020 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_AST_FUNCTION_H_
|
|
|
|
#define SRC_AST_FUNCTION_H_
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <ostream>
|
|
|
|
#include <string>
|
2020-11-03 16:26:09 +00:00
|
|
|
#include <tuple>
|
2020-03-02 20:47:43 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-07-20 13:46:32 +00:00
|
|
|
#include "src/ast/binding_decoration.h"
|
2020-07-27 15:25:00 +00:00
|
|
|
#include "src/ast/block_statement.h"
|
2020-07-15 18:26:25 +00:00
|
|
|
#include "src/ast/builtin_decoration.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
#include "src/ast/expression.h"
|
2020-09-20 19:18:58 +00:00
|
|
|
#include "src/ast/function_decoration.h"
|
2020-07-15 18:26:25 +00:00
|
|
|
#include "src/ast/location_decoration.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
#include "src/ast/node.h"
|
2020-09-21 17:51:31 +00:00
|
|
|
#include "src/ast/pipeline_stage.h"
|
2020-07-20 13:46:32 +00:00
|
|
|
#include "src/ast/set_decoration.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
#include "src/ast/statement.h"
|
2020-11-05 20:09:50 +00:00
|
|
|
#include "src/ast/type/sampler_type.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
#include "src/ast/type/type.h"
|
|
|
|
#include "src/ast/variable.h"
|
2020-12-11 18:24:53 +00:00
|
|
|
#include "src/symbol.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace ast {
|
|
|
|
|
|
|
|
/// A Function statement.
|
2020-11-30 23:30:58 +00:00
|
|
|
class Function : public Castable<Function, Node> {
|
2020-03-02 20:47:43 +00:00
|
|
|
public:
|
2020-07-20 13:46:32 +00:00
|
|
|
/// Information about a binding
|
|
|
|
struct BindingInfo {
|
|
|
|
/// The binding decoration
|
|
|
|
BindingDecoration* binding = nullptr;
|
|
|
|
/// The set decoration
|
|
|
|
SetDecoration* set = nullptr;
|
|
|
|
};
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
/// Create a function
|
|
|
|
/// @param source the variable source
|
2020-12-11 18:24:53 +00:00
|
|
|
/// @param symbol the function symbol
|
2020-03-02 20:47:43 +00:00
|
|
|
/// @param name the function name
|
|
|
|
/// @param params the function parameters
|
|
|
|
/// @param return_type the return type
|
2020-11-14 01:13:04 +00:00
|
|
|
/// @param body the function body
|
2020-12-07 20:45:14 +00:00
|
|
|
/// @param decorations the function decorations
|
2020-03-02 20:47:43 +00:00
|
|
|
Function(const Source& source,
|
2020-12-11 18:24:53 +00:00
|
|
|
Symbol symbol,
|
2020-03-02 20:47:43 +00:00
|
|
|
const std::string& name,
|
2020-04-06 19:37:37 +00:00
|
|
|
VariableList params,
|
2020-11-14 01:13:04 +00:00
|
|
|
type::Type* return_type,
|
2020-12-07 20:45:14 +00:00
|
|
|
BlockStatement* body,
|
|
|
|
FunctionDecorationList decorations);
|
2020-03-02 20:47:43 +00:00
|
|
|
/// Move constructor
|
2020-04-09 18:52:06 +00:00
|
|
|
Function(Function&&);
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
~Function() override;
|
|
|
|
|
2020-12-11 18:24:53 +00:00
|
|
|
/// @returns the function symbol
|
|
|
|
Symbol symbol() const { return symbol_; }
|
2020-03-02 20:47:43 +00:00
|
|
|
/// @returns the function name
|
|
|
|
const std::string& name() { return name_; }
|
|
|
|
/// @returns the function params
|
2020-04-06 19:37:37 +00:00
|
|
|
const VariableList& params() const { return params_; }
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-09-20 19:18:58 +00:00
|
|
|
/// @returns the decorations attached to this function
|
|
|
|
const FunctionDecorationList& decorations() const { return decorations_; }
|
|
|
|
|
2020-09-21 00:28:58 +00:00
|
|
|
/// @returns the workgroup size {x, y, z} for the function. {1, 1, 1} will be
|
|
|
|
/// return if no workgroup size was set.
|
|
|
|
std::tuple<uint32_t, uint32_t, uint32_t> workgroup_size() const;
|
|
|
|
|
2020-09-21 17:51:31 +00:00
|
|
|
/// @returns the functions pipeline stage or None if not set
|
2020-11-30 23:30:58 +00:00
|
|
|
PipelineStage pipeline_stage() const;
|
2020-09-21 17:51:31 +00:00
|
|
|
|
2020-09-21 18:49:01 +00:00
|
|
|
/// @returns true if this function is an entry point
|
2020-11-30 23:30:58 +00:00
|
|
|
bool IsEntryPoint() const { return pipeline_stage() != PipelineStage::kNone; }
|
2020-09-21 18:49:01 +00:00
|
|
|
|
2020-06-22 20:52:24 +00:00
|
|
|
/// Adds the given variable to the list of referenced module variables if it
|
|
|
|
/// is not already included.
|
|
|
|
/// @param var the module variable to add
|
|
|
|
void add_referenced_module_variable(Variable* var);
|
2020-12-08 21:07:24 +00:00
|
|
|
/// Adds the given variable to the list of locally referenced module variables
|
|
|
|
/// if it is not already included.
|
|
|
|
/// @param var the module variable to add
|
|
|
|
void add_local_referenced_module_variable(Variable* var);
|
2020-10-08 18:38:45 +00:00
|
|
|
/// Note: If this function calls other functions, the return will also include
|
|
|
|
/// all of the referenced variables from the callees.
|
2020-06-22 20:52:24 +00:00
|
|
|
/// @returns the referenced module variables
|
|
|
|
const std::vector<Variable*>& referenced_module_variables() const {
|
|
|
|
return referenced_module_vars_;
|
|
|
|
}
|
2020-12-08 21:07:24 +00:00
|
|
|
/// @returns the locally referenced module variables
|
|
|
|
const std::vector<Variable*>& local_referenced_module_variables() const {
|
|
|
|
return local_referenced_module_vars_;
|
|
|
|
}
|
2020-07-15 18:26:25 +00:00
|
|
|
/// Retrieves any referenced location variables
|
|
|
|
/// @returns the <variable, decoration> pair.
|
|
|
|
const std::vector<std::pair<Variable*, LocationDecoration*>>
|
|
|
|
referenced_location_variables() const;
|
|
|
|
/// Retrieves any referenced builtin variables
|
|
|
|
/// @returns the <variable, decoration> pair.
|
|
|
|
const std::vector<std::pair<Variable*, BuiltinDecoration*>>
|
|
|
|
referenced_builtin_variables() const;
|
2020-11-10 22:30:56 +00:00
|
|
|
/// Retrieves any referenced uniform variables. Note, the variables must be
|
2020-07-20 13:46:32 +00:00
|
|
|
/// decorated with both binding and set decorations.
|
|
|
|
/// @returns the referenced uniforms
|
|
|
|
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
|
|
|
referenced_uniform_variables() const;
|
2020-11-10 22:30:56 +00:00
|
|
|
/// Retrieves any referenced storagebuffer variables. Note, the variables
|
2020-07-20 22:13:20 +00:00
|
|
|
/// must be decorated with both binding and set decorations.
|
|
|
|
/// @returns the referenced storagebuffers
|
|
|
|
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
|
|
|
referenced_storagebuffer_variables() const;
|
2020-11-05 20:09:50 +00:00
|
|
|
/// Retrieves any referenced regular Sampler variables. Note, the
|
2020-11-10 22:30:56 +00:00
|
|
|
/// variables must be decorated with both binding and set decorations.
|
2020-11-05 20:09:50 +00:00
|
|
|
/// @returns the referenced storagebuffers
|
|
|
|
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
|
|
|
referenced_sampler_variables() const;
|
|
|
|
/// Retrieves any referenced comparison Sampler variables. Note, the
|
2020-11-10 22:30:56 +00:00
|
|
|
/// variables must be decorated with both binding and set decorations.
|
2020-11-05 20:09:50 +00:00
|
|
|
/// @returns the referenced storagebuffers
|
|
|
|
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
|
|
|
referenced_comparison_sampler_variables() const;
|
2020-11-10 22:30:56 +00:00
|
|
|
/// Retrieves any referenced sampled textures variables. Note, the
|
|
|
|
/// variables must be decorated with both binding and set decorations.
|
|
|
|
/// @returns the referenced sampled textures
|
|
|
|
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
|
|
|
referenced_sampled_texture_variables() const;
|
2020-11-12 21:50:50 +00:00
|
|
|
/// Retrieves any referenced multisampled textures variables. Note, the
|
|
|
|
/// variables must be decorated with both binding and set decorations.
|
|
|
|
/// @returns the referenced sampled textures
|
|
|
|
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
|
|
|
referenced_multisampled_texture_variables() const;
|
2020-06-22 20:52:24 +00:00
|
|
|
|
2020-12-08 21:07:24 +00:00
|
|
|
/// Retrieves any locally referenced builtin variables
|
|
|
|
/// @returns the <variable, decoration> pairs.
|
|
|
|
const std::vector<std::pair<Variable*, BuiltinDecoration*>>
|
|
|
|
local_referenced_builtin_variables() const;
|
|
|
|
|
2020-07-14 19:45:47 +00:00
|
|
|
/// Adds an ancestor entry point
|
|
|
|
/// @param ep the entry point ancestor
|
2020-12-11 18:24:53 +00:00
|
|
|
void add_ancestor_entry_point(Symbol ep);
|
2020-07-14 19:45:47 +00:00
|
|
|
/// @returns the ancestor entry points
|
2020-12-11 18:24:53 +00:00
|
|
|
const std::vector<Symbol>& ancestor_entry_points() const {
|
2020-07-14 19:45:47 +00:00
|
|
|
return ancestor_entry_points_;
|
|
|
|
}
|
2020-09-03 01:46:36 +00:00
|
|
|
/// Checks if the given entry point is an ancestor
|
2020-12-11 18:24:53 +00:00
|
|
|
/// @param sym the entry point symbol
|
|
|
|
/// @returns true if `sym` is an ancestor entry point of this function
|
|
|
|
bool HasAncestorEntryPoint(Symbol sym) const;
|
2020-07-14 19:45:47 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
/// @returns the function return type.
|
|
|
|
type::Type* return_type() const { return return_type_; }
|
2020-08-12 20:06:29 +00:00
|
|
|
/// @returns a pointer to the last statement of the function or nullptr if
|
|
|
|
// function is empty
|
|
|
|
const Statement* get_last_statement() const;
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
/// @returns the function body
|
2020-11-16 16:31:07 +00:00
|
|
|
const BlockStatement* body() const { return body_; }
|
2020-09-29 18:40:40 +00:00
|
|
|
/// @returns the function body
|
2020-11-16 16:31:07 +00:00
|
|
|
BlockStatement* body() { return body_; }
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
/// Clones this node and all transitive child nodes using the `CloneContext`
|
|
|
|
/// `ctx`.
|
|
|
|
/// @note Semantic information such as resolved expression type and intrinsic
|
|
|
|
/// information is not cloned.
|
|
|
|
/// @param ctx the clone context
|
|
|
|
/// @return the newly cloned node
|
|
|
|
Function* Clone(CloneContext* ctx) const override;
|
|
|
|
|
2020-03-20 19:02:05 +00:00
|
|
|
/// @returns true if the name and type are both present
|
2020-03-02 20:47:43 +00:00
|
|
|
bool IsValid() const override;
|
|
|
|
|
|
|
|
/// Writes a representation of the node to the output stream
|
|
|
|
/// @param out the stream to write to
|
|
|
|
/// @param indent number of spaces to indent the node when writing
|
|
|
|
void to_str(std::ostream& out, size_t indent) const override;
|
|
|
|
|
2020-03-25 19:43:20 +00:00
|
|
|
/// @returns the type name for this function
|
|
|
|
std::string type_name() const;
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
private:
|
|
|
|
Function(const Function&) = delete;
|
2020-11-05 20:09:50 +00:00
|
|
|
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
|
|
|
ReferencedSamplerVariablesImpl(type::SamplerKind kind) const;
|
2020-11-12 21:50:50 +00:00
|
|
|
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
|
|
|
ReferencedSampledTextureVariablesImpl(bool multisampled) const;
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-12-11 18:24:53 +00:00
|
|
|
Symbol symbol_;
|
2020-03-02 20:47:43 +00:00
|
|
|
std::string name_;
|
2020-04-06 19:37:37 +00:00
|
|
|
VariableList params_;
|
2020-03-02 20:47:43 +00:00
|
|
|
type::Type* return_type_ = nullptr;
|
2020-11-16 16:31:07 +00:00
|
|
|
BlockStatement* body_ = nullptr;
|
2020-06-22 20:52:24 +00:00
|
|
|
std::vector<Variable*> referenced_module_vars_;
|
2020-12-08 21:07:24 +00:00
|
|
|
std::vector<Variable*> local_referenced_module_vars_;
|
2020-12-11 18:24:53 +00:00
|
|
|
std::vector<Symbol> ancestor_entry_points_;
|
2020-09-20 19:18:58 +00:00
|
|
|
FunctionDecorationList decorations_;
|
2020-03-02 20:47:43 +00:00
|
|
|
};
|
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
/// A list of functions
|
|
|
|
using FunctionList = std::vector<Function*>;
|
2020-04-06 19:37:37 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace ast
|
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // SRC_AST_FUNCTION_H_
|