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"
|
|
|
|
|
|
|
|
#include <sstream>
|
2021-01-26 16:57:10 +00:00
|
|
|
#include <utility>
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/ast/module.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/clone_context.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/program_builder.h"
|
2021-01-27 18:49:05 +00:00
|
|
|
#include "src/type_determiner.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
|
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)
|
|
|
|
: types_(std::move(program.types_)),
|
|
|
|
nodes_(std::move(program.nodes_)),
|
|
|
|
ast_(std::move(program.ast_)),
|
2021-01-27 18:49:05 +00:00
|
|
|
symbols_(std::move(program.symbols_)),
|
|
|
|
diagnostics_(std::move(program.diagnostics_)),
|
|
|
|
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-01-27 19:23:55 +00:00
|
|
|
is_valid_ = builder.IsValid();
|
|
|
|
if (builder.ResolveOnBuild() && builder.IsValid()) {
|
|
|
|
TypeDeterminer td(&builder);
|
|
|
|
if (!td.Determine()) {
|
|
|
|
diagnostics_.add_error(td.error());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
nodes_ = std::move(builder.Nodes());
|
|
|
|
ast_ = nodes_.Create<ast::Module>(Source{}, builder.AST().ConstructedTypes(),
|
|
|
|
builder.AST().Functions(),
|
|
|
|
builder.AST().GlobalVariables());
|
|
|
|
symbols_ = std::move(builder.Symbols());
|
|
|
|
diagnostics_ = std::move(builder.Diagnostics());
|
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.
|
|
|
|
diagnostics_.add_error("invalid program generated");
|
|
|
|
}
|
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;
|
|
|
|
types_ = std::move(program.types_);
|
|
|
|
nodes_ = std::move(program.nodes_);
|
|
|
|
ast_ = std::move(program.ast_);
|
|
|
|
symbols_ = std::move(program.symbols_);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
std::string Program::to_str() const {
|
2021-01-26 16:57:10 +00:00
|
|
|
AssertNotMoved();
|
2021-01-26 16:57:10 +00:00
|
|
|
return ast_->to_str();
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
void Program::AssertNotMoved() const {
|
|
|
|
assert(!moved_);
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
} // namespace tint
|