2020-03-02 20:47:43 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef SRC_AST_MODULE_H_
|
|
|
|
#define SRC_AST_MODULE_H_
|
|
|
|
|
2020-12-14 20:31:17 +00:00
|
|
|
#include <functional>
|
2020-03-02 20:47:43 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2020-11-23 19:58:55 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_map>
|
2020-03-02 20:47:43 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "src/ast/function.h"
|
2020-12-14 21:46:37 +00:00
|
|
|
#include "src/ast/traits.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
#include "src/ast/variable.h"
|
2021-01-21 15:30:10 +00:00
|
|
|
#include "src/block_allocator.h"
|
2020-12-10 16:56:02 +00:00
|
|
|
#include "src/symbol_table.h"
|
2021-01-21 15:42:10 +00:00
|
|
|
#include "src/type/alias_type.h"
|
|
|
|
#include "src/type/type_manager.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace ast {
|
|
|
|
|
|
|
|
/// Represents all the source in a given program.
|
|
|
|
class Module {
|
|
|
|
public:
|
2020-12-03 18:21:49 +00:00
|
|
|
/// Constructor
|
2020-04-09 18:52:06 +00:00
|
|
|
Module();
|
2020-12-03 18:21:49 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
/// Move constructor
|
2020-04-09 18:52:06 +00:00
|
|
|
Module(Module&&);
|
2020-12-03 18:21:49 +00:00
|
|
|
|
|
|
|
/// Move assignment operator
|
2020-12-02 15:31:08 +00:00
|
|
|
/// @param rhs the Module to move
|
2020-12-03 18:21:49 +00:00
|
|
|
/// @return this Module
|
2020-12-02 04:27:08 +00:00
|
|
|
Module& operator=(Module&& rhs);
|
2020-12-03 18:21:49 +00:00
|
|
|
|
|
|
|
/// Destructor
|
2020-04-09 18:52:06 +00:00
|
|
|
~Module();
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
/// @return a deep copy of this module
|
2020-12-15 12:32:18 +00:00
|
|
|
Module Clone() const;
|
2020-12-01 18:04:17 +00:00
|
|
|
|
2020-12-15 12:32:18 +00:00
|
|
|
/// Clone this module into `ctx->mod` using the provided CloneContext
|
|
|
|
/// The module will be cloned in this order:
|
|
|
|
/// * Constructed types
|
|
|
|
/// * Global variables
|
|
|
|
/// * Functions
|
|
|
|
/// @param ctx the clone context
|
|
|
|
void Clone(CloneContext* ctx) const;
|
2020-12-04 09:06:09 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
/// Add a global variable to the module
|
|
|
|
/// @param var the variable to add
|
2020-11-16 16:31:07 +00:00
|
|
|
void AddGlobalVariable(Variable* var) { global_variables_.push_back(var); }
|
2020-03-02 20:47:43 +00:00
|
|
|
/// @returns the global variables for the module
|
2020-04-06 19:37:37 +00:00
|
|
|
const VariableList& global_variables() const { return global_variables_; }
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-07-30 13:59:00 +00:00
|
|
|
/// @returns the global variables for the module
|
|
|
|
VariableList& global_variables() { return global_variables_; }
|
|
|
|
|
2020-10-19 15:31:47 +00:00
|
|
|
/// Adds a constructed type to the module.
|
|
|
|
/// The type must be an alias or a struct.
|
|
|
|
/// @param type the constructed type to add
|
|
|
|
void AddConstructedType(type::Type* type) {
|
|
|
|
constructed_types_.push_back(type);
|
|
|
|
}
|
|
|
|
/// @returns the constructed types in the module
|
|
|
|
const std::vector<type::Type*>& constructed_types() const {
|
|
|
|
return constructed_types_;
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a function to the module
|
|
|
|
/// @param func the function
|
2020-11-16 16:31:07 +00:00
|
|
|
void AddFunction(Function* func) { functions_.push_back(func); }
|
2020-03-02 20:47:43 +00:00
|
|
|
/// @returns the modules functions
|
2020-04-06 19:37:37 +00:00
|
|
|
const FunctionList& functions() const { return functions_; }
|
2020-07-14 20:37:20 +00:00
|
|
|
/// Returns the function with the given name
|
2020-12-11 18:24:53 +00:00
|
|
|
/// @param sym the function symbol to search for
|
2020-07-14 20:37:20 +00:00
|
|
|
/// @returns the associated function or nullptr if none exists
|
2020-12-11 18:24:53 +00:00
|
|
|
Function* FindFunctionBySymbol(Symbol sym) const;
|
2020-09-22 14:53:03 +00:00
|
|
|
/// Returns the function with the given name
|
2020-12-11 18:24:53 +00:00
|
|
|
/// @param sym the function symbol to search for
|
2020-09-22 14:53:03 +00:00
|
|
|
/// @param stage the pipeline stage
|
|
|
|
/// @returns the associated function or nullptr if none exists
|
2020-12-11 18:24:53 +00:00
|
|
|
Function* FindFunctionBySymbolAndStage(Symbol sym, PipelineStage stage) const;
|
2020-12-03 14:54:09 +00:00
|
|
|
/// @param stage the pipeline stage
|
|
|
|
/// @returns true if the module contains an entrypoint function with the given
|
|
|
|
/// stage
|
|
|
|
bool HasStage(PipelineStage stage) const;
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
/// @returns true if all required fields in the AST are present.
|
|
|
|
bool IsValid() const;
|
|
|
|
|
|
|
|
/// @returns a string representation of the module
|
|
|
|
std::string to_str() const;
|
|
|
|
|
2020-12-14 21:46:37 +00:00
|
|
|
/// Creates a new Node owned by the Module. When the Module is
|
|
|
|
/// destructed, the Node will also be destructed.
|
2020-11-18 20:58:20 +00:00
|
|
|
/// @param args the arguments to pass to the type constructor
|
|
|
|
/// @returns the node pointer
|
|
|
|
template <typename T, typename... ARGS>
|
2020-12-14 21:46:37 +00:00
|
|
|
traits::EnableIfIsType<T, Node>* create(ARGS&&... args) {
|
2021-01-21 15:30:10 +00:00
|
|
|
return ast_nodes_.Create<T>(std::forward<ARGS>(args)...);
|
2020-11-18 20:58:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 21:46:37 +00:00
|
|
|
/// Creates a new type::Type owned by the Module.
|
2020-11-23 19:58:55 +00:00
|
|
|
/// When the Module is destructed, owned Module and the returned
|
2020-11-30 23:30:58 +00:00
|
|
|
/// `Type` will also be destructed.
|
2020-11-24 15:11:06 +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.
|
2020-11-23 19:58:55 +00:00
|
|
|
/// @param args the arguments to pass to the type constructor
|
|
|
|
/// @returns the de-aliased type pointer
|
|
|
|
template <typename T, typename... ARGS>
|
2020-12-14 21:46:37 +00:00
|
|
|
traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) {
|
2020-11-30 23:30:58 +00:00
|
|
|
static_assert(std::is_base_of<type::Type, T>::value,
|
|
|
|
"T does not derive from type::Type");
|
2020-11-23 19:58:55 +00:00
|
|
|
return type_mgr_.Get<T>(std::forward<ARGS>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Moves the type `ty` to the Module, returning a pointer to the unique
|
|
|
|
/// (de-aliased) type.
|
2020-11-30 23:30:58 +00:00
|
|
|
/// When the Module is destructed, the returned `Type` will also be
|
2020-11-23 19:58:55 +00:00
|
|
|
/// destructed.
|
2020-11-24 15:11:06 +00:00
|
|
|
/// @see create()
|
2020-11-23 19:58:55 +00:00
|
|
|
/// @param ty the type to add to the module
|
|
|
|
/// @returns the de-aliased type pointer
|
|
|
|
template <typename T>
|
2020-12-14 21:46:37 +00:00
|
|
|
traits::EnableIfIsType<T, type::Type>* unique_type(std::unique_ptr<T> ty) {
|
2020-11-23 19:58:55 +00:00
|
|
|
return static_cast<T*>(type_mgr_.Get(std::move(ty)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns all the declared types in the module
|
|
|
|
/// @returns the mapping from name string to type.
|
2020-11-30 23:30:58 +00:00
|
|
|
const std::unordered_map<std::string, std::unique_ptr<type::Type>>& types() {
|
2020-11-23 19:58:55 +00:00
|
|
|
return type_mgr_.types();
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
/// @returns all the declared nodes in the module
|
2021-01-21 15:30:10 +00:00
|
|
|
BlockAllocator<Node>::View nodes() { return ast_nodes_.Objects(); }
|
2020-12-01 18:04:17 +00:00
|
|
|
|
2020-12-10 16:56:02 +00:00
|
|
|
/// Registers `name` as a symbol
|
|
|
|
/// @param name the name to register
|
|
|
|
/// @returns the symbol for the `name`. If `name` is already registered the
|
|
|
|
/// previously generated symbol will be returned.
|
|
|
|
Symbol RegisterSymbol(const std::string& name);
|
|
|
|
|
2020-12-11 18:24:53 +00:00
|
|
|
/// Returns the symbol for `name`
|
|
|
|
/// @param name the name to lookup
|
|
|
|
/// @returns the symbol for name or symbol::kInvalid
|
|
|
|
Symbol GetSymbol(const std::string& name) const;
|
|
|
|
|
2020-12-10 18:40:01 +00:00
|
|
|
/// Returns the `name` for `sym`
|
|
|
|
/// @param sym the symbol to retrieve the name for
|
|
|
|
/// @returns the use provided `name` for the symbol or "" if not found
|
|
|
|
std::string SymbolToName(const Symbol sym) const;
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
private:
|
|
|
|
Module(const Module&) = delete;
|
|
|
|
|
2020-12-10 16:56:02 +00:00
|
|
|
SymbolTable symbol_table_;
|
2020-04-06 19:37:37 +00:00
|
|
|
VariableList global_variables_;
|
2020-10-19 15:31:47 +00:00
|
|
|
// The constructed types are owned by the type manager
|
|
|
|
std::vector<type::Type*> constructed_types_;
|
2020-04-06 19:37:37 +00:00
|
|
|
FunctionList functions_;
|
2021-01-21 15:30:10 +00:00
|
|
|
BlockAllocator<Node> ast_nodes_;
|
2021-01-21 15:42:10 +00:00
|
|
|
type::Manager type_mgr_;
|
2020-03-02 20:47:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ast
|
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // SRC_AST_MODULE_H_
|