2021-01-25 18:14:08 +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_H_
|
|
|
|
#define SRC_PROGRAM_H_
|
|
|
|
|
|
|
|
#include <string>
|
2021-06-25 10:26:26 +00:00
|
|
|
#include <unordered_set>
|
2021-01-25 18:14:08 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/ast/function.h"
|
2021-04-13 23:27:27 +00:00
|
|
|
#include "src/program_id.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/info.h"
|
2021-04-19 22:54:43 +00:00
|
|
|
#include "src/sem/type_manager.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/symbol_table.h"
|
2021-01-25 18:14:08 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
// Forward declarations
|
|
|
|
class CloneContext;
|
|
|
|
|
|
|
|
namespace ast {
|
|
|
|
|
|
|
|
class Module;
|
|
|
|
|
|
|
|
} // namespace ast
|
|
|
|
|
|
|
|
/// Program holds the AST, Type information and SymbolTable for a tint program.
|
2021-01-25 18:14:08 +00:00
|
|
|
class Program {
|
|
|
|
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
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Constructor
|
|
|
|
Program();
|
2021-01-25 18:14:08 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Move constructor
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param rhs the Program to move
|
|
|
|
Program(Program&& rhs);
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Move constructor from builder
|
|
|
|
/// @param builder the builder used to construct the program
|
|
|
|
explicit Program(ProgramBuilder&& builder);
|
|
|
|
|
|
|
|
/// Destructor
|
|
|
|
~Program();
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Move assignment operator
|
|
|
|
/// @param rhs the Program to move
|
|
|
|
/// @return this Program
|
|
|
|
Program& operator=(Program&& rhs);
|
|
|
|
|
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
|
|
|
const sem::Manager& Types() const {
|
2021-01-26 16:57:10 +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
|
|
|
const ASTNodeAllocator& ASTNodes() const {
|
|
|
|
AssertNotMoved();
|
|
|
|
return ast_nodes_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns a reference to the program's semantic nodes storage
|
|
|
|
const SemNodeAllocator& SemNodes() const {
|
2021-01-26 16:57:10 +00:00
|
|
|
AssertNotMoved();
|
2021-01-29 15:17:30 +00:00
|
|
|
return sem_nodes_;
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
/// @returns a reference to the program's AST root Module
|
2021-01-26 16:57:10 +00:00
|
|
|
const ast::Module& AST() const {
|
|
|
|
AssertNotMoved();
|
|
|
|
return *ast_;
|
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-01-29 10:55:40 +00:00
|
|
|
/// @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
|
2021-01-26 16:57:10 +00:00
|
|
|
const SymbolTable& Symbols() const {
|
|
|
|
AssertNotMoved();
|
|
|
|
return symbols_;
|
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-01-27 18:49:05 +00:00
|
|
|
/// @returns a reference to the program's diagnostics
|
|
|
|
const diag::List& Diagnostics() const {
|
|
|
|
AssertNotMoved();
|
|
|
|
return diagnostics_;
|
|
|
|
}
|
|
|
|
|
2021-02-17 01:01:03 +00:00
|
|
|
/// Performs a deep clone of this program.
|
|
|
|
/// The returned Program will contain no pointers to objects owned by this
|
|
|
|
/// Program, and so after calling, this Program can be safely destructed.
|
|
|
|
/// @return a new Program copied from this Program
|
2021-01-26 16:57:10 +00:00
|
|
|
Program Clone() const;
|
|
|
|
|
2021-02-17 01:01:03 +00:00
|
|
|
/// Performs a deep clone of this Program's AST nodes, types and symbols into
|
|
|
|
/// a new ProgramBuilder. Semantic nodes are not cloned, as these will be
|
|
|
|
/// rebuilt when the ProgramBuilder builds its Program.
|
|
|
|
/// The returned ProgramBuilder will contain no pointers to objects owned by
|
|
|
|
/// this Program, and so after calling, this Program can be safely destructed.
|
|
|
|
/// @return a new ProgramBuilder copied from this Program
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder CloneAsBuilder() const;
|
2021-01-26 16:57:10 +00:00
|
|
|
|
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-06-25 10:26:26 +00:00
|
|
|
/// @return the TypeInfo pointers of all transforms that have been applied to
|
|
|
|
/// this program.
|
|
|
|
std::unordered_set<const TypeInfo*> TransformsApplied() const {
|
|
|
|
return transforms_applied_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @param transform the TypeInfo of the transform
|
|
|
|
/// @returns true if the transform with the given TypeInfo was applied to the
|
|
|
|
/// Program
|
|
|
|
bool HasTransformApplied(const TypeInfo* transform) const {
|
|
|
|
return transforms_applied_.count(transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns true if the transform of type `T` was applied.
|
|
|
|
template <typename T>
|
|
|
|
bool HasTransformApplied() const {
|
|
|
|
return HasTransformApplied(&TypeInfo::Of<T>());
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
/// Helper for returning the resolved semantic type of the expression `expr`.
|
|
|
|
/// @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-01-29 16:43:41 +00:00
|
|
|
|
2021-04-30 20:20:19 +00:00
|
|
|
/// Helper for returning the resolved semantic type of the AST type `type`.
|
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`.
|
|
|
|
/// @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-04-30 20:20:19 +00:00
|
|
|
|
2021-07-15 20:24:38 +00:00
|
|
|
/// A function that can be used to print a program
|
|
|
|
using Printer = std::string (*)(const Program*);
|
|
|
|
|
|
|
|
/// The Program printer used for testing and debugging.
|
|
|
|
static Printer printer;
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
private:
|
|
|
|
Program(const Program&) = delete;
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Asserts that the program has not been moved.
|
|
|
|
void AssertNotMoved() const;
|
|
|
|
|
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-02-19 19:04:18 +00:00
|
|
|
ast::Module* ast_ = nullptr;
|
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-06-25 10:26:26 +00:00
|
|
|
std::unordered_set<const TypeInfo*> transforms_applied_;
|
2021-02-19 19:04:18 +00:00
|
|
|
bool is_valid_ = false; // Not valid until it is built
|
2021-01-26 16:57:10 +00:00
|
|
|
bool moved_ = false;
|
2021-01-25 18:14:08 +00:00
|
|
|
};
|
|
|
|
|
2021-04-19 16:50:23 +00:00
|
|
|
/// @param program the Program
|
|
|
|
/// @returns the ProgramID of the Program
|
|
|
|
inline ProgramID ProgramIDOf(const Program* program) {
|
|
|
|
return program->ID();
|
|
|
|
}
|
|
|
|
|
2021-01-25 18:14:08 +00:00
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // SRC_PROGRAM_H_
|