2021-01-26 16:57:10 +00:00
|
|
|
// 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_PROGRAM_BUILDER_H_
|
|
|
|
#define SRC_PROGRAM_BUILDER_H_
|
|
|
|
|
|
|
|
#include <string>
|
2021-06-25 10:26:26 +00:00
|
|
|
#include <unordered_set>
|
2021-01-26 16:57:10 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2021-04-22 14:40:23 +00:00
|
|
|
#include "src/ast/alias.h"
|
2021-04-22 14:32:53 +00:00
|
|
|
#include "src/ast/array.h"
|
2021-03-09 10:26:57 +00:00
|
|
|
#include "src/ast/assignment_statement.h"
|
2021-06-17 15:48:39 +00:00
|
|
|
#include "src/ast/atomic.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/ast/binary_expression.h"
|
2022-02-02 23:07:11 +00:00
|
|
|
#include "src/ast/binding_attribute.h"
|
2021-06-29 12:37:45 +00:00
|
|
|
#include "src/ast/bitcast_expression.h"
|
2021-04-21 13:37:22 +00:00
|
|
|
#include "src/ast/bool.h"
|
2021-11-09 09:35:00 +00:00
|
|
|
#include "src/ast/bool_literal_expression.h"
|
2021-07-27 15:12:27 +00:00
|
|
|
#include "src/ast/break_statement.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/ast/call_expression.h"
|
2021-06-11 13:22:27 +00:00
|
|
|
#include "src/ast/call_statement.h"
|
2021-03-25 12:55:27 +00:00
|
|
|
#include "src/ast/case_statement.h"
|
2021-12-03 15:49:34 +00:00
|
|
|
#include "src/ast/continue_statement.h"
|
2021-07-26 22:19:48 +00:00
|
|
|
#include "src/ast/depth_multisampled_texture.h"
|
2021-04-22 14:40:23 +00:00
|
|
|
#include "src/ast/depth_texture.h"
|
2022-02-02 23:07:11 +00:00
|
|
|
#include "src/ast/disable_validation_attribute.h"
|
2021-11-22 11:44:57 +00:00
|
|
|
#include "src/ast/discard_statement.h"
|
2021-04-27 17:32:37 +00:00
|
|
|
#include "src/ast/external_texture.h"
|
2021-04-21 13:37:22 +00:00
|
|
|
#include "src/ast/f32.h"
|
2021-11-22 11:44:57 +00:00
|
|
|
#include "src/ast/fallthrough_statement.h"
|
2021-11-09 09:35:00 +00:00
|
|
|
#include "src/ast/float_literal_expression.h"
|
2021-07-02 19:27:42 +00:00
|
|
|
#include "src/ast/for_loop_statement.h"
|
2021-04-21 13:37:22 +00:00
|
|
|
#include "src/ast/i32.h"
|
2021-03-09 10:26:57 +00:00
|
|
|
#include "src/ast/if_statement.h"
|
2021-11-09 09:35:00 +00:00
|
|
|
#include "src/ast/index_accessor_expression.h"
|
2022-02-02 23:07:11 +00:00
|
|
|
#include "src/ast/interpolate_attribute.h"
|
|
|
|
#include "src/ast/invariant_attribute.h"
|
2021-03-09 10:26:57 +00:00
|
|
|
#include "src/ast/loop_statement.h"
|
2021-04-22 14:30:53 +00:00
|
|
|
#include "src/ast/matrix.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/ast/member_accessor_expression.h"
|
|
|
|
#include "src/ast/module.h"
|
2021-04-22 14:40:23 +00:00
|
|
|
#include "src/ast/multisampled_texture.h"
|
2022-02-02 23:07:11 +00:00
|
|
|
#include "src/ast/override_attribute.h"
|
2021-10-21 18:33:14 +00:00
|
|
|
#include "src/ast/phony_expression.h"
|
2021-04-22 14:40:23 +00:00
|
|
|
#include "src/ast/pointer.h"
|
2021-03-19 14:04:51 +00:00
|
|
|
#include "src/ast/return_statement.h"
|
2021-04-22 14:40:23 +00:00
|
|
|
#include "src/ast/sampled_texture.h"
|
2021-07-22 13:24:59 +00:00
|
|
|
#include "src/ast/sampler.h"
|
2021-11-09 09:35:00 +00:00
|
|
|
#include "src/ast/sint_literal_expression.h"
|
2022-02-02 23:07:11 +00:00
|
|
|
#include "src/ast/stage_attribute.h"
|
2021-04-22 14:40:23 +00:00
|
|
|
#include "src/ast/storage_texture.h"
|
2022-02-02 23:07:11 +00:00
|
|
|
#include "src/ast/stride_attribute.h"
|
|
|
|
#include "src/ast/struct_block_attribute.h"
|
|
|
|
#include "src/ast/struct_member_align_attribute.h"
|
|
|
|
#include "src/ast/struct_member_offset_attribute.h"
|
|
|
|
#include "src/ast/struct_member_size_attribute.h"
|
2021-03-25 12:55:27 +00:00
|
|
|
#include "src/ast/switch_statement.h"
|
2021-05-04 17:36:31 +00:00
|
|
|
#include "src/ast/type_name.h"
|
2021-04-21 13:37:22 +00:00
|
|
|
#include "src/ast/u32.h"
|
2021-11-09 09:35:00 +00:00
|
|
|
#include "src/ast/uint_literal_expression.h"
|
2021-05-17 15:51:47 +00:00
|
|
|
#include "src/ast/unary_op_expression.h"
|
2021-03-09 10:26:57 +00:00
|
|
|
#include "src/ast/variable_decl_statement.h"
|
2021-04-22 14:24:43 +00:00
|
|
|
#include "src/ast/vector.h"
|
2021-04-21 13:37:22 +00:00
|
|
|
#include "src/ast/void.h"
|
2022-02-02 23:07:11 +00:00
|
|
|
#include "src/ast/workgroup_attribute.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/program.h"
|
2021-04-13 23:27:27 +00:00
|
|
|
#include "src/program_id.h"
|
2021-05-07 20:58:34 +00:00
|
|
|
#include "src/sem/array.h"
|
2021-04-19 22:54:43 +00:00
|
|
|
#include "src/sem/bool_type.h"
|
2021-04-22 14:40:23 +00:00
|
|
|
#include "src/sem/depth_texture_type.h"
|
2021-04-27 17:32:37 +00:00
|
|
|
#include "src/sem/external_texture_type.h"
|
2021-04-19 22:54:43 +00:00
|
|
|
#include "src/sem/f32_type.h"
|
|
|
|
#include "src/sem/i32_type.h"
|
|
|
|
#include "src/sem/matrix_type.h"
|
2021-04-22 14:40:23 +00:00
|
|
|
#include "src/sem/multisampled_texture_type.h"
|
2021-04-19 22:54:43 +00:00
|
|
|
#include "src/sem/pointer_type.h"
|
2021-04-22 14:40:23 +00:00
|
|
|
#include "src/sem/sampled_texture_type.h"
|
|
|
|
#include "src/sem/storage_texture_type.h"
|
2021-05-07 14:49:34 +00:00
|
|
|
#include "src/sem/struct.h"
|
2021-04-19 22:54:43 +00:00
|
|
|
#include "src/sem/u32_type.h"
|
|
|
|
#include "src/sem/vector_type.h"
|
|
|
|
#include "src/sem/void_type.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-06-05 12:45:50 +00:00
|
|
|
#ifdef INCLUDE_TINT_TINT_H_
|
|
|
|
#error "internal tint header being #included from tint.h"
|
|
|
|
#endif
|
|
|
|
|
2021-02-03 17:19:59 +00:00
|
|
|
// Forward declarations
|
2021-06-25 10:26:26 +00:00
|
|
|
namespace tint {
|
2021-02-03 17:19:59 +00:00
|
|
|
namespace ast {
|
|
|
|
class VariableDeclStatement;
|
|
|
|
} // namespace ast
|
2021-06-25 10:26:26 +00:00
|
|
|
} // namespace tint
|
2021-02-03 17:19:59 +00:00
|
|
|
|
2021-06-25 10:26:26 +00:00
|
|
|
namespace tint {
|
2021-01-26 16:57:10 +00:00
|
|
|
class CloneContext;
|
|
|
|
|
|
|
|
/// ProgramBuilder is a mutable builder for a Program.
|
|
|
|
/// To construct a Program, populate the builder and then `std::move` it to a
|
|
|
|
/// Program.
|
|
|
|
class ProgramBuilder {
|
2021-07-17 18:09:26 +00:00
|
|
|
/// A helper used to disable overloads if the first type in `TYPES` is a
|
|
|
|
/// Source. Used to avoid ambiguities in overloads that take a Source as the
|
|
|
|
/// first parameter and those that perfectly-forward the first argument.
|
|
|
|
template <typename... TYPES>
|
|
|
|
using DisableIfSource = traits::EnableIfIsNotType<
|
|
|
|
traits::Decay<traits::NthTypeOf<0, TYPES..., void>>,
|
|
|
|
Source>;
|
|
|
|
|
2021-06-04 19:55:08 +00:00
|
|
|
/// VarOptionals is a helper for accepting a number of optional, extra
|
|
|
|
/// arguments for Var() and Global().
|
|
|
|
struct VarOptionals {
|
|
|
|
template <typename... ARGS>
|
|
|
|
explicit VarOptionals(ARGS&&... args) {
|
|
|
|
Apply(std::forward<ARGS>(args)...);
|
|
|
|
}
|
|
|
|
~VarOptionals();
|
|
|
|
|
|
|
|
ast::StorageClass storage = ast::StorageClass::kNone;
|
2021-06-04 20:41:47 +00:00
|
|
|
ast::Access access = ast::Access::kUndefined;
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Expression* constructor = nullptr;
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {};
|
2021-06-04 19:55:08 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void Set(ast::StorageClass sc) { storage = sc; }
|
2021-06-04 20:41:47 +00:00
|
|
|
void Set(ast::Access ac) { access = ac; }
|
2021-10-19 18:38:54 +00:00
|
|
|
void Set(const ast::Expression* c) { constructor = c; }
|
2022-02-02 23:07:11 +00:00
|
|
|
void Set(const ast::AttributeList& l) { attributes = l; }
|
2021-06-04 19:55:08 +00:00
|
|
|
|
|
|
|
template <typename FIRST, typename... ARGS>
|
|
|
|
void Apply(FIRST&& first, ARGS&&... args) {
|
|
|
|
Set(std::forward<FIRST>(first));
|
|
|
|
Apply(std::forward<ARGS>(args)...);
|
|
|
|
}
|
|
|
|
void Apply() {}
|
|
|
|
};
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
public:
|
2021-01-29 15:17:30 +00:00
|
|
|
/// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
|
|
|
|
using ASTNodeAllocator = BlockAllocator<ast::Node>;
|
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
/// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
|
|
|
|
using SemNodeAllocator = BlockAllocator<sem::Node>;
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
/// `i32` is a type alias to `int`.
|
|
|
|
/// Useful for passing to template methods such as `vec2<i32>()` to imitate
|
|
|
|
/// WGSL syntax.
|
|
|
|
/// Note: this is intentionally not aliased to uint32_t as we want integer
|
|
|
|
/// literals passed to the builder to match WGSL's integer literal types.
|
|
|
|
using i32 = decltype(1);
|
|
|
|
/// `u32` is a type alias to `unsigned int`.
|
|
|
|
/// Useful for passing to template methods such as `vec2<u32>()` to imitate
|
|
|
|
/// WGSL syntax.
|
|
|
|
/// Note: this is intentionally not aliased to uint32_t as we want integer
|
|
|
|
/// literals passed to the builder to match WGSL's integer literal types.
|
|
|
|
using u32 = decltype(1u);
|
|
|
|
/// `f32` is a type alias to `float`
|
|
|
|
/// Useful for passing to template methods such as `vec2<f32>()` to imitate
|
|
|
|
/// WGSL syntax.
|
|
|
|
using f32 = float;
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
ProgramBuilder();
|
|
|
|
|
|
|
|
/// Move constructor
|
|
|
|
/// @param rhs the builder to move
|
|
|
|
ProgramBuilder(ProgramBuilder&& rhs);
|
|
|
|
|
|
|
|
/// Destructor
|
|
|
|
virtual ~ProgramBuilder();
|
|
|
|
|
|
|
|
/// Move assignment operator
|
|
|
|
/// @param rhs the builder to move
|
|
|
|
/// @return this builder
|
|
|
|
ProgramBuilder& operator=(ProgramBuilder&& rhs);
|
|
|
|
|
2021-01-29 11:59:32 +00:00
|
|
|
/// Wrap returns a new ProgramBuilder wrapping the Program `program` without
|
|
|
|
/// making a deep clone of the Program contents.
|
|
|
|
/// ProgramBuilder returned by Wrap() is intended to temporarily extend an
|
|
|
|
/// existing immutable program.
|
|
|
|
/// As the returned ProgramBuilder wraps `program`, `program` must not be
|
|
|
|
/// destructed or assigned while using the returned ProgramBuilder.
|
|
|
|
/// TODO(bclayton) - Evaluate whether there are safer alternatives to this
|
|
|
|
/// function. See crbug.com/tint/460.
|
|
|
|
/// @param program the immutable Program to wrap
|
|
|
|
/// @return the ProgramBuilder that wraps `program`
|
|
|
|
static ProgramBuilder Wrap(const Program* program);
|
|
|
|
|
2021-04-13 23:27:27 +00:00
|
|
|
/// @returns the unique identifier for this program
|
|
|
|
ProgramID ID() const { return id_; }
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns a reference to the program's types
|
2021-04-19 22:51:23 +00:00
|
|
|
sem::Manager& Types() {
|
2021-01-26 16:57:10 +00:00
|
|
|
AssertNotMoved();
|
|
|
|
return types_;
|
|
|
|
}
|
|
|
|
|
2021-01-29 10:55:40 +00:00
|
|
|
/// @returns a reference to the program's types
|
2021-04-19 22:51:23 +00:00
|
|
|
const sem::Manager& Types() const {
|
2021-01-29 10:55:40 +00:00
|
|
|
AssertNotMoved();
|
|
|
|
return types_;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns a reference to the program's AST nodes storage
|
2021-01-29 15:17:30 +00:00
|
|
|
ASTNodeAllocator& ASTNodes() {
|
2021-01-26 16:57:10 +00:00
|
|
|
AssertNotMoved();
|
2021-01-29 15:17:30 +00:00
|
|
|
return ast_nodes_;
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 10:55:40 +00:00
|
|
|
/// @returns a reference to the program's AST nodes storage
|
2021-01-29 15:17:30 +00:00
|
|
|
const ASTNodeAllocator& ASTNodes() const {
|
|
|
|
AssertNotMoved();
|
|
|
|
return ast_nodes_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns a reference to the program's semantic nodes storage
|
|
|
|
SemNodeAllocator& SemNodes() {
|
|
|
|
AssertNotMoved();
|
|
|
|
return sem_nodes_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns a reference to the program's semantic nodes storage
|
|
|
|
const SemNodeAllocator& SemNodes() const {
|
2021-01-29 10:55:40 +00:00
|
|
|
AssertNotMoved();
|
2021-01-29 15:17:30 +00:00
|
|
|
return sem_nodes_;
|
2021-01-29 10:55:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns a reference to the program's AST root Module
|
|
|
|
ast::Module& AST() {
|
|
|
|
AssertNotMoved();
|
|
|
|
return *ast_;
|
|
|
|
}
|
|
|
|
|
2021-01-29 10:55:40 +00:00
|
|
|
/// @returns a reference to the program's AST root Module
|
|
|
|
const ast::Module& AST() const {
|
|
|
|
AssertNotMoved();
|
|
|
|
return *ast_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns a reference to the program's semantic info
|
2021-04-16 19:07:51 +00:00
|
|
|
sem::Info& Sem() {
|
2021-01-29 10:55:40 +00:00
|
|
|
AssertNotMoved();
|
|
|
|
return sem_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns a reference to the program's semantic info
|
2021-04-16 19:07:51 +00:00
|
|
|
const sem::Info& Sem() const {
|
2021-01-29 10:55:40 +00:00
|
|
|
AssertNotMoved();
|
|
|
|
return sem_;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns a reference to the program's SymbolTable
|
|
|
|
SymbolTable& Symbols() {
|
|
|
|
AssertNotMoved();
|
|
|
|
return symbols_;
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:22:40 +00:00
|
|
|
/// @returns a reference to the program's SymbolTable
|
|
|
|
const SymbolTable& Symbols() const {
|
|
|
|
AssertNotMoved();
|
|
|
|
return symbols_;
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:49:05 +00:00
|
|
|
/// @returns a reference to the program's diagnostics
|
|
|
|
diag::List& Diagnostics() {
|
|
|
|
AssertNotMoved();
|
|
|
|
return diagnostics_;
|
|
|
|
}
|
|
|
|
|
2021-01-29 10:55:40 +00:00
|
|
|
/// @returns a reference to the program's diagnostics
|
|
|
|
const diag::List& Diagnostics() const {
|
|
|
|
AssertNotMoved();
|
|
|
|
return diagnostics_;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
/// Controls whether the Resolver will be run on the program when it is built.
|
2021-01-27 19:23:55 +00:00
|
|
|
/// @param enable the new flag value (defaults to true)
|
|
|
|
void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
/// @return true if the Resolver will be run on the program when it is
|
2021-01-27 19:23:55 +00:00
|
|
|
/// built.
|
|
|
|
bool ResolveOnBuild() const { return resolve_on_build_; }
|
|
|
|
|
2021-01-27 18:49:05 +00:00
|
|
|
/// @returns true if the program has no error diagnostics and is not missing
|
|
|
|
/// information
|
2021-01-26 16:57:10 +00:00
|
|
|
bool IsValid() const;
|
|
|
|
|
2021-01-29 15:17:30 +00:00
|
|
|
/// Creates a new ast::Node owned by the ProgramBuilder. When the
|
|
|
|
/// ProgramBuilder is destructed, the ast::Node will also be destructed.
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param args the arguments to pass to the type constructor
|
|
|
|
/// @returns the node pointer
|
|
|
|
template <typename T, typename... ARGS>
|
|
|
|
traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
|
|
|
|
ARGS&&... args) {
|
|
|
|
AssertNotMoved();
|
2021-04-13 23:27:27 +00:00
|
|
|
return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 15:17:30 +00:00
|
|
|
/// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
|
|
|
|
/// Source as set by the last call to SetSource() as the only argument to the
|
2021-01-26 16:57:10 +00:00
|
|
|
/// constructor.
|
2021-01-29 15:17:30 +00:00
|
|
|
/// When the ProgramBuilder is destructed, the ast::Node will also be
|
|
|
|
/// destructed.
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns the node pointer
|
|
|
|
template <typename T>
|
|
|
|
traits::EnableIfIsType<T, ast::Node>* create() {
|
|
|
|
AssertNotMoved();
|
2021-04-13 23:27:27 +00:00
|
|
|
return ast_nodes_.Create<T>(id_, source_);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 15:17:30 +00:00
|
|
|
/// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
|
|
|
|
/// Source as set by the last call to SetSource() as the first argument to the
|
2021-01-26 16:57:10 +00:00
|
|
|
/// constructor.
|
2021-01-29 15:17:30 +00:00
|
|
|
/// When the ProgramBuilder is destructed, the ast::Node will also be
|
|
|
|
/// destructed.
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param arg0 the first arguments to pass to the type constructor
|
|
|
|
/// @param args the remaining arguments to pass to the type constructor
|
|
|
|
/// @returns the node pointer
|
|
|
|
template <typename T, typename ARG0, typename... ARGS>
|
|
|
|
traits::EnableIf</* T is ast::Node and ARG0 is not Source */
|
|
|
|
traits::IsTypeOrDerived<T, ast::Node>::value &&
|
|
|
|
!traits::IsTypeOrDerived<ARG0, Source>::value,
|
|
|
|
T>*
|
|
|
|
create(ARG0&& arg0, ARGS&&... args) {
|
|
|
|
AssertNotMoved();
|
2021-04-13 23:27:27 +00:00
|
|
|
return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
|
2021-01-29 15:17:30 +00:00
|
|
|
std::forward<ARGS>(args)...);
|
|
|
|
}
|
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
/// Creates a new sem::Node owned by the ProgramBuilder.
|
|
|
|
/// When the ProgramBuilder is destructed, the sem::Node will also be
|
2021-01-29 15:17:30 +00:00
|
|
|
/// destructed.
|
|
|
|
/// @param args the arguments to pass to the type constructor
|
|
|
|
/// @returns the node pointer
|
|
|
|
template <typename T, typename... ARGS>
|
2021-05-06 15:52:33 +00:00
|
|
|
traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
|
|
|
|
!traits::IsTypeOrDerived<T, sem::Type>::value,
|
|
|
|
T>*
|
|
|
|
create(ARGS&&... args) {
|
2021-01-29 15:17:30 +00:00
|
|
|
AssertNotMoved();
|
|
|
|
return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 22:51:23 +00:00
|
|
|
/// Creates a new sem::Type owned by the ProgramBuilder.
|
2021-01-29 15:17:30 +00:00
|
|
|
/// When the ProgramBuilder is destructed, owned ProgramBuilder and the
|
|
|
|
/// returned`Type` will also be destructed.
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Types are unique (de-aliased), and so calling create() for the same `T`
|
|
|
|
/// and arguments will return the same pointer.
|
|
|
|
/// @warning Use this method to acquire a type only if all of its type
|
|
|
|
/// information is provided in the constructor arguments `args`.<br>
|
|
|
|
/// If the type requires additional configuration after construction that
|
|
|
|
/// affect its fundamental type, build the type with `std::make_unique`, make
|
|
|
|
/// any necessary alterations and then call unique_type() instead.
|
|
|
|
/// @param args the arguments to pass to the type constructor
|
|
|
|
/// @returns the de-aliased type pointer
|
|
|
|
template <typename T, typename... ARGS>
|
2021-04-19 22:51:23 +00:00
|
|
|
traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
|
|
|
|
static_assert(std::is_base_of<sem::Type, T>::value,
|
|
|
|
"T does not derive from sem::Type");
|
2021-01-26 16:57:10 +00:00
|
|
|
AssertNotMoved();
|
|
|
|
return types_.Get<T>(std::forward<ARGS>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Marks this builder as moved, preventing any further use of the builder.
|
|
|
|
void MarkAsMoved();
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TypesBuilder
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/// TypesBuilder holds basic `tint` types and methods for constructing
|
|
|
|
/// complex types.
|
|
|
|
class TypesBuilder {
|
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
/// @param builder the program builder
|
|
|
|
explicit TypesBuilder(ProgramBuilder* builder);
|
|
|
|
|
|
|
|
/// @return the tint AST type for the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Type* Of() const {
|
2021-01-26 16:57:10 +00:00
|
|
|
return CToAST<T>::get(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns a boolean type
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @returns a boolean type
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Bool* bool_(const Source& source) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::Bool>(source);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns a f32 type
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::F32* f32() const { return builder->create<ast::F32>(); }
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @returns a f32 type
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::F32* f32(const Source& source) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::F32>(source);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns a i32 type
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::I32* i32() const { return builder->create<ast::I32>(); }
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @returns a i32 type
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::I32* i32(const Source& source) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::I32>(source);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns a u32 type
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::U32* u32() const { return builder->create<ast::U32>(); }
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @returns a u32 type
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::U32* u32(const Source& source) const {
|
2021-05-19 17:47:11 +00:00
|
|
|
return builder->create<ast::U32>(source);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns a void type
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Void* void_() const { return builder->create<ast::Void>(); }
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @returns a void type
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Void* void_(const Source& source) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::Void>(source);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 13:32:33 +00:00
|
|
|
/// @param type vector subtype
|
2021-04-22 14:40:23 +00:00
|
|
|
/// @param n vector width in elements
|
|
|
|
/// @return the tint AST type for a `n`-element vector of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Vector* vec(const ast::Type* type, uint32_t n) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::Vector>(type, n);
|
2021-04-13 13:32:33 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param type vector subtype
|
|
|
|
/// @param n vector width in elements
|
|
|
|
/// @return the tint AST type for a `n`-element vector of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Vector* vec(const Source& source,
|
|
|
|
const ast::Type* type,
|
|
|
|
uint32_t n) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::Vector>(source, type, n);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 14:40:23 +00:00
|
|
|
/// @param type vector subtype
|
|
|
|
/// @return the tint AST type for a 2-element vector of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Vector* vec2(const ast::Type* type) const {
|
|
|
|
return vec(type, 2u);
|
|
|
|
}
|
2021-04-22 14:40:23 +00:00
|
|
|
|
2021-04-13 13:32:33 +00:00
|
|
|
/// @param type vector subtype
|
|
|
|
/// @return the tint AST type for a 3-element vector of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Vector* vec3(const ast::Type* type) const {
|
|
|
|
return vec(type, 3u);
|
|
|
|
}
|
2021-04-13 13:32:33 +00:00
|
|
|
|
|
|
|
/// @param type vector subtype
|
|
|
|
/// @return the tint AST type for a 4-element vector of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Vector* vec4(const ast::Type* type) const {
|
|
|
|
return vec(type, 4u);
|
|
|
|
}
|
2021-04-22 14:40:23 +00:00
|
|
|
|
|
|
|
/// @param n vector width in elements
|
|
|
|
/// @return the tint AST type for a `n`-element vector of `type`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Vector* vec(uint32_t n) const {
|
2021-04-22 14:40:23 +00:00
|
|
|
return vec(Of<T>(), n);
|
2021-04-13 13:32:33 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @return the tint AST type for a 2-element vector of the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Vector* vec2() const {
|
2021-04-13 13:32:33 +00:00
|
|
|
return vec2(Of<T>());
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @return the tint AST type for a 3-element vector of the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Vector* vec3() const {
|
2021-04-13 13:32:33 +00:00
|
|
|
return vec3(Of<T>());
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @return the tint AST type for a 4-element vector of the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Vector* vec4() const {
|
2021-04-13 13:32:33 +00:00
|
|
|
return vec4(Of<T>());
|
|
|
|
}
|
|
|
|
|
2021-04-22 14:40:23 +00:00
|
|
|
/// @param type matrix subtype
|
|
|
|
/// @param columns number of columns for the matrix
|
|
|
|
/// @param rows number of rows for the matrix
|
|
|
|
/// @return the tint AST type for a matrix of `type`
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat(const ast::Type* type,
|
|
|
|
uint32_t columns,
|
|
|
|
uint32_t rows) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::Matrix>(type, rows, columns);
|
2021-04-22 14:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param type matrix subtype
|
|
|
|
/// @param columns number of columns for the matrix
|
|
|
|
/// @param rows number of rows for the matrix
|
|
|
|
/// @return the tint AST type for a matrix of `type`
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat(const Source& source,
|
|
|
|
const ast::Type* type,
|
|
|
|
uint32_t columns,
|
|
|
|
uint32_t rows) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::Matrix>(source, type, rows, columns);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 13:32:33 +00:00
|
|
|
/// @param type matrix subtype
|
|
|
|
/// @return the tint AST type for a 2x3 matrix of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat2x2(const ast::Type* type) const {
|
|
|
|
return mat(type, 2u, 2u);
|
|
|
|
}
|
2021-04-13 13:32:33 +00:00
|
|
|
|
|
|
|
/// @param type matrix subtype
|
|
|
|
/// @return the tint AST type for a 2x3 matrix of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat2x3(const ast::Type* type) const {
|
|
|
|
return mat(type, 2u, 3u);
|
|
|
|
}
|
2021-04-13 13:32:33 +00:00
|
|
|
|
|
|
|
/// @param type matrix subtype
|
|
|
|
/// @return the tint AST type for a 2x4 matrix of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat2x4(const ast::Type* type) const {
|
|
|
|
return mat(type, 2u, 4u);
|
|
|
|
}
|
2021-04-13 13:32:33 +00:00
|
|
|
|
|
|
|
/// @param type matrix subtype
|
|
|
|
/// @return the tint AST type for a 3x2 matrix of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat3x2(const ast::Type* type) const {
|
|
|
|
return mat(type, 3u, 2u);
|
|
|
|
}
|
2021-04-13 13:32:33 +00:00
|
|
|
|
|
|
|
/// @param type matrix subtype
|
|
|
|
/// @return the tint AST type for a 3x3 matrix of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat3x3(const ast::Type* type) const {
|
|
|
|
return mat(type, 3u, 3u);
|
|
|
|
}
|
2021-04-13 13:32:33 +00:00
|
|
|
|
|
|
|
/// @param type matrix subtype
|
|
|
|
/// @return the tint AST type for a 3x4 matrix of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat3x4(const ast::Type* type) const {
|
|
|
|
return mat(type, 3u, 4u);
|
|
|
|
}
|
2021-04-13 13:32:33 +00:00
|
|
|
|
|
|
|
/// @param type matrix subtype
|
|
|
|
/// @return the tint AST type for a 4x2 matrix of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat4x2(const ast::Type* type) const {
|
|
|
|
return mat(type, 4u, 2u);
|
|
|
|
}
|
2021-04-13 13:32:33 +00:00
|
|
|
|
|
|
|
/// @param type matrix subtype
|
|
|
|
/// @return the tint AST type for a 4x3 matrix of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat4x3(const ast::Type* type) const {
|
|
|
|
return mat(type, 4u, 3u);
|
|
|
|
}
|
2021-04-13 13:32:33 +00:00
|
|
|
|
|
|
|
/// @param type matrix subtype
|
|
|
|
/// @return the tint AST type for a 4x4 matrix of `type`.
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat4x4(const ast::Type* type) const {
|
|
|
|
return mat(type, 4u, 4u);
|
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-04-22 14:40:23 +00:00
|
|
|
/// @param columns number of columns for the matrix
|
|
|
|
/// @param rows number of rows for the matrix
|
|
|
|
/// @return the tint AST type for a matrix of `type`
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
|
2021-04-22 14:40:23 +00:00
|
|
|
return mat(Of<T>(), columns, rows);
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @return the tint AST type for a 2x3 matrix of the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat2x2() const {
|
2021-04-13 13:32:33 +00:00
|
|
|
return mat2x2(Of<T>());
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @return the tint AST type for a 2x3 matrix of the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat2x3() const {
|
2021-04-13 13:32:33 +00:00
|
|
|
return mat2x3(Of<T>());
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @return the tint AST type for a 2x4 matrix of the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat2x4() const {
|
2021-04-13 13:32:33 +00:00
|
|
|
return mat2x4(Of<T>());
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @return the tint AST type for a 3x2 matrix of the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat3x2() const {
|
2021-04-13 13:32:33 +00:00
|
|
|
return mat3x2(Of<T>());
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @return the tint AST type for a 3x3 matrix of the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat3x3() const {
|
2021-04-13 13:32:33 +00:00
|
|
|
return mat3x3(Of<T>());
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @return the tint AST type for a 3x4 matrix of the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat3x4() const {
|
2021-04-13 13:32:33 +00:00
|
|
|
return mat3x4(Of<T>());
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @return the tint AST type for a 4x2 matrix of the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat4x2() const {
|
2021-04-13 13:32:33 +00:00
|
|
|
return mat4x2(Of<T>());
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @return the tint AST type for a 4x3 matrix of the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat4x3() const {
|
2021-04-13 13:32:33 +00:00
|
|
|
return mat4x3(Of<T>());
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @return the tint AST type for a 4x4 matrix of the C type `T`.
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Matrix* mat4x4() const {
|
2021-04-13 13:32:33 +00:00
|
|
|
return mat4x4(Of<T>());
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param subtype the array element type
|
2021-09-02 13:49:59 +00:00
|
|
|
/// @param n the array size. nullptr represents a runtime-array
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attrs the optional attributes for the array
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @return the tint AST type for a array of size `n` of type `T`
|
2021-09-02 13:49:59 +00:00
|
|
|
template <typename EXPR = ast::Expression*>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Array* array(const ast::Type* subtype,
|
|
|
|
EXPR&& n = nullptr,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attrs = {}) const {
|
2021-09-02 13:49:59 +00:00
|
|
|
return builder->create<ast::Array>(
|
2022-02-02 23:07:11 +00:00
|
|
|
subtype, builder->Expr(std::forward<EXPR>(n)), attrs);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param subtype the array element type
|
2021-09-02 13:49:59 +00:00
|
|
|
/// @param n the array size. nullptr represents a runtime-array
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attrs the optional attributes for the array
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @return the tint AST type for a array of size `n` of type `T`
|
2021-09-02 13:49:59 +00:00
|
|
|
template <typename EXPR = ast::Expression*>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Array* array(const Source& source,
|
|
|
|
const ast::Type* subtype,
|
|
|
|
EXPR&& n = nullptr,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attrs = {}) const {
|
2021-09-02 13:49:59 +00:00
|
|
|
return builder->create<ast::Array>(
|
2022-02-02 23:07:11 +00:00
|
|
|
source, subtype, builder->Expr(std::forward<EXPR>(n)), attrs);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 21:16:21 +00:00
|
|
|
/// @param subtype the array element type
|
2021-09-02 13:49:59 +00:00
|
|
|
/// @param n the array size. nullptr represents a runtime-array
|
2021-05-10 17:25:21 +00:00
|
|
|
/// @param stride the array stride. 0 represents implicit stride
|
2021-02-08 21:16:21 +00:00
|
|
|
/// @return the tint AST type for a array of size `n` of type `T`
|
2021-09-02 13:49:59 +00:00
|
|
|
template <typename EXPR>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Array* array(const ast::Type* subtype,
|
|
|
|
EXPR&& n,
|
|
|
|
uint32_t stride) const {
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attrs;
|
2021-05-10 17:25:21 +00:00
|
|
|
if (stride) {
|
2022-02-02 23:07:11 +00:00
|
|
|
attrs.emplace_back(builder->create<ast::StrideAttribute>(stride));
|
2021-05-10 17:25:21 +00:00
|
|
|
}
|
2022-02-02 23:07:11 +00:00
|
|
|
return array(subtype, std::forward<EXPR>(n), std::move(attrs));
|
2021-02-08 21:16:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 17:36:31 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param subtype the array element type
|
2021-09-02 13:49:59 +00:00
|
|
|
/// @param n the array size. nullptr represents a runtime-array
|
2021-05-10 17:25:21 +00:00
|
|
|
/// @param stride the array stride. 0 represents implicit stride
|
2021-05-04 17:36:31 +00:00
|
|
|
/// @return the tint AST type for a array of size `n` of type `T`
|
2021-09-02 13:49:59 +00:00
|
|
|
template <typename EXPR>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Array* array(const Source& source,
|
|
|
|
const ast::Type* subtype,
|
|
|
|
EXPR&& n,
|
|
|
|
uint32_t stride) const {
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attrs;
|
2021-05-10 17:25:21 +00:00
|
|
|
if (stride) {
|
2022-02-02 23:07:11 +00:00
|
|
|
attrs.emplace_back(builder->create<ast::StrideAttribute>(stride));
|
2021-05-10 17:25:21 +00:00
|
|
|
}
|
2022-02-02 23:07:11 +00:00
|
|
|
return array(source, subtype, std::forward<EXPR>(n), std::move(attrs));
|
2021-09-02 13:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @return the tint AST type for a runtime-sized array of type `T`
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Array* array() const {
|
2021-09-02 13:49:59 +00:00
|
|
|
return array(Of<T>(), nullptr);
|
2021-05-04 17:36:31 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @return the tint AST type for an array of size `N` of type `T`
|
2021-09-02 13:49:59 +00:00
|
|
|
template <typename T, int N>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Array* array() const {
|
2021-09-02 13:49:59 +00:00
|
|
|
return array(Of<T>(), builder->Expr(N));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param stride the array stride
|
|
|
|
/// @return the tint AST type for a runtime-sized array of type `T`
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Array* array(uint32_t stride) const {
|
2021-09-02 13:49:59 +00:00
|
|
|
return array(Of<T>(), nullptr, stride);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 21:16:21 +00:00
|
|
|
/// @param stride the array stride
|
|
|
|
/// @return the tint AST type for an array of size `N` of type `T`
|
2021-09-02 13:49:59 +00:00
|
|
|
template <typename T, int N>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Array* array(uint32_t stride) const {
|
2021-09-02 13:49:59 +00:00
|
|
|
return array(Of<T>(), builder->Expr(N), stride);
|
2021-02-08 21:16:21 +00:00
|
|
|
}
|
2021-04-16 08:47:14 +00:00
|
|
|
|
2021-05-04 17:36:31 +00:00
|
|
|
/// Creates a type name
|
|
|
|
/// @param name the name
|
|
|
|
/// @returns the type name
|
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::TypeName* type_name(NAME&& name) const {
|
2021-05-04 17:36:31 +00:00
|
|
|
return builder->create<ast::TypeName>(
|
|
|
|
builder->Sym(std::forward<NAME>(name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a type name
|
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param name the name
|
|
|
|
/// @returns the type name
|
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::TypeName* type_name(const Source& source, NAME&& name) const {
|
2021-05-04 17:36:31 +00:00
|
|
|
return builder->create<ast::TypeName>(
|
|
|
|
source, builder->Sym(std::forward<NAME>(name)));
|
|
|
|
}
|
|
|
|
|
2021-02-02 14:29:15 +00:00
|
|
|
/// Creates an alias type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param name the alias name
|
|
|
|
/// @param type the alias type
|
|
|
|
/// @returns the alias pointer
|
2021-04-09 16:19:48 +00:00
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Alias* alias(NAME&& name, const ast::Type* type) const {
|
2021-04-22 14:40:23 +00:00
|
|
|
auto sym = builder->Sym(std::forward<NAME>(name));
|
2021-05-10 17:38:01 +00:00
|
|
|
return builder->create<ast::Alias>(sym, type);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// Creates an alias type
|
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param name the alias name
|
|
|
|
/// @param type the alias type
|
|
|
|
/// @returns the alias pointer
|
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Alias* alias(const Source& source,
|
|
|
|
NAME&& name,
|
|
|
|
const ast::Type* type) const {
|
2021-04-27 17:32:37 +00:00
|
|
|
auto sym = builder->Sym(std::forward<NAME>(name));
|
2021-05-10 17:38:01 +00:00
|
|
|
return builder->create<ast::Alias>(source, sym, type);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 12:46:52 +00:00
|
|
|
/// @param type the type of the pointer
|
|
|
|
/// @param storage_class the storage class of the pointer
|
2021-06-04 22:17:37 +00:00
|
|
|
/// @param access the optional access control of the pointer
|
2021-04-22 14:40:23 +00:00
|
|
|
/// @return the pointer to `type` with the given ast::StorageClass
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Pointer* pointer(
|
|
|
|
const ast::Type* type,
|
|
|
|
ast::StorageClass storage_class,
|
|
|
|
ast::Access access = ast::Access::kUndefined) const {
|
2021-06-04 22:17:37 +00:00
|
|
|
return builder->create<ast::Pointer>(type, storage_class, access);
|
2021-03-31 12:46:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param type the type of the pointer
|
|
|
|
/// @param storage_class the storage class of the pointer
|
2021-06-04 22:17:37 +00:00
|
|
|
/// @param access the optional access control of the pointer
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @return the pointer to `type` with the given ast::StorageClass
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Pointer* pointer(
|
|
|
|
const Source& source,
|
|
|
|
const ast::Type* type,
|
|
|
|
ast::StorageClass storage_class,
|
|
|
|
ast::Access access = ast::Access::kUndefined) const {
|
2021-06-04 22:17:37 +00:00
|
|
|
return builder->create<ast::Pointer>(source, type, storage_class, access);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param storage_class the storage class of the pointer
|
2021-06-04 22:17:37 +00:00
|
|
|
/// @param access the optional access control of the pointer
|
2021-04-22 14:40:23 +00:00
|
|
|
/// @return the pointer to type `T` with the given ast::StorageClass.
|
2021-01-26 16:57:10 +00:00
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Pointer* pointer(
|
|
|
|
ast::StorageClass storage_class,
|
|
|
|
ast::Access access = ast::Access::kUndefined) const {
|
2021-06-04 22:17:37 +00:00
|
|
|
return pointer(Of<T>(), storage_class, access);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 15:48:39 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param type the type of the atomic
|
|
|
|
/// @return the atomic to `type`
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Atomic* atomic(const Source& source,
|
|
|
|
const ast::Type* type) const {
|
2021-06-17 15:48:39 +00:00
|
|
|
return builder->create<ast::Atomic>(source, type);
|
|
|
|
}
|
|
|
|
|
2021-06-17 19:56:14 +00:00
|
|
|
/// @param type the type of the atomic
|
|
|
|
/// @return the atomic to `type`
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Atomic* atomic(const ast::Type* type) const {
|
2021-06-17 19:56:14 +00:00
|
|
|
return builder->create<ast::Atomic>(type);
|
|
|
|
}
|
|
|
|
|
2021-06-17 15:48:39 +00:00
|
|
|
/// @return the atomic to type `T`
|
|
|
|
template <typename T>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Atomic* atomic() const {
|
2021-06-17 15:48:39 +00:00
|
|
|
return atomic(Of<T>());
|
|
|
|
}
|
|
|
|
|
2021-04-22 14:40:23 +00:00
|
|
|
/// @param kind the kind of sampler
|
|
|
|
/// @returns the sampler
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Sampler* sampler(ast::SamplerKind kind) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::Sampler>(kind);
|
2021-04-22 14:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param kind the kind of sampler
|
|
|
|
/// @returns the sampler
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Sampler* sampler(const Source& source,
|
|
|
|
ast::SamplerKind kind) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::Sampler>(source, kind);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 14:40:23 +00:00
|
|
|
/// @param dims the dimensionality of the texture
|
|
|
|
/// @returns the depth texture
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::DepthTexture>(dims);
|
2021-04-22 14:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param dims the dimensionality of the texture
|
|
|
|
/// @returns the depth texture
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::DepthTexture* depth_texture(const Source& source,
|
|
|
|
ast::TextureDimension dims) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::DepthTexture>(source, dims);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 22:19:48 +00:00
|
|
|
/// @param dims the dimensionality of the texture
|
|
|
|
/// @returns the multisampled depth texture
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::DepthMultisampledTexture* depth_multisampled_texture(
|
2021-07-26 22:19:48 +00:00
|
|
|
ast::TextureDimension dims) const {
|
|
|
|
return builder->create<ast::DepthMultisampledTexture>(dims);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param dims the dimensionality of the texture
|
|
|
|
/// @returns the multisampled depth texture
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::DepthMultisampledTexture* depth_multisampled_texture(
|
2021-07-26 22:19:48 +00:00
|
|
|
const Source& source,
|
|
|
|
ast::TextureDimension dims) const {
|
|
|
|
return builder->create<ast::DepthMultisampledTexture>(source, dims);
|
|
|
|
}
|
|
|
|
|
2021-04-22 14:40:23 +00:00
|
|
|
/// @param dims the dimensionality of the texture
|
|
|
|
/// @param subtype the texture subtype.
|
|
|
|
/// @returns the sampled texture
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
|
|
|
|
const ast::Type* subtype) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::SampledTexture>(dims, subtype);
|
2021-04-22 14:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param dims the dimensionality of the texture
|
|
|
|
/// @param subtype the texture subtype.
|
|
|
|
/// @returns the sampled texture
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::SampledTexture* sampled_texture(const Source& source,
|
|
|
|
ast::TextureDimension dims,
|
|
|
|
const ast::Type* subtype) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::SampledTexture>(source, dims, subtype);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 14:40:23 +00:00
|
|
|
/// @param dims the dimensionality of the texture
|
|
|
|
/// @param subtype the texture subtype.
|
|
|
|
/// @returns the multisampled texture
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::MultisampledTexture* multisampled_texture(
|
|
|
|
ast::TextureDimension dims,
|
|
|
|
const ast::Type* subtype) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::MultisampledTexture>(dims, subtype);
|
2021-04-22 14:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param dims the dimensionality of the texture
|
|
|
|
/// @param subtype the texture subtype.
|
|
|
|
/// @returns the multisampled texture
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::MultisampledTexture* multisampled_texture(
|
|
|
|
const Source& source,
|
|
|
|
ast::TextureDimension dims,
|
|
|
|
const ast::Type* subtype) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::MultisampledTexture>(source, dims, subtype);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 14:40:23 +00:00
|
|
|
/// @param dims the dimensionality of the texture
|
2022-01-06 17:20:22 +00:00
|
|
|
/// @param format the texel format of the texture
|
2021-06-04 20:41:47 +00:00
|
|
|
/// @param access the access control of the texture
|
2021-04-22 14:40:23 +00:00
|
|
|
/// @returns the storage texture
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::StorageTexture* storage_texture(ast::TextureDimension dims,
|
2022-01-06 17:20:22 +00:00
|
|
|
ast::TexelFormat format,
|
2021-10-19 18:38:54 +00:00
|
|
|
ast::Access access) const {
|
2021-05-20 14:42:28 +00:00
|
|
|
auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
|
2021-06-04 20:41:47 +00:00
|
|
|
return builder->create<ast::StorageTexture>(dims, format, subtype,
|
|
|
|
access);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @param dims the dimensionality of the texture
|
2022-01-06 17:20:22 +00:00
|
|
|
/// @param format the texel format of the texture
|
2021-06-04 20:41:47 +00:00
|
|
|
/// @param access the access control of the texture
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @returns the storage texture
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::StorageTexture* storage_texture(const Source& source,
|
|
|
|
ast::TextureDimension dims,
|
2022-01-06 17:20:22 +00:00
|
|
|
ast::TexelFormat format,
|
2021-10-19 18:38:54 +00:00
|
|
|
ast::Access access) const {
|
2021-05-20 14:42:28 +00:00
|
|
|
auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
|
2021-06-04 20:41:47 +00:00
|
|
|
return builder->create<ast::StorageTexture>(source, dims, format, subtype,
|
|
|
|
access);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 17:40:17 +00:00
|
|
|
/// @returns the external texture
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::ExternalTexture* external_texture() const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::ExternalTexture>();
|
2021-05-17 17:40:17 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:32:37 +00:00
|
|
|
/// @param source the Source of the node
|
|
|
|
/// @returns the external texture
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::ExternalTexture* external_texture(const Source& source) const {
|
2021-05-20 15:04:08 +00:00
|
|
|
return builder->create<ast::ExternalTexture>(source);
|
2021-04-27 17:32:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 14:32:14 +00:00
|
|
|
/// Constructs a TypeName for the type declaration.
|
2021-04-23 15:41:34 +00:00
|
|
|
/// @param type the type
|
|
|
|
/// @return either type or a pointer to a new ast::TypeName
|
2021-06-09 14:32:14 +00:00
|
|
|
const ast::TypeName* Of(const ast::TypeDecl* type) const;
|
2021-04-23 15:41:34 +00:00
|
|
|
|
2021-04-28 14:31:23 +00:00
|
|
|
/// The ProgramBuilder
|
|
|
|
ProgramBuilder* const builder;
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
private:
|
|
|
|
/// CToAST<T> is specialized for various `T` types and each specialization
|
|
|
|
/// contains a single static `get()` method for obtaining the corresponding
|
|
|
|
/// AST type for the C type `T`.
|
|
|
|
/// `get()` has the signature:
|
2021-10-19 18:38:54 +00:00
|
|
|
/// `static const ast::Type* get(Types* t)`
|
2021-01-26 16:57:10 +00:00
|
|
|
template <typename T>
|
|
|
|
struct CToAST {};
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// AST helper methods
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-04-29 12:59:14 +00:00
|
|
|
/// @return a new unnamed symbol
|
|
|
|
Symbol Sym() { return Symbols().New(); }
|
|
|
|
|
2021-04-07 11:16:01 +00:00
|
|
|
/// @param name the symbol string
|
|
|
|
/// @return a Symbol with the given name
|
|
|
|
Symbol Sym(const std::string& name) { return Symbols().Register(name); }
|
|
|
|
|
|
|
|
/// @param sym the symbol
|
|
|
|
/// @return `sym`
|
|
|
|
Symbol Sym(Symbol sym) { return sym; }
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param expr the expression
|
|
|
|
/// @return expr
|
2021-02-24 14:15:02 +00:00
|
|
|
template <typename T>
|
|
|
|
traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
|
|
|
|
return expr;
|
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-04-19 16:52:42 +00:00
|
|
|
/// Passthrough for nullptr
|
|
|
|
/// @return nullptr
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
|
2021-04-19 16:52:42 +00:00
|
|
|
|
2021-05-17 15:51:47 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param symbol the identifier symbol
|
|
|
|
/// @return an ast::IdentifierExpression with the given symbol
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
|
2021-05-17 15:51:47 +00:00
|
|
|
return create<ast::IdentifierExpression>(source, symbol);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 21:34:26 +00:00
|
|
|
/// @param symbol the identifier symbol
|
|
|
|
/// @return an ast::IdentifierExpression with the given symbol
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::IdentifierExpression* Expr(Symbol symbol) {
|
2021-02-10 21:34:26 +00:00
|
|
|
return create<ast::IdentifierExpression>(symbol);
|
|
|
|
}
|
|
|
|
|
2021-05-17 15:51:47 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param variable the AST variable
|
|
|
|
/// @return an ast::IdentifierExpression with the variable's symbol
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::IdentifierExpression* Expr(const Source& source,
|
|
|
|
const ast::Variable* variable) {
|
2021-10-15 17:33:10 +00:00
|
|
|
return create<ast::IdentifierExpression>(source, variable->symbol);
|
2021-05-17 15:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 16:52:42 +00:00
|
|
|
/// @param variable the AST variable
|
|
|
|
/// @return an ast::IdentifierExpression with the variable's symbol
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::IdentifierExpression* Expr(const ast::Variable* variable) {
|
2021-10-15 17:33:10 +00:00
|
|
|
return create<ast::IdentifierExpression>(variable->symbol);
|
2021-04-19 16:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 15:51:47 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param name the identifier name
|
|
|
|
/// @return an ast::IdentifierExpression with the given name
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::IdentifierExpression* Expr(const Source& source,
|
|
|
|
const char* name) {
|
2021-05-17 15:51:47 +00:00
|
|
|
return create<ast::IdentifierExpression>(source, Symbols().Register(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param name the identifier name
|
|
|
|
/// @return an ast::IdentifierExpression with the given name
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::IdentifierExpression* Expr(const char* name) {
|
2021-05-17 15:51:47 +00:00
|
|
|
return create<ast::IdentifierExpression>(Symbols().Register(name));
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param name the identifier name
|
|
|
|
/// @return an ast::IdentifierExpression with the given name
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::IdentifierExpression* Expr(const Source& source,
|
|
|
|
const std::string& name) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return create<ast::IdentifierExpression>(source, Symbols().Register(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param name the identifier name
|
|
|
|
/// @return an ast::IdentifierExpression with the given name
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::IdentifierExpression* Expr(const std::string& name) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return create<ast::IdentifierExpression>(Symbols().Register(name));
|
|
|
|
}
|
|
|
|
|
2021-05-17 15:51:47 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param value the boolean value
|
|
|
|
/// @return a Scalar constructor for the given value
|
2021-11-12 13:53:49 +00:00
|
|
|
const ast::BoolLiteralExpression* Expr(const Source& source, bool value) {
|
2021-11-10 19:23:07 +00:00
|
|
|
return create<ast::BoolLiteralExpression>(source, value);
|
2021-05-17 15:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param value the boolean value
|
|
|
|
/// @return a Scalar constructor for the given value
|
2021-11-10 19:23:07 +00:00
|
|
|
const ast::BoolLiteralExpression* Expr(bool value) {
|
|
|
|
return create<ast::BoolLiteralExpression>(value);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 15:51:47 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param value the float value
|
|
|
|
/// @return a Scalar constructor for the given value
|
2021-11-10 19:23:07 +00:00
|
|
|
const ast::FloatLiteralExpression* Expr(const Source& source, f32 value) {
|
|
|
|
return create<ast::FloatLiteralExpression>(source, value);
|
2021-05-17 15:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param value the float value
|
|
|
|
/// @return a Scalar constructor for the given value
|
2021-11-10 19:23:07 +00:00
|
|
|
const ast::FloatLiteralExpression* Expr(f32 value) {
|
|
|
|
return create<ast::FloatLiteralExpression>(value);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 15:51:47 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param value the integer value
|
|
|
|
/// @return a Scalar constructor for the given value
|
2021-11-12 13:53:49 +00:00
|
|
|
const ast::SintLiteralExpression* Expr(const Source& source, i32 value) {
|
2021-11-10 19:23:07 +00:00
|
|
|
return create<ast::SintLiteralExpression>(source, value);
|
2021-05-17 15:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param value the integer value
|
|
|
|
/// @return a Scalar constructor for the given value
|
2021-11-10 19:23:07 +00:00
|
|
|
const ast::SintLiteralExpression* Expr(i32 value) {
|
|
|
|
return create<ast::SintLiteralExpression>(value);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 15:51:47 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param value the unsigned int value
|
|
|
|
/// @return a Scalar constructor for the given value
|
2021-11-10 19:23:07 +00:00
|
|
|
const ast::UintLiteralExpression* Expr(const Source& source, u32 value) {
|
|
|
|
return create<ast::UintLiteralExpression>(source, value);
|
2021-05-17 15:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param value the unsigned int value
|
|
|
|
/// @return a Scalar constructor for the given value
|
2021-11-10 19:23:07 +00:00
|
|
|
const ast::UintLiteralExpression* Expr(u32 value) {
|
|
|
|
return create<ast::UintLiteralExpression>(value);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
|
|
|
|
/// `list`.
|
|
|
|
/// @param list the list to append too
|
|
|
|
/// @param arg the arg to create
|
|
|
|
template <typename ARG>
|
|
|
|
void Append(ast::ExpressionList& list, ARG&& arg) {
|
|
|
|
list.emplace_back(Expr(std::forward<ARG>(arg)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
|
|
|
|
/// then appends them to `list`.
|
|
|
|
/// @param list the list to append too
|
|
|
|
/// @param arg0 the first argument
|
|
|
|
/// @param args the rest of the arguments
|
|
|
|
template <typename ARG0, typename... ARGS>
|
|
|
|
void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
|
|
|
|
Append(list, std::forward<ARG0>(arg0));
|
|
|
|
Append(list, std::forward<ARGS>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @return an empty list of expressions
|
|
|
|
ast::ExpressionList ExprList() { return {}; }
|
|
|
|
|
|
|
|
/// @param args the list of expressions
|
|
|
|
/// @return the list of expressions converted to `ast::Expression`s using
|
|
|
|
/// `Expr()`,
|
|
|
|
template <typename... ARGS>
|
|
|
|
ast::ExpressionList ExprList(ARGS&&... args) {
|
|
|
|
ast::ExpressionList list;
|
|
|
|
list.reserve(sizeof...(args));
|
|
|
|
Append(list, std::forward<ARGS>(args)...);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param list the list of expressions
|
|
|
|
/// @return `list`
|
|
|
|
ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
|
|
|
|
|
|
|
|
/// @param args the arguments for the type constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of type `ty`, with the values
|
2021-01-26 16:57:10 +00:00
|
|
|
/// of `args` converted to `ast::Expression`s using `Expr()`
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* Construct(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param type the type to construct
|
|
|
|
/// @param args the arguments for the constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of `type` constructed with the
|
2021-01-26 16:57:10 +00:00
|
|
|
/// values `args`.
|
|
|
|
template <typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* Construct(const ast::Type* type, ARGS&&... args) {
|
|
|
|
return Construct(source_, type, std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 19:56:14 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param type the type to construct
|
|
|
|
/// @param args the arguments for the constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of `type` constructed with the
|
2021-06-17 19:56:14 +00:00
|
|
|
/// values `args`.
|
|
|
|
template <typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* Construct(const Source& source,
|
|
|
|
const ast::Type* type,
|
|
|
|
ARGS&&... args) {
|
|
|
|
return create<ast::CallExpression>(source, type,
|
|
|
|
ExprList(std::forward<ARGS>(args)...));
|
2021-06-17 19:56:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:37:45 +00:00
|
|
|
/// @param expr the expression for the bitcast
|
|
|
|
/// @return an `ast::BitcastExpression` of type `ty`, with the values of
|
|
|
|
/// `expr` converted to `ast::Expression`s using `Expr()`
|
|
|
|
template <typename T, typename EXPR>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BitcastExpression* Bitcast(EXPR&& expr) {
|
2021-06-29 12:37:45 +00:00
|
|
|
return Bitcast(ty.Of<T>(), std::forward<EXPR>(expr));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param type the type to cast to
|
|
|
|
/// @param expr the expression for the bitcast
|
|
|
|
/// @return an `ast::BitcastExpression` of `type` constructed with the values
|
|
|
|
/// `expr`.
|
|
|
|
template <typename EXPR>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BitcastExpression* Bitcast(const ast::Type* type, EXPR&& expr) {
|
2021-06-29 12:37:45 +00:00
|
|
|
return create<ast::BitcastExpression>(type, Expr(std::forward<EXPR>(expr)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param source the source information
|
|
|
|
/// @param type the type to cast to
|
|
|
|
/// @param expr the expression for the bitcast
|
|
|
|
/// @return an `ast::BitcastExpression` of `type` constructed with the values
|
|
|
|
/// `expr`.
|
|
|
|
template <typename EXPR>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BitcastExpression* Bitcast(const Source& source,
|
|
|
|
const ast::Type* type,
|
|
|
|
EXPR&& expr) {
|
2021-06-29 12:37:45 +00:00
|
|
|
return create<ast::BitcastExpression>(source, type,
|
|
|
|
Expr(std::forward<EXPR>(expr)));
|
|
|
|
}
|
|
|
|
|
2021-06-18 14:59:51 +00:00
|
|
|
/// @param args the arguments for the vector constructor
|
|
|
|
/// @param type the vector type
|
|
|
|
/// @param size the vector size
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a `size`-element vector of
|
2021-06-18 14:59:51 +00:00
|
|
|
/// type `type`, constructed with the values `args`.
|
|
|
|
template <typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* vec(const ast::Type* type,
|
|
|
|
uint32_t size,
|
|
|
|
ARGS&&... args) {
|
2021-06-18 14:59:51 +00:00
|
|
|
return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param args the arguments for the vector constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a 2-element vector of type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `T`, constructed with the values `args`.
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* vec2(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param args the arguments for the vector constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a 3-element vector of type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `T`, constructed with the values `args`.
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* vec3(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param args the arguments for the vector constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a 4-element vector of type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `T`, constructed with the values `args`.
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* vec4(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param args the arguments for the matrix constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a 2x2 matrix of type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `T`, constructed with the values `args`.
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* mat2x2(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param args the arguments for the matrix constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a 2x3 matrix of type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `T`, constructed with the values `args`.
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* mat2x3(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param args the arguments for the matrix constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a 2x4 matrix of type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `T`, constructed with the values `args`.
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* mat2x4(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param args the arguments for the matrix constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a 3x2 matrix of type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `T`, constructed with the values `args`.
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* mat3x2(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param args the arguments for the matrix constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a 3x3 matrix of type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `T`, constructed with the values `args`.
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* mat3x3(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param args the arguments for the matrix constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a 3x4 matrix of type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `T`, constructed with the values `args`.
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* mat3x4(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param args the arguments for the matrix constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a 4x2 matrix of type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `T`, constructed with the values `args`.
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* mat4x2(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param args the arguments for the matrix constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a 4x3 matrix of type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `T`, constructed with the values `args`.
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* mat4x3(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param args the arguments for the matrix constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of a 4x4 matrix of type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `T`, constructed with the values `args`.
|
|
|
|
template <typename T, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* mat4x4(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param args the arguments for the array constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of an array with element type
|
2021-09-02 13:49:59 +00:00
|
|
|
/// `T` and size `N`, constructed with the values `args`.
|
|
|
|
template <typename T, int N, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* array(ARGS&&... args) {
|
2021-05-04 17:36:31 +00:00
|
|
|
return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param subtype the array element type
|
2021-09-02 13:49:59 +00:00
|
|
|
/// @param n the array size. nullptr represents a runtime-array.
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param args the arguments for the array constructor
|
2021-11-15 20:45:50 +00:00
|
|
|
/// @return an `ast::CallExpression` of an array with element type
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `subtype`, constructed with the values `args`.
|
2021-09-02 13:49:59 +00:00
|
|
|
template <typename EXPR, typename... ARGS>
|
2021-11-15 20:45:50 +00:00
|
|
|
const ast::CallExpression* array(const ast::Type* subtype,
|
|
|
|
EXPR&& n,
|
|
|
|
ARGS&&... args) {
|
2021-09-02 13:49:59 +00:00
|
|
|
return Construct(ty.array(subtype, std::forward<EXPR>(n)),
|
|
|
|
std::forward<ARGS>(args)...);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param name the variable name
|
|
|
|
/// @param type the variable type
|
2021-06-04 19:55:08 +00:00
|
|
|
/// @param optional the optional variable settings.
|
|
|
|
/// Can be any of the following, in any order:
|
|
|
|
/// * ast::StorageClass - specifies the variable storage class
|
2021-06-04 20:41:47 +00:00
|
|
|
/// * ast::Access - specifies the variable's access control
|
2021-06-04 19:55:08 +00:00
|
|
|
/// * ast::Expression* - specifies the variable's initializer expression
|
2022-02-02 23:07:11 +00:00
|
|
|
/// * ast::AttributeList - specifies the variable's attributes
|
2021-06-04 19:55:08 +00:00
|
|
|
/// Note that repeated arguments of the same type will use the last argument's
|
|
|
|
/// value.
|
|
|
|
/// @returns a `ast::Variable` with the given name, type and additional
|
|
|
|
/// options
|
|
|
|
template <typename NAME, typename... OPTIONAL>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Variable* Var(NAME&& name,
|
|
|
|
const ast::Type* type,
|
|
|
|
OPTIONAL&&... optional) {
|
2021-06-04 19:55:08 +00:00
|
|
|
VarOptionals opts(std::forward<OPTIONAL>(optional)...);
|
|
|
|
return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
|
2021-06-04 20:41:47 +00:00
|
|
|
opts.access, type, false, opts.constructor,
|
2022-02-02 23:07:11 +00:00
|
|
|
std::move(opts.attributes));
|
2021-02-10 21:34:26 +00:00
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
/// @param source the variable source
|
|
|
|
/// @param name the variable name
|
|
|
|
/// @param type the variable type
|
2021-06-04 19:55:08 +00:00
|
|
|
/// @param optional the optional variable settings.
|
|
|
|
/// Can be any of the following, in any order:
|
|
|
|
/// * ast::StorageClass - specifies the variable storage class
|
2021-06-04 20:41:47 +00:00
|
|
|
/// * ast::Access - specifies the variable's access control
|
2021-06-04 19:55:08 +00:00
|
|
|
/// * ast::Expression* - specifies the variable's initializer expression
|
2022-02-02 23:07:11 +00:00
|
|
|
/// * ast::AttributeList - specifies the variable's attributes
|
2021-06-04 19:55:08 +00:00
|
|
|
/// Note that repeated arguments of the same type will use the last argument's
|
|
|
|
/// value.
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns a `ast::Variable` with the given name, storage and type
|
2021-06-04 19:55:08 +00:00
|
|
|
template <typename NAME, typename... OPTIONAL>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Variable* Var(const Source& source,
|
|
|
|
NAME&& name,
|
|
|
|
const ast::Type* type,
|
|
|
|
OPTIONAL&&... optional) {
|
2021-06-04 19:55:08 +00:00
|
|
|
VarOptionals opts(std::forward<OPTIONAL>(optional)...);
|
|
|
|
return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
|
2021-06-04 20:41:47 +00:00
|
|
|
opts.storage, opts.access, type, false,
|
2022-02-02 23:07:11 +00:00
|
|
|
opts.constructor, std::move(opts.attributes));
|
2021-02-10 21:34:26 +00:00
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
/// @param name the variable name
|
|
|
|
/// @param type the variable type
|
2021-05-10 20:42:56 +00:00
|
|
|
/// @param constructor constructor expression
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attributes optional variable attributes
|
2021-04-08 22:15:48 +00:00
|
|
|
/// @returns a constant `ast::Variable` with the given name and type
|
2021-04-07 11:16:01 +00:00
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Variable* Const(NAME&& name,
|
|
|
|
const ast::Type* type,
|
|
|
|
const ast::Expression* constructor,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {}) {
|
2021-06-04 20:41:47 +00:00
|
|
|
return create<ast::Variable>(
|
|
|
|
Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::Access::kUndefined, type, true, constructor, attributes);
|
2021-02-10 21:34:26 +00:00
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
/// @param source the variable source
|
|
|
|
/// @param name the variable name
|
|
|
|
/// @param type the variable type
|
2021-05-10 20:42:56 +00:00
|
|
|
/// @param constructor constructor expression
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attributes optional variable attributes
|
2021-04-08 22:15:48 +00:00
|
|
|
/// @returns a constant `ast::Variable` with the given name and type
|
2021-04-07 11:16:01 +00:00
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Variable* Const(const Source& source,
|
|
|
|
NAME&& name,
|
|
|
|
const ast::Type* type,
|
|
|
|
const ast::Expression* constructor,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {}) {
|
2021-06-04 20:41:47 +00:00
|
|
|
return create<ast::Variable>(
|
|
|
|
source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::Access::kUndefined, type, true, constructor, attributes);
|
2021-02-10 21:34:26 +00:00
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-04-08 22:15:48 +00:00
|
|
|
/// @param name the parameter name
|
|
|
|
/// @param type the parameter type
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attributes optional parameter attributes
|
2021-04-08 22:15:48 +00:00
|
|
|
/// @returns a constant `ast::Variable` with the given name and type
|
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Variable* Param(NAME&& name,
|
|
|
|
const ast::Type* type,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {}) {
|
2021-06-04 20:41:47 +00:00
|
|
|
return create<ast::Variable>(
|
|
|
|
Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::Access::kUndefined, type, true, nullptr, attributes);
|
2021-04-08 22:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param source the parameter source
|
|
|
|
/// @param name the parameter name
|
|
|
|
/// @param type the parameter type
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attributes optional parameter attributes
|
2021-04-08 22:15:48 +00:00
|
|
|
/// @returns a constant `ast::Variable` with the given name and type
|
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Variable* Param(const Source& source,
|
|
|
|
NAME&& name,
|
|
|
|
const ast::Type* type,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {}) {
|
2021-06-04 20:41:47 +00:00
|
|
|
return create<ast::Variable>(
|
|
|
|
source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::Access::kUndefined, type, true, nullptr, attributes);
|
2021-04-08 22:15:48 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 17:55:12 +00:00
|
|
|
/// @param name the variable name
|
|
|
|
/// @param type the variable type
|
2021-06-04 19:55:08 +00:00
|
|
|
/// @param optional the optional variable settings.
|
|
|
|
/// Can be any of the following, in any order:
|
|
|
|
/// * ast::StorageClass - specifies the variable storage class
|
2021-06-04 20:41:47 +00:00
|
|
|
/// * ast::Access - specifies the variable's access control
|
2021-06-04 19:55:08 +00:00
|
|
|
/// * ast::Expression* - specifies the variable's initializer expression
|
2022-02-02 23:07:11 +00:00
|
|
|
/// * ast::AttributeList - specifies the variable's attributes
|
2021-06-04 19:55:08 +00:00
|
|
|
/// Note that repeated arguments of the same type will use the last argument's
|
|
|
|
/// value.
|
2021-04-21 17:55:12 +00:00
|
|
|
/// @returns a new `ast::Variable`, which is automatically registered as a
|
|
|
|
/// global variable with the ast::Module.
|
2021-07-15 19:09:25 +00:00
|
|
|
template <typename NAME,
|
|
|
|
typename... OPTIONAL,
|
2021-07-17 18:09:26 +00:00
|
|
|
typename = DisableIfSource<NAME>>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Variable* Global(NAME&& name,
|
|
|
|
const ast::Type* type,
|
|
|
|
OPTIONAL&&... optional) {
|
2021-06-04 19:55:08 +00:00
|
|
|
auto* var = Var(std::forward<NAME>(name), type,
|
|
|
|
std::forward<OPTIONAL>(optional)...);
|
2021-04-21 17:55:12 +00:00
|
|
|
AST().AddGlobalVariable(var);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param source the variable source
|
|
|
|
/// @param name the variable name
|
|
|
|
/// @param type the variable type
|
2021-06-04 19:55:08 +00:00
|
|
|
/// @param optional the optional variable settings.
|
|
|
|
/// Can be any of the following, in any order:
|
|
|
|
/// * ast::StorageClass - specifies the variable storage class
|
2021-06-04 20:41:47 +00:00
|
|
|
/// * ast::Access - specifies the variable's access control
|
2021-06-04 19:55:08 +00:00
|
|
|
/// * ast::Expression* - specifies the variable's initializer expression
|
2022-02-02 23:07:11 +00:00
|
|
|
/// * ast::AttributeList - specifies the variable's attributes
|
2021-06-04 19:55:08 +00:00
|
|
|
/// Note that repeated arguments of the same type will use the last argument's
|
|
|
|
/// value.
|
2021-04-21 17:55:12 +00:00
|
|
|
/// @returns a new `ast::Variable`, which is automatically registered as a
|
|
|
|
/// global variable with the ast::Module.
|
2021-06-04 19:55:08 +00:00
|
|
|
template <typename NAME, typename... OPTIONAL>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Variable* Global(const Source& source,
|
|
|
|
NAME&& name,
|
|
|
|
const ast::Type* type,
|
|
|
|
OPTIONAL&&... optional) {
|
2021-06-04 19:55:08 +00:00
|
|
|
auto* var = Var(source, std::forward<NAME>(name), type,
|
|
|
|
std::forward<OPTIONAL>(optional)...);
|
2021-02-03 17:19:59 +00:00
|
|
|
AST().AddGlobalVariable(var);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2021-05-10 20:42:56 +00:00
|
|
|
/// @param name the variable name
|
|
|
|
/// @param type the variable type
|
|
|
|
/// @param constructor constructor expression
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attributes optional variable attributes
|
2021-02-03 17:19:59 +00:00
|
|
|
/// @returns a const `ast::Variable` constructed by calling Var() with the
|
|
|
|
/// arguments of `args`, which is automatically registered as a global
|
|
|
|
/// variable with the ast::Module.
|
2021-05-10 20:42:56 +00:00
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Variable* GlobalConst(NAME&& name,
|
|
|
|
const ast::Type* type,
|
|
|
|
const ast::Expression* constructor,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {}) {
|
2021-05-10 20:42:56 +00:00
|
|
|
auto* var = Const(std::forward<NAME>(name), type, constructor,
|
2022-02-02 23:07:11 +00:00
|
|
|
std::move(attributes));
|
2021-05-10 20:42:56 +00:00
|
|
|
AST().AddGlobalVariable(var);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param source the variable source
|
|
|
|
/// @param name the variable name
|
|
|
|
/// @param type the variable type
|
|
|
|
/// @param constructor constructor expression
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attributes optional variable attributes
|
2021-05-10 20:42:56 +00:00
|
|
|
/// @returns a const `ast::Variable` constructed by calling Var() with the
|
|
|
|
/// arguments of `args`, which is automatically registered as a global
|
|
|
|
/// variable with the ast::Module.
|
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Variable* GlobalConst(const Source& source,
|
|
|
|
NAME&& name,
|
|
|
|
const ast::Type* type,
|
|
|
|
const ast::Expression* constructor,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {}) {
|
2021-05-10 20:42:56 +00:00
|
|
|
auto* var = Const(source, std::forward<NAME>(name), type, constructor,
|
2022-02-02 23:07:11 +00:00
|
|
|
std::move(attributes));
|
2021-02-03 17:19:59 +00:00
|
|
|
AST().AddGlobalVariable(var);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2021-05-17 15:51:47 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param expr the expression to take the address of
|
|
|
|
/// @return an ast::UnaryOpExpression that takes the address of `expr`
|
|
|
|
template <typename EXPR>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
|
2021-05-17 15:51:47 +00:00
|
|
|
return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
|
|
|
|
Expr(std::forward<EXPR>(expr)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param expr the expression to take the address of
|
|
|
|
/// @return an ast::UnaryOpExpression that takes the address of `expr`
|
|
|
|
template <typename EXPR>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
|
2021-05-17 15:51:47 +00:00
|
|
|
return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
|
|
|
|
Expr(std::forward<EXPR>(expr)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param source the source information
|
|
|
|
/// @param expr the expression to perform an indirection on
|
|
|
|
/// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
|
|
|
|
template <typename EXPR>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
|
2021-05-17 15:51:47 +00:00
|
|
|
return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
|
|
|
|
Expr(std::forward<EXPR>(expr)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param expr the expression to perform an indirection on
|
|
|
|
/// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
|
|
|
|
template <typename EXPR>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::UnaryOpExpression* Deref(EXPR&& expr) {
|
2021-05-17 15:51:47 +00:00
|
|
|
return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
|
|
|
|
Expr(std::forward<EXPR>(expr)));
|
|
|
|
}
|
|
|
|
|
2021-06-09 20:17:59 +00:00
|
|
|
/// @param source the source information
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param func the function name
|
|
|
|
/// @param args the function call arguments
|
|
|
|
/// @returns a `ast::CallExpression` to the function `func`, with the
|
|
|
|
/// arguments of `args` converted to `ast::Expression`s using `Expr()`.
|
|
|
|
template <typename NAME, typename... ARGS>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::CallExpression* Call(const Source& source,
|
|
|
|
NAME&& func,
|
|
|
|
ARGS&&... args) {
|
2021-06-09 20:17:59 +00:00
|
|
|
return create<ast::CallExpression>(source, Expr(func),
|
|
|
|
ExprList(std::forward<ARGS>(args)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param func the function name
|
|
|
|
/// @param args the function call arguments
|
|
|
|
/// @returns a `ast::CallExpression` to the function `func`, with the
|
|
|
|
/// arguments of `args` converted to `ast::Expression`s using `Expr()`.
|
2021-07-17 18:09:26 +00:00
|
|
|
template <typename NAME, typename... ARGS, typename = DisableIfSource<NAME>>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return create<ast::CallExpression>(Expr(func),
|
|
|
|
ExprList(std::forward<ARGS>(args)...));
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:36:04 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param call the call expression to wrap in a call statement
|
|
|
|
/// @returns a `ast::CallStatement` for the given call expression
|
|
|
|
const ast::CallStatement* CallStmt(const Source& source,
|
|
|
|
const ast::CallExpression* call) {
|
|
|
|
return create<ast::CallStatement>(source, call);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param call the call expression to wrap in a call statement
|
|
|
|
/// @returns a `ast::CallStatement` for the given call expression
|
|
|
|
const ast::CallStatement* CallStmt(const ast::CallExpression* call) {
|
|
|
|
return create<ast::CallStatement>(call);
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:33:14 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @returns a `ast::PhonyExpression`
|
|
|
|
const ast::PhonyExpression* Phony(const Source& source) {
|
|
|
|
return create<ast::PhonyExpression>(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns a `ast::PhonyExpression`
|
|
|
|
const ast::PhonyExpression* Phony() { return create<ast::PhonyExpression>(); }
|
|
|
|
|
2021-06-11 13:22:27 +00:00
|
|
|
/// @param expr the expression to ignore
|
2021-10-21 23:28:44 +00:00
|
|
|
/// @returns a `ast::AssignmentStatement` that assigns 'expr' to the phony
|
|
|
|
/// (underscore) variable.
|
2021-06-11 13:22:27 +00:00
|
|
|
template <typename EXPR>
|
2021-10-21 23:28:44 +00:00
|
|
|
const ast::AssignmentStatement* Ignore(EXPR&& expr) {
|
|
|
|
return create<ast::AssignmentStatement>(Phony(), Expr(expr));
|
2021-06-11 13:22:27 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param lhs the left hand argument to the addition operation
|
|
|
|
/// @param rhs the right hand argument to the addition operation
|
|
|
|
/// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
|
|
|
|
template <typename LHS, typename RHS>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
|
|
|
|
Expr(std::forward<LHS>(lhs)),
|
|
|
|
Expr(std::forward<RHS>(rhs)));
|
|
|
|
}
|
|
|
|
|
2021-06-22 20:08:29 +00:00
|
|
|
/// @param lhs the left hand argument to the and operation
|
|
|
|
/// @param rhs the right hand argument to the and operation
|
|
|
|
/// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
|
|
|
|
template <typename LHS, typename RHS>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
|
2021-06-22 20:08:29 +00:00
|
|
|
return create<ast::BinaryExpression>(ast::BinaryOp::kAnd,
|
|
|
|
Expr(std::forward<LHS>(lhs)),
|
|
|
|
Expr(std::forward<RHS>(rhs)));
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:37:45 +00:00
|
|
|
/// @param lhs the left hand argument to the or operation
|
|
|
|
/// @param rhs the right hand argument to the or operation
|
|
|
|
/// @returns a `ast::BinaryExpression` bitwise or-ing `lhs` and `rhs`
|
|
|
|
template <typename LHS, typename RHS>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BinaryExpression* Or(LHS&& lhs, RHS&& rhs) {
|
2021-06-29 12:37:45 +00:00
|
|
|
return create<ast::BinaryExpression>(ast::BinaryOp::kOr,
|
|
|
|
Expr(std::forward<LHS>(lhs)),
|
|
|
|
Expr(std::forward<RHS>(rhs)));
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param lhs the left hand argument to the subtraction operation
|
|
|
|
/// @param rhs the right hand argument to the subtraction operation
|
|
|
|
/// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
|
|
|
|
template <typename LHS, typename RHS>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
|
|
|
|
Expr(std::forward<LHS>(lhs)),
|
|
|
|
Expr(std::forward<RHS>(rhs)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param lhs the left hand argument to the multiplication operation
|
|
|
|
/// @param rhs the right hand argument to the multiplication operation
|
|
|
|
/// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
|
|
|
|
template <typename LHS, typename RHS>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
|
|
|
|
Expr(std::forward<LHS>(lhs)),
|
|
|
|
Expr(std::forward<RHS>(rhs)));
|
|
|
|
}
|
|
|
|
|
2021-04-01 19:58:37 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param lhs the left hand argument to the multiplication operation
|
|
|
|
/// @param rhs the right hand argument to the multiplication operation
|
|
|
|
/// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
|
|
|
|
template <typename LHS, typename RHS>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
|
2021-04-01 19:58:37 +00:00
|
|
|
return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
|
|
|
|
Expr(std::forward<LHS>(lhs)),
|
|
|
|
Expr(std::forward<RHS>(rhs)));
|
|
|
|
}
|
|
|
|
|
2021-04-07 11:16:01 +00:00
|
|
|
/// @param lhs the left hand argument to the division operation
|
|
|
|
/// @param rhs the right hand argument to the division operation
|
|
|
|
/// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
|
|
|
|
template <typename LHS, typename RHS>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
|
2021-04-07 11:16:01 +00:00
|
|
|
return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
|
|
|
|
Expr(std::forward<LHS>(lhs)),
|
2021-06-29 12:37:45 +00:00
|
|
|
Expr(std::forward<RHS>(rhs)));
|
2021-12-22 15:02:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param lhs the left hand argument to the modulo operation
|
|
|
|
/// @param rhs the right hand argument to the modulo operation
|
|
|
|
/// @returns a `ast::BinaryExpression` applying modulo of `lhs` by `rhs`
|
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
const ast::Expression* Mod(LHS&& lhs, RHS&& rhs) {
|
|
|
|
return create<ast::BinaryExpression>(ast::BinaryOp::kModulo,
|
|
|
|
Expr(std::forward<LHS>(lhs)),
|
|
|
|
Expr(std::forward<RHS>(rhs)));
|
2021-06-29 12:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param lhs the left hand argument to the bit shift right operation
|
|
|
|
/// @param rhs the right hand argument to the bit shift right operation
|
|
|
|
/// @returns a `ast::BinaryExpression` bit shifting right `lhs` by `rhs`
|
|
|
|
template <typename LHS, typename RHS>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BinaryExpression* Shr(LHS&& lhs, RHS&& rhs) {
|
2021-06-29 12:37:45 +00:00
|
|
|
return create<ast::BinaryExpression>(ast::BinaryOp::kShiftRight,
|
|
|
|
Expr(std::forward<LHS>(lhs)),
|
|
|
|
Expr(std::forward<RHS>(rhs)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param lhs the left hand argument to the bit shift left operation
|
|
|
|
/// @param rhs the right hand argument to the bit shift left operation
|
|
|
|
/// @returns a `ast::BinaryExpression` bit shifting left `lhs` by `rhs`
|
|
|
|
template <typename LHS, typename RHS>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BinaryExpression* Shl(LHS&& lhs, RHS&& rhs) {
|
2021-06-29 12:37:45 +00:00
|
|
|
return create<ast::BinaryExpression>(ast::BinaryOp::kShiftLeft,
|
|
|
|
Expr(std::forward<LHS>(lhs)),
|
2021-04-07 11:16:01 +00:00
|
|
|
Expr(std::forward<RHS>(rhs)));
|
|
|
|
}
|
|
|
|
|
2021-11-10 14:38:34 +00:00
|
|
|
/// @param lhs the left hand argument to the equal expression
|
|
|
|
/// @param rhs the right hand argument to the equal expression
|
|
|
|
/// @returns a `ast::BinaryExpression` comparing `lhs` equal to `rhs`
|
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
const ast::BinaryExpression* Equal(LHS&& lhs, RHS&& rhs) {
|
|
|
|
return create<ast::BinaryExpression>(ast::BinaryOp::kEqual,
|
|
|
|
Expr(std::forward<LHS>(lhs)),
|
|
|
|
Expr(std::forward<RHS>(rhs)));
|
|
|
|
}
|
|
|
|
|
2021-06-16 09:19:36 +00:00
|
|
|
/// @param source the source information
|
2021-11-10 19:23:07 +00:00
|
|
|
/// @param obj the object for the index accessor expression
|
|
|
|
/// @param idx the index argument for the index accessor expression
|
2021-11-09 09:35:00 +00:00
|
|
|
/// @returns a `ast::IndexAccessorExpression` that indexes `arr` with `idx`
|
2021-11-10 19:23:07 +00:00
|
|
|
template <typename OBJ, typename IDX>
|
2021-11-09 09:35:00 +00:00
|
|
|
const ast::IndexAccessorExpression* IndexAccessor(const Source& source,
|
2021-11-10 19:23:07 +00:00
|
|
|
OBJ&& obj,
|
2021-10-19 18:38:54 +00:00
|
|
|
IDX&& idx) {
|
2021-11-09 09:35:00 +00:00
|
|
|
return create<ast::IndexAccessorExpression>(
|
2021-11-10 19:23:07 +00:00
|
|
|
source, Expr(std::forward<OBJ>(obj)), Expr(std::forward<IDX>(idx)));
|
2021-06-16 09:19:36 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 19:23:07 +00:00
|
|
|
/// @param obj the object for the index accessor expression
|
|
|
|
/// @param idx the index argument for the index accessor expression
|
2021-11-09 09:35:00 +00:00
|
|
|
/// @returns a `ast::IndexAccessorExpression` that indexes `arr` with `idx`
|
2021-11-10 19:23:07 +00:00
|
|
|
template <typename OBJ, typename IDX>
|
|
|
|
const ast::IndexAccessorExpression* IndexAccessor(OBJ&& obj, IDX&& idx) {
|
|
|
|
return create<ast::IndexAccessorExpression>(Expr(std::forward<OBJ>(obj)),
|
2021-01-26 16:57:10 +00:00
|
|
|
Expr(std::forward<IDX>(idx)));
|
|
|
|
}
|
|
|
|
|
2021-10-21 23:04:44 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param obj the object for the member accessor expression
|
2021-11-10 19:23:07 +00:00
|
|
|
/// @param idx the index argument for the member accessor expression
|
2021-10-21 23:04:44 +00:00
|
|
|
/// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
|
|
|
|
template <typename OBJ, typename IDX>
|
|
|
|
const ast::MemberAccessorExpression* MemberAccessor(const Source& source,
|
|
|
|
OBJ&& obj,
|
|
|
|
IDX&& idx) {
|
|
|
|
return create<ast::MemberAccessorExpression>(
|
|
|
|
source, Expr(std::forward<OBJ>(obj)), Expr(std::forward<IDX>(idx)));
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param obj the object for the member accessor expression
|
2021-11-10 19:23:07 +00:00
|
|
|
/// @param idx the index argument for the member accessor expression
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
|
|
|
|
template <typename OBJ, typename IDX>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
|
|
|
|
Expr(std::forward<IDX>(idx)));
|
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates a ast::StructMemberOffsetAttribute
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param val the offset value
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the offset attribute pointer
|
|
|
|
const ast::StructMemberOffsetAttribute* MemberOffset(uint32_t val) {
|
|
|
|
return create<ast::StructMemberOffsetAttribute>(source_, val);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates a ast::StructMemberSizeAttribute
|
2021-03-15 10:43:11 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param val the size value
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the size attribute pointer
|
|
|
|
const ast::StructMemberSizeAttribute* MemberSize(const Source& source,
|
|
|
|
uint32_t val) {
|
|
|
|
return create<ast::StructMemberSizeAttribute>(source, val);
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates a ast::StructMemberSizeAttribute
|
2021-03-15 10:43:11 +00:00
|
|
|
/// @param val the size value
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the size attribute pointer
|
|
|
|
const ast::StructMemberSizeAttribute* MemberSize(uint32_t val) {
|
|
|
|
return create<ast::StructMemberSizeAttribute>(source_, val);
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates a ast::StructMemberAlignAttribute
|
2021-03-15 10:43:11 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param val the align value
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the align attribute pointer
|
|
|
|
const ast::StructMemberAlignAttribute* MemberAlign(const Source& source,
|
|
|
|
uint32_t val) {
|
|
|
|
return create<ast::StructMemberAlignAttribute>(source, val);
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates a ast::StructMemberAlignAttribute
|
2021-03-15 10:43:11 +00:00
|
|
|
/// @param val the align value
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the align attribute pointer
|
|
|
|
const ast::StructMemberAlignAttribute* MemberAlign(uint32_t val) {
|
|
|
|
return create<ast::StructMemberAlignAttribute>(source_, val);
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates a ast::StructBlockAttribute
|
|
|
|
/// @returns the struct block attribute pointer
|
|
|
|
const ast::StructBlockAttribute* StructBlock() {
|
|
|
|
return create<ast::StructBlockAttribute>();
|
2021-07-15 19:09:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates the ast::GroupAttribute
|
|
|
|
/// @param value group attribute index
|
|
|
|
/// @returns the group attribute pointer
|
|
|
|
const ast::GroupAttribute* Group(uint32_t value) {
|
|
|
|
return create<ast::GroupAttribute>(value);
|
2021-07-15 19:09:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates the ast::BindingAttribute
|
2021-07-15 19:09:25 +00:00
|
|
|
/// @param value the binding index
|
|
|
|
/// @returns the binding deocration pointer
|
2022-02-02 23:07:11 +00:00
|
|
|
const ast::BindingAttribute* Binding(uint32_t value) {
|
|
|
|
return create<ast::BindingAttribute>(value);
|
2021-07-15 19:09:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Convenience function to create both a ast::GroupAttribute and
|
|
|
|
/// ast::BindingAttribute
|
2021-07-15 19:09:25 +00:00
|
|
|
/// @param group the group index
|
|
|
|
/// @param binding the binding index
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns a attribute list with both the group and binding attributes
|
|
|
|
ast::AttributeList GroupAndBinding(uint32_t group, uint32_t binding) {
|
2021-07-15 19:09:25 +00:00
|
|
|
return {Group(group), Binding(binding)};
|
|
|
|
}
|
|
|
|
|
2021-02-02 14:29:15 +00:00
|
|
|
/// Creates an ast::Function and registers it with the ast::Module.
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param name the function name
|
|
|
|
/// @param params the function parameters
|
|
|
|
/// @param type the function return type
|
|
|
|
/// @param body the function body
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attributes the optional function attributes
|
|
|
|
/// @param return_type_attributes the optional function return type
|
|
|
|
/// attributes
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns the function pointer
|
2021-04-07 11:16:01 +00:00
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Function* Func(const Source& source,
|
|
|
|
NAME&& name,
|
|
|
|
ast::VariableList params,
|
|
|
|
const ast::Type* type,
|
|
|
|
ast::StatementList body,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {},
|
|
|
|
ast::AttributeList return_type_attributes = {}) {
|
|
|
|
auto* func = create<ast::Function>(
|
|
|
|
source, Sym(std::forward<NAME>(name)), params, type,
|
|
|
|
create<ast::BlockStatement>(body), attributes, return_type_attributes);
|
2021-02-09 21:26:21 +00:00
|
|
|
AST().AddFunction(func);
|
2021-02-02 14:29:15 +00:00
|
|
|
return func;
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 14:29:15 +00:00
|
|
|
/// Creates an ast::Function and registers it with the ast::Module.
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param name the function name
|
|
|
|
/// @param params the function parameters
|
|
|
|
/// @param type the function return type
|
|
|
|
/// @param body the function body
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attributes the optional function attributes
|
|
|
|
/// @param return_type_attributes the optional function return type
|
|
|
|
/// attributes
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns the function pointer
|
2021-04-07 11:16:01 +00:00
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Function* Func(NAME&& name,
|
|
|
|
ast::VariableList params,
|
|
|
|
const ast::Type* type,
|
|
|
|
ast::StatementList body,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {},
|
|
|
|
ast::AttributeList return_type_attributes = {}) {
|
2021-04-07 11:16:01 +00:00
|
|
|
auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
|
|
|
|
type, create<ast::BlockStatement>(body),
|
2022-02-02 23:07:11 +00:00
|
|
|
attributes, return_type_attributes);
|
2021-02-09 21:26:21 +00:00
|
|
|
AST().AddFunction(func);
|
2021-02-02 14:29:15 +00:00
|
|
|
return func;
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 15:12:27 +00:00
|
|
|
/// Creates an ast::BreakStatement
|
|
|
|
/// @param source the source information
|
|
|
|
/// @returns the break statement pointer
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BreakStatement* Break(const Source& source) {
|
2021-07-27 15:12:27 +00:00
|
|
|
return create<ast::BreakStatement>(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an ast::BreakStatement
|
|
|
|
/// @returns the break statement pointer
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BreakStatement* Break() { return create<ast::BreakStatement>(); }
|
2021-07-27 15:12:27 +00:00
|
|
|
|
2021-12-03 15:49:34 +00:00
|
|
|
/// Creates an ast::ContinueStatement
|
|
|
|
/// @param source the source information
|
|
|
|
/// @returns the continue statement pointer
|
|
|
|
const ast::ContinueStatement* Continue(const Source& source) {
|
|
|
|
return create<ast::ContinueStatement>(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an ast::ContinueStatement
|
|
|
|
/// @returns the continue statement pointer
|
|
|
|
const ast::ContinueStatement* Continue() {
|
|
|
|
return create<ast::ContinueStatement>();
|
|
|
|
}
|
|
|
|
|
2021-04-22 13:50:53 +00:00
|
|
|
/// Creates an ast::ReturnStatement with no return value
|
|
|
|
/// @param source the source information
|
|
|
|
/// @returns the return statement pointer
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::ReturnStatement* Return(const Source& source) {
|
2021-04-22 13:50:53 +00:00
|
|
|
return create<ast::ReturnStatement>(source);
|
|
|
|
}
|
|
|
|
|
2021-04-17 05:32:01 +00:00
|
|
|
/// Creates an ast::ReturnStatement with no return value
|
2021-03-19 14:04:51 +00:00
|
|
|
/// @returns the return statement pointer
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::ReturnStatement* Return() {
|
|
|
|
return create<ast::ReturnStatement>();
|
|
|
|
}
|
2021-04-17 05:32:01 +00:00
|
|
|
|
2021-04-22 13:50:53 +00:00
|
|
|
/// Creates an ast::ReturnStatement with the given return value
|
|
|
|
/// @param source the source information
|
|
|
|
/// @param val the return value
|
|
|
|
/// @returns the return statement pointer
|
|
|
|
template <typename EXPR>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
|
2021-04-22 13:50:53 +00:00
|
|
|
return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
|
|
|
|
}
|
|
|
|
|
2021-04-17 05:32:01 +00:00
|
|
|
/// Creates an ast::ReturnStatement with the given return value
|
|
|
|
/// @param val the return value
|
|
|
|
/// @returns the return statement pointer
|
2021-07-17 18:09:26 +00:00
|
|
|
template <typename EXPR, typename = DisableIfSource<EXPR>>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::ReturnStatement* Return(EXPR&& val) {
|
2021-04-17 05:32:01 +00:00
|
|
|
return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
|
2021-03-19 14:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 11:44:57 +00:00
|
|
|
/// Creates an ast::DiscardStatement
|
|
|
|
/// @param source the source information
|
|
|
|
/// @returns the discard statement pointer
|
|
|
|
const ast::DiscardStatement* Discard(const Source& source) {
|
|
|
|
return create<ast::DiscardStatement>(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an ast::DiscardStatement
|
|
|
|
/// @returns the discard statement pointer
|
|
|
|
const ast::DiscardStatement* Discard() {
|
|
|
|
return create<ast::DiscardStatement>();
|
|
|
|
}
|
|
|
|
|
2021-06-09 14:32:14 +00:00
|
|
|
/// Creates a ast::Alias registering it with the AST().TypeDecls().
|
2021-06-09 14:32:14 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param name the alias name
|
|
|
|
/// @param type the alias target type
|
|
|
|
/// @returns the alias type
|
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Alias* Alias(const Source& source,
|
|
|
|
NAME&& name,
|
|
|
|
const ast::Type* type) {
|
2021-06-09 14:32:14 +00:00
|
|
|
auto* out = ty.alias(source, std::forward<NAME>(name), type);
|
2021-06-09 14:32:14 +00:00
|
|
|
AST().AddTypeDecl(out);
|
2021-06-09 14:32:14 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-06-09 14:32:14 +00:00
|
|
|
/// Creates a ast::Alias registering it with the AST().TypeDecls().
|
2021-06-09 14:32:14 +00:00
|
|
|
/// @param name the alias name
|
|
|
|
/// @param type the alias target type
|
|
|
|
/// @returns the alias type
|
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Alias* Alias(NAME&& name, const ast::Type* type) {
|
2021-06-09 14:32:14 +00:00
|
|
|
auto* out = ty.alias(std::forward<NAME>(name), type);
|
2021-06-09 14:32:14 +00:00
|
|
|
AST().AddTypeDecl(out);
|
2021-06-09 14:32:14 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-06-09 14:32:14 +00:00
|
|
|
/// Creates a ast::Struct registering it with the AST().TypeDecls().
|
2021-03-15 10:43:11 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param name the struct name
|
|
|
|
/// @param members the struct members
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attributes the optional struct attributes
|
2021-03-15 10:43:11 +00:00
|
|
|
/// @returns the struct type
|
2021-04-09 16:19:48 +00:00
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Struct* Structure(const Source& source,
|
|
|
|
NAME&& name,
|
|
|
|
ast::StructMemberList members,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {}) {
|
2021-04-20 15:04:21 +00:00
|
|
|
auto sym = Sym(std::forward<NAME>(name));
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* type = create<ast::Struct>(source, sym, std::move(members),
|
2022-02-02 23:07:11 +00:00
|
|
|
std::move(attributes));
|
2021-06-09 14:32:14 +00:00
|
|
|
AST().AddTypeDecl(type);
|
2021-03-15 10:43:11 +00:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2021-06-09 14:32:14 +00:00
|
|
|
/// Creates a ast::Struct registering it with the AST().TypeDecls().
|
2021-03-15 10:43:11 +00:00
|
|
|
/// @param name the struct name
|
|
|
|
/// @param members the struct members
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attributes the optional struct attributes
|
2021-03-15 10:43:11 +00:00
|
|
|
/// @returns the struct type
|
2021-04-09 16:19:48 +00:00
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Struct* Structure(NAME&& name,
|
|
|
|
ast::StructMemberList members,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {}) {
|
2021-04-20 15:04:21 +00:00
|
|
|
auto sym = Sym(std::forward<NAME>(name));
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* type =
|
2022-02-02 23:07:11 +00:00
|
|
|
create<ast::Struct>(sym, std::move(members), std::move(attributes));
|
2021-06-09 14:32:14 +00:00
|
|
|
AST().AddTypeDecl(type);
|
2021-03-15 10:43:11 +00:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2021-02-02 14:29:15 +00:00
|
|
|
/// Creates a ast::StructMember
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param name the struct member name
|
|
|
|
/// @param type the struct member type
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attributes the optional struct member attributes
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns the struct member pointer
|
2021-04-07 11:16:01 +00:00
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::StructMember* Member(const Source& source,
|
|
|
|
NAME&& name,
|
|
|
|
const ast::Type* type,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {}) {
|
2021-04-07 11:16:01 +00:00
|
|
|
return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
|
2022-02-02 23:07:11 +00:00
|
|
|
type, std::move(attributes));
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 14:29:15 +00:00
|
|
|
/// Creates a ast::StructMember
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param name the struct member name
|
|
|
|
/// @param type the struct member type
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param attributes the optional struct member attributes
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns the struct member pointer
|
2021-04-07 11:16:01 +00:00
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::StructMember* Member(NAME&& name,
|
|
|
|
const ast::Type* type,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList attributes = {}) {
|
2021-04-07 11:16:01 +00:00
|
|
|
return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
|
2022-02-02 23:07:11 +00:00
|
|
|
type, std::move(attributes));
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 21:16:21 +00:00
|
|
|
/// Creates a ast::StructMember with the given byte offset
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @param offset the offset to use in the StructMemberOffsetattribute
|
2021-02-08 21:16:21 +00:00
|
|
|
/// @param name the struct member name
|
|
|
|
/// @param type the struct member type
|
|
|
|
/// @returns the struct member pointer
|
2021-04-07 11:16:01 +00:00
|
|
|
template <typename NAME>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::StructMember* Member(uint32_t offset,
|
|
|
|
NAME&& name,
|
|
|
|
const ast::Type* type) {
|
2021-02-08 21:16:21 +00:00
|
|
|
return create<ast::StructMember>(
|
2021-04-07 11:16:01 +00:00
|
|
|
source_, Sym(std::forward<NAME>(name)), type,
|
2022-02-02 23:07:11 +00:00
|
|
|
ast::AttributeList{
|
|
|
|
create<ast::StructMemberOffsetAttribute>(offset),
|
2021-02-08 21:16:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:26:57 +00:00
|
|
|
/// Creates a ast::BlockStatement with input statements
|
2021-07-17 18:09:26 +00:00
|
|
|
/// @param source the source information for the block
|
2021-03-09 10:26:57 +00:00
|
|
|
/// @param statements statements of block
|
|
|
|
/// @returns the block statement pointer
|
|
|
|
template <typename... Statements>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BlockStatement* Block(const Source& source,
|
|
|
|
Statements&&... statements) {
|
2021-07-17 18:09:26 +00:00
|
|
|
return create<ast::BlockStatement>(
|
|
|
|
source, ast::StatementList{std::forward<Statements>(statements)...});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a ast::BlockStatement with input statements
|
|
|
|
/// @param statements statements of block
|
|
|
|
/// @returns the block statement pointer
|
|
|
|
template <typename... STATEMENTS, typename = DisableIfSource<STATEMENTS...>>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BlockStatement* Block(STATEMENTS&&... statements) {
|
2021-03-09 10:26:57 +00:00
|
|
|
return create<ast::BlockStatement>(
|
2021-07-17 18:09:26 +00:00
|
|
|
ast::StatementList{std::forward<STATEMENTS>(statements)...});
|
2021-03-09 10:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a ast::ElseStatement with input condition and body
|
|
|
|
/// @param condition the else condition expression
|
|
|
|
/// @param body the else body
|
|
|
|
/// @returns the else statement pointer
|
2021-04-19 16:52:42 +00:00
|
|
|
template <typename CONDITION>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::ElseStatement* Else(CONDITION&& condition,
|
|
|
|
const ast::BlockStatement* body) {
|
2021-04-19 16:52:42 +00:00
|
|
|
return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
|
|
|
|
body);
|
2021-03-09 10:26:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 15:49:34 +00:00
|
|
|
/// Creates a ast::ElseStatement with no condition and body
|
|
|
|
/// @param body the else body
|
|
|
|
/// @returns the else statement pointer
|
|
|
|
const ast::ElseStatement* Else(const ast::BlockStatement* body) {
|
|
|
|
return create<ast::ElseStatement>(nullptr, body);
|
|
|
|
}
|
|
|
|
|
2022-02-03 00:12:52 +00:00
|
|
|
/// Creates a ast::IfStatement with input condition, body, and optional
|
|
|
|
/// variadic else statements
|
|
|
|
/// @param source the source information for the if statement
|
|
|
|
/// @param condition the if statement condition expression
|
|
|
|
/// @param body the if statement body
|
|
|
|
/// @param elseStatements optional variadic else statements
|
|
|
|
/// @returns the if statement pointer
|
|
|
|
template <typename CONDITION, typename... ELSE_STATEMENTS>
|
|
|
|
const ast::IfStatement* If(const Source& source,
|
|
|
|
CONDITION&& condition,
|
|
|
|
const ast::BlockStatement* body,
|
|
|
|
ELSE_STATEMENTS&&... elseStatements) {
|
|
|
|
return create<ast::IfStatement>(
|
|
|
|
source, Expr(std::forward<CONDITION>(condition)), body,
|
|
|
|
ast::ElseStatementList{
|
|
|
|
std::forward<ELSE_STATEMENTS>(elseStatements)...});
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:26:57 +00:00
|
|
|
/// Creates a ast::IfStatement with input condition, body, and optional
|
|
|
|
/// variadic else statements
|
|
|
|
/// @param condition the if statement condition expression
|
|
|
|
/// @param body the if statement body
|
|
|
|
/// @param elseStatements optional variadic else statements
|
|
|
|
/// @returns the if statement pointer
|
2021-04-19 16:52:42 +00:00
|
|
|
template <typename CONDITION, typename... ELSE_STATEMENTS>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::IfStatement* If(CONDITION&& condition,
|
|
|
|
const ast::BlockStatement* body,
|
|
|
|
ELSE_STATEMENTS&&... elseStatements) {
|
2021-03-09 10:26:57 +00:00
|
|
|
return create<ast::IfStatement>(
|
2021-04-19 16:52:42 +00:00
|
|
|
Expr(std::forward<CONDITION>(condition)), body,
|
2021-03-09 10:26:57 +00:00
|
|
|
ast::ElseStatementList{
|
2021-04-19 16:52:42 +00:00
|
|
|
std::forward<ELSE_STATEMENTS>(elseStatements)...});
|
2021-03-09 10:26:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 13:50:53 +00:00
|
|
|
/// Creates a ast::AssignmentStatement with input lhs and rhs expressions
|
|
|
|
/// @param source the source information
|
|
|
|
/// @param lhs the left hand side expression initializer
|
|
|
|
/// @param rhs the right hand side expression initializer
|
|
|
|
/// @returns the assignment statement pointer
|
|
|
|
template <typename LhsExpressionInit, typename RhsExpressionInit>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::AssignmentStatement* Assign(const Source& source,
|
|
|
|
LhsExpressionInit&& lhs,
|
|
|
|
RhsExpressionInit&& rhs) {
|
2021-04-22 13:50:53 +00:00
|
|
|
return create<ast::AssignmentStatement>(
|
|
|
|
source, Expr(std::forward<LhsExpressionInit>(lhs)),
|
|
|
|
Expr(std::forward<RhsExpressionInit>(rhs)));
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:26:57 +00:00
|
|
|
/// Creates a ast::AssignmentStatement with input lhs and rhs expressions
|
2021-03-25 12:55:27 +00:00
|
|
|
/// @param lhs the left hand side expression initializer
|
|
|
|
/// @param rhs the right hand side expression initializer
|
2021-03-09 10:26:57 +00:00
|
|
|
/// @returns the assignment statement pointer
|
2021-03-25 12:55:27 +00:00
|
|
|
template <typename LhsExpressionInit, typename RhsExpressionInit>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
|
|
|
|
RhsExpressionInit&& rhs) {
|
2021-03-25 12:55:27 +00:00
|
|
|
return create<ast::AssignmentStatement>(
|
|
|
|
Expr(std::forward<LhsExpressionInit>(lhs)),
|
|
|
|
Expr(std::forward<RhsExpressionInit>(rhs)));
|
2021-03-09 10:26:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 17:51:48 +00:00
|
|
|
/// Creates a ast::LoopStatement with input body and optional continuing
|
|
|
|
/// @param source the source information
|
|
|
|
/// @param body the loop body
|
|
|
|
/// @param continuing the optional continuing block
|
|
|
|
/// @returns the loop statement pointer
|
|
|
|
const ast::LoopStatement* Loop(
|
|
|
|
const Source& source,
|
|
|
|
const ast::BlockStatement* body,
|
|
|
|
const ast::BlockStatement* continuing = nullptr) {
|
|
|
|
return create<ast::LoopStatement>(source, body, continuing);
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:26:57 +00:00
|
|
|
/// Creates a ast::LoopStatement with input body and optional continuing
|
|
|
|
/// @param body the loop body
|
|
|
|
/// @param continuing the optional continuing block
|
|
|
|
/// @returns the loop statement pointer
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::LoopStatement* Loop(
|
|
|
|
const ast::BlockStatement* body,
|
|
|
|
const ast::BlockStatement* continuing = nullptr) {
|
2021-03-09 10:26:57 +00:00
|
|
|
return create<ast::LoopStatement>(body, continuing);
|
|
|
|
}
|
|
|
|
|
2021-07-02 19:27:42 +00:00
|
|
|
/// Creates a ast::ForLoopStatement with input body and optional initializer,
|
|
|
|
/// condition and continuing.
|
|
|
|
/// @param source the source information
|
|
|
|
/// @param init the optional loop initializer
|
|
|
|
/// @param cond the optional loop condition
|
|
|
|
/// @param cont the optional loop continuing
|
|
|
|
/// @param body the loop body
|
|
|
|
/// @returns the for loop statement pointer
|
|
|
|
template <typename COND>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::ForLoopStatement* For(const Source& source,
|
|
|
|
const ast::Statement* init,
|
|
|
|
COND&& cond,
|
|
|
|
const ast::Statement* cont,
|
|
|
|
const ast::BlockStatement* body) {
|
2021-07-02 19:27:42 +00:00
|
|
|
return create<ast::ForLoopStatement>(
|
|
|
|
source, init, Expr(std::forward<COND>(cond)), cont, body);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a ast::ForLoopStatement with input body and optional initializer,
|
|
|
|
/// condition and continuing.
|
|
|
|
/// @param init the optional loop initializer
|
|
|
|
/// @param cond the optional loop condition
|
|
|
|
/// @param cont the optional loop continuing
|
|
|
|
/// @param body the loop body
|
|
|
|
/// @returns the for loop statement pointer
|
|
|
|
template <typename COND>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::ForLoopStatement* For(const ast::Statement* init,
|
|
|
|
COND&& cond,
|
|
|
|
const ast::Statement* cont,
|
|
|
|
const ast::BlockStatement* body) {
|
2021-07-02 19:27:42 +00:00
|
|
|
return create<ast::ForLoopStatement>(init, Expr(std::forward<COND>(cond)),
|
|
|
|
cont, body);
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:26:57 +00:00
|
|
|
/// Creates a ast::VariableDeclStatement for the input variable
|
2021-04-22 13:50:53 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param var the variable to wrap in a decl statement
|
|
|
|
/// @returns the variable decl statement pointer
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::VariableDeclStatement* Decl(const Source& source,
|
|
|
|
const ast::Variable* var) {
|
2021-04-22 13:50:53 +00:00
|
|
|
return create<ast::VariableDeclStatement>(source, var);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a ast::VariableDeclStatement for the input variable
|
2021-03-09 10:26:57 +00:00
|
|
|
/// @param var the variable to wrap in a decl statement
|
|
|
|
/// @returns the variable decl statement pointer
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::VariableDeclStatement* Decl(const ast::Variable* var) {
|
2021-03-09 10:26:57 +00:00
|
|
|
return create<ast::VariableDeclStatement>(var);
|
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
/// Creates a ast::SwitchStatement with input expression and cases
|
2021-10-14 20:30:29 +00:00
|
|
|
/// @param source the source information
|
2021-03-25 12:55:27 +00:00
|
|
|
/// @param condition the condition expression initializer
|
|
|
|
/// @param cases case statements
|
|
|
|
/// @returns the switch statement pointer
|
|
|
|
template <typename ExpressionInit, typename... Cases>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::SwitchStatement* Switch(const Source& source,
|
|
|
|
ExpressionInit&& condition,
|
|
|
|
Cases&&... cases) {
|
2021-10-14 20:30:29 +00:00
|
|
|
return create<ast::SwitchStatement>(
|
|
|
|
source, Expr(std::forward<ExpressionInit>(condition)),
|
|
|
|
ast::CaseStatementList{std::forward<Cases>(cases)...});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a ast::SwitchStatement with input expression and cases
|
|
|
|
/// @param condition the condition expression initializer
|
|
|
|
/// @param cases case statements
|
|
|
|
/// @returns the switch statement pointer
|
|
|
|
template <typename ExpressionInit,
|
|
|
|
typename... Cases,
|
|
|
|
typename = DisableIfSource<ExpressionInit>>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::SwitchStatement* Switch(ExpressionInit&& condition,
|
|
|
|
Cases&&... cases) {
|
2021-03-25 12:55:27 +00:00
|
|
|
return create<ast::SwitchStatement>(
|
|
|
|
Expr(std::forward<ExpressionInit>(condition)),
|
|
|
|
ast::CaseStatementList{std::forward<Cases>(cases)...});
|
|
|
|
}
|
|
|
|
|
2021-10-14 20:30:29 +00:00
|
|
|
/// Creates a ast::CaseStatement with input list of selectors, and body
|
|
|
|
/// @param source the source information
|
|
|
|
/// @param selectors list of selectors
|
|
|
|
/// @param body the case body
|
|
|
|
/// @returns the case statement pointer
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::CaseStatement* Case(const Source& source,
|
|
|
|
ast::CaseSelectorList selectors,
|
|
|
|
const ast::BlockStatement* body = nullptr) {
|
2021-10-14 20:30:29 +00:00
|
|
|
return create<ast::CaseStatement>(source, std::move(selectors),
|
|
|
|
body ? body : Block());
|
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
/// Creates a ast::CaseStatement with input list of selectors, and body
|
|
|
|
/// @param selectors list of selectors
|
|
|
|
/// @param body the case body
|
|
|
|
/// @returns the case statement pointer
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::CaseStatement* Case(ast::CaseSelectorList selectors,
|
|
|
|
const ast::BlockStatement* body = nullptr) {
|
2021-03-25 12:55:27 +00:00
|
|
|
return create<ast::CaseStatement>(std::move(selectors),
|
|
|
|
body ? body : Block());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenient overload that takes a single selector
|
|
|
|
/// @param selector a single case selector
|
|
|
|
/// @param body the case body
|
|
|
|
/// @returns the case statement pointer
|
2021-11-10 19:23:07 +00:00
|
|
|
const ast::CaseStatement* Case(const ast::IntLiteralExpression* selector,
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::BlockStatement* body = nullptr) {
|
2021-03-25 12:55:27 +00:00
|
|
|
return Case(ast::CaseSelectorList{selector}, body);
|
|
|
|
}
|
|
|
|
|
2021-10-14 20:30:29 +00:00
|
|
|
/// Convenience function that creates a 'default' ast::CaseStatement
|
|
|
|
/// @param source the source information
|
|
|
|
/// @param body the case body
|
|
|
|
/// @returns the case statement pointer
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::CaseStatement* DefaultCase(
|
|
|
|
const Source& source,
|
|
|
|
const ast::BlockStatement* body = nullptr) {
|
2021-10-14 20:30:29 +00:00
|
|
|
return Case(source, ast::CaseSelectorList{}, body);
|
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
/// Convenience function that creates a 'default' ast::CaseStatement
|
|
|
|
/// @param body the case body
|
|
|
|
/// @returns the case statement pointer
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::CaseStatement* DefaultCase(
|
|
|
|
const ast::BlockStatement* body = nullptr) {
|
2021-03-25 12:55:27 +00:00
|
|
|
return Case(ast::CaseSelectorList{}, body);
|
|
|
|
}
|
|
|
|
|
2021-11-22 11:44:57 +00:00
|
|
|
/// Creates an ast::FallthroughStatement
|
|
|
|
/// @param source the source information
|
|
|
|
/// @returns the fallthrough statement pointer
|
|
|
|
const ast::FallthroughStatement* Fallthrough(const Source& source) {
|
|
|
|
return create<ast::FallthroughStatement>(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an ast::FallthroughStatement
|
|
|
|
/// @returns the fallthrough statement pointer
|
|
|
|
const ast::FallthroughStatement* Fallthrough() {
|
|
|
|
return create<ast::FallthroughStatement>();
|
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::BuiltinAttribute
|
2021-04-06 15:51:47 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param builtin the builtin value
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the builtin attribute pointer
|
|
|
|
const ast::BuiltinAttribute* Builtin(const Source& source,
|
|
|
|
ast::Builtin builtin) {
|
|
|
|
return create<ast::BuiltinAttribute>(source, builtin);
|
2021-04-06 15:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::BuiltinAttribute
|
2021-04-06 15:51:47 +00:00
|
|
|
/// @param builtin the builtin value
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the builtin attribute pointer
|
|
|
|
const ast::BuiltinAttribute* Builtin(ast::Builtin builtin) {
|
|
|
|
return create<ast::BuiltinAttribute>(source_, builtin);
|
2021-04-06 15:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::InterpolateAttribute
|
2021-06-28 23:04:43 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param type the interpolation type
|
|
|
|
/// @param sampling the interpolation sampling
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the interpolate attribute pointer
|
|
|
|
const ast::InterpolateAttribute* Interpolate(
|
2021-10-19 18:38:54 +00:00
|
|
|
const Source& source,
|
|
|
|
ast::InterpolationType type,
|
2022-01-25 01:01:39 +00:00
|
|
|
ast::InterpolationSampling sampling = ast::InterpolationSampling::kNone) {
|
2022-02-02 23:07:11 +00:00
|
|
|
return create<ast::InterpolateAttribute>(source, type, sampling);
|
2021-06-28 23:04:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::InterpolateAttribute
|
2021-06-28 23:04:43 +00:00
|
|
|
/// @param type the interpolation type
|
|
|
|
/// @param sampling the interpolation sampling
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the interpolate attribute pointer
|
|
|
|
const ast::InterpolateAttribute* Interpolate(
|
2021-10-19 18:38:54 +00:00
|
|
|
ast::InterpolationType type,
|
2022-01-25 01:01:39 +00:00
|
|
|
ast::InterpolationSampling sampling = ast::InterpolationSampling::kNone) {
|
2022-02-02 23:07:11 +00:00
|
|
|
return create<ast::InterpolateAttribute>(source_, type, sampling);
|
2021-06-28 23:04:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::InterpolateAttribute using flat interpolation
|
2022-01-25 01:01:39 +00:00
|
|
|
/// @param source the source information
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the interpolate attribute pointer
|
|
|
|
const ast::InterpolateAttribute* Flat(const Source& source) {
|
2022-01-25 01:01:39 +00:00
|
|
|
return Interpolate(source, ast::InterpolationType::kFlat);
|
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::InterpolateAttribute using flat interpolation
|
|
|
|
/// @returns the interpolate attribute pointer
|
|
|
|
const ast::InterpolateAttribute* Flat() {
|
2022-01-25 01:01:39 +00:00
|
|
|
return Interpolate(ast::InterpolationType::kFlat);
|
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::InvariantAttribute
|
2021-07-12 12:12:32 +00:00
|
|
|
/// @param source the source information
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the invariant attribute pointer
|
|
|
|
const ast::InvariantAttribute* Invariant(const Source& source) {
|
|
|
|
return create<ast::InvariantAttribute>(source);
|
2021-07-12 12:12:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::InvariantAttribute
|
|
|
|
/// @returns the invariant attribute pointer
|
|
|
|
const ast::InvariantAttribute* Invariant() {
|
|
|
|
return create<ast::InvariantAttribute>(source_);
|
2021-07-12 12:12:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::LocationAttribute
|
2021-04-06 15:51:47 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param location the location value
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the location attribute pointer
|
|
|
|
const ast::LocationAttribute* Location(const Source& source,
|
|
|
|
uint32_t location) {
|
|
|
|
return create<ast::LocationAttribute>(source, location);
|
2021-04-06 15:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::LocationAttribute
|
2021-04-06 15:51:47 +00:00
|
|
|
/// @param location the location value
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the location attribute pointer
|
|
|
|
const ast::LocationAttribute* Location(uint32_t location) {
|
|
|
|
return create<ast::LocationAttribute>(source_, location);
|
2021-04-06 15:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::OverrideAttribute with a specific constant ID
|
2021-05-13 20:32:32 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param id the id value
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the override attribute pointer
|
|
|
|
const ast::OverrideAttribute* Override(const Source& source, uint32_t id) {
|
|
|
|
return create<ast::OverrideAttribute>(source, id);
|
2021-05-13 20:32:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::OverrideAttribute with a specific constant ID
|
2021-05-13 20:32:32 +00:00
|
|
|
/// @param id the optional id value
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the override attribute pointer
|
|
|
|
const ast::OverrideAttribute* Override(uint32_t id) {
|
2021-05-13 20:32:32 +00:00
|
|
|
return Override(source_, id);
|
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::OverrideAttribute without a constant ID
|
2021-05-13 20:32:32 +00:00
|
|
|
/// @param source the source information
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the override attribute pointer
|
|
|
|
const ast::OverrideAttribute* Override(const Source& source) {
|
|
|
|
return create<ast::OverrideAttribute>(source);
|
2021-05-13 20:32:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::OverrideAttribute without a constant ID
|
|
|
|
/// @returns the override attribute pointer
|
|
|
|
const ast::OverrideAttribute* Override() { return Override(source_); }
|
2021-05-13 20:32:32 +00:00
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::StageAttribute
|
2021-04-06 15:51:47 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param stage the pipeline stage
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the stage attribute pointer
|
|
|
|
const ast::StageAttribute* Stage(const Source& source,
|
|
|
|
ast::PipelineStage stage) {
|
|
|
|
return create<ast::StageAttribute>(source, stage);
|
2021-04-06 15:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::StageAttribute
|
2021-04-06 15:51:47 +00:00
|
|
|
/// @param stage the pipeline stage
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the stage attribute pointer
|
|
|
|
const ast::StageAttribute* Stage(ast::PipelineStage stage) {
|
|
|
|
return create<ast::StageAttribute>(source_, stage);
|
2021-04-06 15:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::WorkgroupAttribute
|
2021-05-19 13:40:08 +00:00
|
|
|
/// @param x the x dimension expression
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the workgroup attribute pointer
|
2021-05-19 13:40:08 +00:00
|
|
|
template <typename EXPR_X>
|
2022-02-02 23:07:11 +00:00
|
|
|
const ast::WorkgroupAttribute* WorkgroupSize(EXPR_X&& x) {
|
2021-05-19 13:40:08 +00:00
|
|
|
return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::WorkgroupAttribute
|
2021-05-19 13:40:08 +00:00
|
|
|
/// @param x the x dimension expression
|
|
|
|
/// @param y the y dimension expression
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the workgroup attribute pointer
|
2021-05-19 13:40:08 +00:00
|
|
|
template <typename EXPR_X, typename EXPR_Y>
|
2022-02-02 23:07:11 +00:00
|
|
|
const ast::WorkgroupAttribute* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
|
2021-05-19 13:40:08 +00:00
|
|
|
return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::WorkgroupAttribute
|
2021-06-09 18:53:57 +00:00
|
|
|
/// @param source the source information
|
|
|
|
/// @param x the x dimension expression
|
|
|
|
/// @param y the y dimension expression
|
|
|
|
/// @param z the z dimension expression
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the workgroup attribute pointer
|
2021-06-09 18:53:57 +00:00
|
|
|
template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
|
2022-02-02 23:07:11 +00:00
|
|
|
const ast::WorkgroupAttribute* WorkgroupSize(const Source& source,
|
|
|
|
EXPR_X&& x,
|
|
|
|
EXPR_Y&& y,
|
|
|
|
EXPR_Z&& z) {
|
|
|
|
return create<ast::WorkgroupAttribute>(
|
2021-06-09 18:53:57 +00:00
|
|
|
source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
|
|
|
|
Expr(std::forward<EXPR_Z>(z)));
|
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::WorkgroupAttribute
|
2021-05-19 13:40:08 +00:00
|
|
|
/// @param x the x dimension expression
|
|
|
|
/// @param y the y dimension expression
|
|
|
|
/// @param z the z dimension expression
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the workgroup attribute pointer
|
2021-05-19 13:40:08 +00:00
|
|
|
template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
|
2022-02-02 23:07:11 +00:00
|
|
|
const ast::WorkgroupAttribute* WorkgroupSize(EXPR_X&& x,
|
|
|
|
EXPR_Y&& y,
|
|
|
|
EXPR_Z&& z) {
|
|
|
|
return create<ast::WorkgroupAttribute>(
|
2021-05-19 13:40:08 +00:00
|
|
|
source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
|
|
|
|
Expr(std::forward<EXPR_Z>(z)));
|
|
|
|
}
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
/// Creates an ast::DisableValidationAttribute
|
2021-10-28 15:00:39 +00:00
|
|
|
/// @param validation the validation to disable
|
2022-02-02 23:07:11 +00:00
|
|
|
/// @returns the disable validation attribute pointer
|
|
|
|
const ast::DisableValidationAttribute* Disable(
|
2021-10-28 15:00:39 +00:00
|
|
|
ast::DisabledValidation validation) {
|
2022-02-02 23:07:11 +00:00
|
|
|
return ASTNodes().Create<ast::DisableValidationAttribute>(ID(), validation);
|
2021-10-28 15:00:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Sets the current builder source to `src`
|
|
|
|
/// @param src the Source used for future create() calls
|
|
|
|
void SetSource(const Source& src) {
|
|
|
|
AssertNotMoved();
|
|
|
|
source_ = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the current builder source to `loc`
|
|
|
|
/// @param loc the Source used for future create() calls
|
|
|
|
void SetSource(const Source::Location& loc) {
|
|
|
|
AssertNotMoved();
|
|
|
|
source_ = Source(loc);
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
/// Helper for returning the resolved semantic type of the expression `expr`.
|
2021-03-09 10:54:37 +00:00
|
|
|
/// @note As the Resolver is run when the Program is built, this will only be
|
|
|
|
/// useful for the Resolver itself and tests that use their own Resolver.
|
2021-01-29 16:43:41 +00:00
|
|
|
/// @param expr the AST expression
|
|
|
|
/// @return the resolved semantic type for the expression, or nullptr if the
|
|
|
|
/// expression has no resolved type.
|
2021-10-19 18:38:54 +00:00
|
|
|
const sem::Type* TypeOf(const ast::Expression* expr) const;
|
2021-04-30 20:20:19 +00:00
|
|
|
|
2021-05-17 15:51:47 +00:00
|
|
|
/// Helper for returning the resolved semantic type of the variable `var`.
|
|
|
|
/// @note As the Resolver is run when the Program is built, this will only be
|
|
|
|
/// useful for the Resolver itself and tests that use their own Resolver.
|
|
|
|
/// @param var the AST variable
|
|
|
|
/// @return the resolved semantic type for the variable, or nullptr if the
|
|
|
|
/// variable has no resolved type.
|
2021-10-19 18:38:54 +00:00
|
|
|
const sem::Type* TypeOf(const ast::Variable* var) const;
|
2021-05-17 15:51:47 +00:00
|
|
|
|
2021-04-30 20:20:19 +00:00
|
|
|
/// Helper for returning the resolved semantic type of the AST type `type`.
|
|
|
|
/// @note As the Resolver is run when the Program is built, this will only be
|
|
|
|
/// useful for the Resolver itself and tests that use their own Resolver.
|
2021-06-09 14:32:14 +00:00
|
|
|
/// @param type the AST type
|
2021-04-30 20:20:19 +00:00
|
|
|
/// @return the resolved semantic type for the type, or nullptr if the type
|
|
|
|
/// has no resolved type.
|
2021-06-09 14:32:14 +00:00
|
|
|
const sem::Type* TypeOf(const ast::Type* type) const;
|
|
|
|
|
|
|
|
/// Helper for returning the resolved semantic type of the AST type
|
|
|
|
/// declaration `type_decl`.
|
|
|
|
/// @note As the Resolver is run when the Program is built, this will only be
|
|
|
|
/// useful for the Resolver itself and tests that use their own Resolver.
|
|
|
|
/// @param type_decl the AST type declaration
|
|
|
|
/// @return the resolved semantic type for the type declaration, or nullptr if
|
|
|
|
/// the type declaration has no resolved type.
|
|
|
|
const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
|
2021-01-29 16:43:41 +00:00
|
|
|
|
2021-02-03 17:19:59 +00:00
|
|
|
/// Wraps the ast::Expression in a statement. This is used by tests that
|
2021-03-09 10:54:37 +00:00
|
|
|
/// construct a partial AST and require the Resolver to reach these
|
2021-02-03 17:19:59 +00:00
|
|
|
/// nodes.
|
|
|
|
/// @param expr the ast::Expression to be wrapped by an ast::Statement
|
|
|
|
/// @return the ast::Statement that wraps the ast::Expression
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Statement* WrapInStatement(const ast::Expression* expr);
|
2021-02-03 17:19:59 +00:00
|
|
|
/// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
|
2021-03-09 10:54:37 +00:00
|
|
|
/// tests that construct a partial AST and require the Resolver to reach
|
2021-02-03 17:19:59 +00:00
|
|
|
/// these nodes.
|
|
|
|
/// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
|
|
|
|
/// @return the ast::VariableDeclStatement that wraps the ast::Variable
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::VariableDeclStatement* WrapInStatement(const ast::Variable* v);
|
2021-02-03 17:19:59 +00:00
|
|
|
/// Returns the statement argument. Used as a passthrough-overload by
|
|
|
|
/// WrapInFunction().
|
|
|
|
/// @param stmt the ast::Statement
|
|
|
|
/// @return `stmt`
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Statement* WrapInStatement(const ast::Statement* stmt);
|
2021-02-03 17:19:59 +00:00
|
|
|
/// Wraps the list of arguments in a simple function so that each is reachable
|
2021-03-09 10:54:37 +00:00
|
|
|
/// by the Resolver.
|
2021-02-03 17:19:59 +00:00
|
|
|
/// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
|
2021-03-19 14:04:51 +00:00
|
|
|
/// @returns the function
|
2021-02-03 17:19:59 +00:00
|
|
|
template <typename... ARGS>
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Function* WrapInFunction(ARGS&&... args) {
|
2021-02-03 17:19:59 +00:00
|
|
|
ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
|
2021-03-19 14:04:51 +00:00
|
|
|
return WrapInFunction(std::move(stmts));
|
2021-02-03 17:19:59 +00:00
|
|
|
}
|
|
|
|
/// @param stmts a list of ast::Statement that will be wrapped by a function,
|
2021-03-09 10:54:37 +00:00
|
|
|
/// so that each statement is reachable by the Resolver.
|
2021-03-19 14:04:51 +00:00
|
|
|
/// @returns the function
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Function* WrapInFunction(ast::StatementList stmts);
|
2021-02-03 17:19:59 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// The builder types
|
2021-02-17 16:23:52 +00:00
|
|
|
TypesBuilder const ty{this};
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Asserts that the builder has not been moved.
|
|
|
|
void AssertNotMoved() const;
|
|
|
|
|
|
|
|
private:
|
2021-04-13 23:27:27 +00:00
|
|
|
ProgramID id_;
|
2021-04-19 22:51:23 +00:00
|
|
|
sem::Manager types_;
|
2021-01-29 15:17:30 +00:00
|
|
|
ASTNodeAllocator ast_nodes_;
|
|
|
|
SemNodeAllocator sem_nodes_;
|
2021-01-26 16:57:10 +00:00
|
|
|
ast::Module* ast_;
|
2021-04-16 19:07:51 +00:00
|
|
|
sem::Info sem_;
|
2021-04-15 18:20:03 +00:00
|
|
|
SymbolTable symbols_{id_};
|
2021-01-27 18:49:05 +00:00
|
|
|
diag::List diagnostics_;
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
/// The source to use when creating AST nodes without providing a Source as
|
|
|
|
/// the first argument.
|
|
|
|
Source source_;
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
/// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
|
2021-01-27 19:23:55 +00:00
|
|
|
/// program when built.
|
|
|
|
bool resolve_on_build_ = true;
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
|
|
|
|
bool moved_ = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
//! @cond Doxygen_Suppress
|
|
|
|
// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
|
|
|
|
template <>
|
|
|
|
struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
|
2021-10-19 18:38:54 +00:00
|
|
|
static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return t->i32();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
|
2021-10-19 18:38:54 +00:00
|
|
|
static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return t->u32();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
|
2021-10-19 18:38:54 +00:00
|
|
|
static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return t->f32();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct ProgramBuilder::TypesBuilder::CToAST<bool> {
|
2021-10-19 18:38:54 +00:00
|
|
|
static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return t->bool_();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct ProgramBuilder::TypesBuilder::CToAST<void> {
|
2021-10-19 18:38:54 +00:00
|
|
|
static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return t->void_();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
//! @endcond
|
|
|
|
|
2021-04-19 16:50:23 +00:00
|
|
|
/// @param builder the ProgramBuilder
|
|
|
|
/// @returns the ProgramID of the ProgramBuilder
|
|
|
|
inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
|
|
|
|
return builder->ID();
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // SRC_PROGRAM_BUILDER_H_
|