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.
|
|
|
|
|
|
|
|
#include "src/program.h"
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
#include <utility>
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-01-29 11:22:40 +00:00
|
|
|
#include "src/demangler.h"
|
2021-03-09 10:54:37 +00:00
|
|
|
#include "src/resolver/resolver.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/expression.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
namespace tint {
|
2021-07-15 20:24:38 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::string DefaultPrinter(const Program*) {
|
|
|
|
return "<no program printer assigned>";
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Program::Printer Program::printer = DefaultPrinter;
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
Program::Program() = default;
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
Program::Program(Program&& program)
|
2021-04-13 23:27:27 +00:00
|
|
|
: id_(std::move(program.id_)),
|
|
|
|
types_(std::move(program.types_)),
|
2021-01-29 15:17:30 +00:00
|
|
|
ast_nodes_(std::move(program.ast_nodes_)),
|
|
|
|
sem_nodes_(std::move(program.sem_nodes_)),
|
2021-01-26 16:57:10 +00:00
|
|
|
ast_(std::move(program.ast_)),
|
2021-01-29 10:55:40 +00:00
|
|
|
sem_(std::move(program.sem_)),
|
2021-01-27 18:49:05 +00:00
|
|
|
symbols_(std::move(program.symbols_)),
|
|
|
|
diagnostics_(std::move(program.diagnostics_)),
|
2021-06-25 10:26:26 +00:00
|
|
|
transforms_applied_(std::move(program.transforms_applied_)),
|
2021-01-27 18:49:05 +00:00
|
|
|
is_valid_(program.is_valid_) {
|
2021-01-26 16:57:10 +00:00
|
|
|
program.AssertNotMoved();
|
|
|
|
program.moved_ = true;
|
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-01-27 18:49:05 +00:00
|
|
|
Program::Program(ProgramBuilder&& builder) {
|
2021-04-13 23:27:27 +00:00
|
|
|
id_ = builder.ID();
|
|
|
|
|
2021-01-27 19:23:55 +00:00
|
|
|
is_valid_ = builder.IsValid();
|
|
|
|
if (builder.ResolveOnBuild() && builder.IsValid()) {
|
2021-03-09 15:08:48 +00:00
|
|
|
resolver::Resolver resolver(&builder);
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!resolver.Resolve()) {
|
2021-02-02 15:06:05 +00:00
|
|
|
is_valid_ = false;
|
2021-01-27 19:23:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The above must be called *before* the calls to std::move() below
|
2021-01-27 18:49:05 +00:00
|
|
|
types_ = std::move(builder.Types());
|
2021-01-29 15:17:30 +00:00
|
|
|
ast_nodes_ = std::move(builder.ASTNodes());
|
|
|
|
sem_nodes_ = std::move(builder.SemNodes());
|
2021-04-13 23:27:27 +00:00
|
|
|
ast_ = &builder.AST(); // ast::Module is actually a heap allocation.
|
2021-01-29 10:55:40 +00:00
|
|
|
sem_ = std::move(builder.Sem());
|
2021-01-27 18:49:05 +00:00
|
|
|
symbols_ = std::move(builder.Symbols());
|
2021-02-02 15:06:05 +00:00
|
|
|
diagnostics_.add(std::move(builder.Diagnostics()));
|
2021-06-25 10:26:26 +00:00
|
|
|
transforms_applied_ = builder.TransformsApplied();
|
2021-01-26 16:57:10 +00:00
|
|
|
builder.MarkAsMoved();
|
2021-01-27 18:49:05 +00:00
|
|
|
|
|
|
|
if (!is_valid_ && !diagnostics_.contains_errors()) {
|
|
|
|
// If the builder claims to be invalid, then we really should have an error
|
|
|
|
// message generated. If we find a situation where the program is not valid
|
|
|
|
// and there are no errors reported, add one here.
|
2021-06-24 11:27:36 +00:00
|
|
|
diagnostics_.add_error(diag::System::Program, "invalid program generated");
|
2021-01-27 18:49:05 +00:00
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
Program::~Program() = default;
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
Program& Program::operator=(Program&& program) {
|
|
|
|
program.AssertNotMoved();
|
|
|
|
program.moved_ = true;
|
2021-04-13 23:27:27 +00:00
|
|
|
id_ = std::move(program.id_);
|
2021-01-26 16:57:10 +00:00
|
|
|
types_ = std::move(program.types_);
|
2021-01-29 15:17:30 +00:00
|
|
|
ast_nodes_ = std::move(program.ast_nodes_);
|
|
|
|
sem_nodes_ = std::move(program.sem_nodes_);
|
2021-01-26 16:57:10 +00:00
|
|
|
ast_ = std::move(program.ast_);
|
2021-01-29 10:55:40 +00:00
|
|
|
sem_ = std::move(program.sem_);
|
2021-01-26 16:57:10 +00:00
|
|
|
symbols_ = std::move(program.symbols_);
|
2021-02-24 13:51:13 +00:00
|
|
|
diagnostics_ = std::move(program.diagnostics_);
|
2021-06-25 10:26:26 +00:00
|
|
|
transforms_applied_ = std::move(program.transforms_applied_);
|
2021-01-27 18:49:05 +00:00
|
|
|
is_valid_ = program.is_valid_;
|
2021-01-26 16:57:10 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
Program Program::Clone() const {
|
2021-01-26 16:57:10 +00:00
|
|
|
AssertNotMoved();
|
|
|
|
return Program(CloneAsBuilder());
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder Program::CloneAsBuilder() const {
|
|
|
|
AssertNotMoved();
|
|
|
|
ProgramBuilder out;
|
|
|
|
CloneContext(&out, this).Clone();
|
|
|
|
return out;
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Program::IsValid() const {
|
2021-01-26 16:57:10 +00:00
|
|
|
AssertNotMoved();
|
2021-01-27 18:49:05 +00:00
|
|
|
return is_valid_;
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 22:51:23 +00:00
|
|
|
sem::Type* Program::TypeOf(const ast::Expression* expr) const {
|
2021-01-29 16:43:41 +00:00
|
|
|
auto* sem = Sem().Get(expr);
|
|
|
|
return sem ? sem->Type() : nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-30 20:20:19 +00:00
|
|
|
const sem::Type* Program::TypeOf(const ast::Type* type) const {
|
|
|
|
return Sem().Get(type);
|
|
|
|
}
|
|
|
|
|
2021-06-09 14:32:14 +00:00
|
|
|
const sem::Type* Program::TypeOf(const ast::TypeDecl* type_decl) const {
|
|
|
|
return Sem().Get(type_decl);
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:22:40 +00:00
|
|
|
std::string Program::to_str(bool demangle) const {
|
2021-01-26 16:57:10 +00:00
|
|
|
AssertNotMoved();
|
2021-01-29 11:22:40 +00:00
|
|
|
auto str = ast_->to_str(Sem());
|
|
|
|
if (demangle) {
|
|
|
|
str = Demangler().Demangle(Symbols(), str);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Program::str(const ast::Node* node) const {
|
|
|
|
return Demangler().Demangle(Symbols(), node->str(Sem()));
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
void Program::AssertNotMoved() const {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ASSERT(Program, !moved_);
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
} // namespace tint
|