ast: Remove Node::set_source()

All nodes that don't have a Source constructor will need to have one added.
We can then find all missing Source mappings with a search for `Source{}`

Bug: tint:396
Bug: tint:390
Change-Id: I06f9689d4da0f3fd1bd757c7358dcc65f15dc752
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35018
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-12-12 01:24:53 +00:00 committed by Commit Bot service account
parent 4226b6a1d8
commit 604bc72dd9
5 changed files with 21 additions and 61 deletions

View File

@ -47,9 +47,6 @@ class Node : public Castable<Node> {
/// @returns the node source data
const Source& source() const { return source_; }
/// Sets the source data
/// @param source the source data
void set_source(const Source& source) { source_ = source; }
/// @returns true if the node is valid
virtual bool IsValid() const = 0;

View File

@ -16,6 +16,7 @@
#define SRC_AST_TEST_HELPER_H_
#include <memory>
#include <string>
#include <utility>
#include "gtest/gtest.h"

View File

@ -608,12 +608,6 @@ class StructuredTraverser {
std::unordered_set<uint32_t> visited_;
};
/// @param src a source record
/// @returns true if `src` is a non-default Source
bool HasSource(const Source& src) {
return src.range.begin.line > 0 || src.range.begin.column != 0;
}
} // namespace
BlockInfo::BlockInfo(const spvtools::opt::BasicBlock& bb)
@ -722,14 +716,6 @@ ast::Statement* FunctionEmitter::AddStatement(ast::Statement* statement) {
return result;
}
ast::Statement* FunctionEmitter::AddStatementForInstruction(
ast::Statement* statement,
const spvtools::opt::Instruction& inst) {
auto* node = AddStatement(statement);
ApplySourceForInstruction(node, inst);
return node;
}
ast::Statement* FunctionEmitter::LastStatement() {
assert(!statements_stack_.empty());
auto* statement_list = statements_stack_.back().statements_;
@ -1880,7 +1866,7 @@ bool FunctionEmitter::EmitFunctionVariables() {
inst.result_id(), ast::StorageClass::kFunction, var_store_type, false,
constructor, ast::VariableDecorationList{});
auto* var_decl_stmt = create<ast::VariableDeclStatement>(var);
AddStatementForInstruction(var_decl_stmt, inst);
AddStatement(var_decl_stmt);
// Save this as an already-named value.
identifier_values_.insert(inst.result_id());
}
@ -2762,8 +2748,7 @@ bool FunctionEmitter::EmitConstDefinition(
if (!ast_const) {
return false;
}
AddStatementForInstruction(create<ast::VariableDeclStatement>(ast_const),
inst);
AddStatement(create<ast::VariableDeclStatement>(ast_const));
// Save this as an already-named value.
identifier_values_.insert(inst.result_id());
return success();
@ -2777,11 +2762,10 @@ bool FunctionEmitter::EmitConstDefOrWriteToHoistedVar(
if (def_info && def_info->requires_hoisted_def) {
auto name = namer_.Name(result_id);
// Emit an assignment of the expression to the hoisted variable.
AddStatementForInstruction(create<ast::AssignmentStatement>(
create<ast::IdentifierExpression>(
ast_module_.RegisterSymbol(name), name),
ast_expr.expr),
inst);
AddStatement(create<ast::AssignmentStatement>(
create<ast::IdentifierExpression>(ast_module_.RegisterSymbol(name),
namer_.Name(result_id)),
ast_expr.expr));
return true;
}
return EmitConstDefinition(inst, ast_expr);
@ -2848,8 +2832,7 @@ bool FunctionEmitter::EmitStatement(const spvtools::opt::Instruction& inst) {
// TODO(dneto): Order of evaluation?
auto lhs = MakeExpression(ptr_id);
auto rhs = MakeExpression(value_id);
AddStatementForInstruction(
create<ast::AssignmentStatement>(lhs.expr, rhs.expr), inst);
AddStatement(create<ast::AssignmentStatement>(lhs.expr, rhs.expr));
return success();
}
case SpvOpLoad: {
@ -3751,8 +3734,7 @@ bool FunctionEmitter::EmitFunctionCall(const spvtools::opt::Instruction& inst) {
}
if (result_type->Is<ast::type::Void>()) {
return nullptr != AddStatementForInstruction(
create<ast::CallStatement>(call_expr), inst);
return nullptr != AddStatement(create<ast::CallStatement>(call_expr));
}
return EmitConstDefOrWriteToHoistedVar(inst, {result_type, call_expr});
@ -3816,16 +3798,9 @@ TypedExpression FunctionEmitter::MakeSimpleSelect(
return {};
}
void FunctionEmitter::ApplySourceForInstruction(
ast::Node* node,
const spvtools::opt::Instruction& inst) {
if (!node) {
return;
}
const Source& existing = node->source();
if (!HasSource(existing)) {
node->set_source(parser_impl_.GetSourceForInst(&inst));
}
Source FunctionEmitter::GetSourceForInst(
const spvtools::opt::Instruction& inst) const {
return parser_impl_.GetSourceForInst(&inst);
}
bool FunctionEmitter::EmitImageAccess(const spvtools::opt::Instruction& inst) {
@ -4028,7 +4003,7 @@ bool FunctionEmitter::EmitImageAccess(const spvtools::opt::Instruction& inst) {
} else {
// It's an image write. No value is returned, so make a statement out
// of the call.
AddStatementForInstruction(create<ast::CallStatement>(call_expr), inst);
AddStatement(create<ast::CallStatement>(call_expr));
}
return success();
}

View File

@ -803,22 +803,10 @@ class FunctionEmitter {
/// @returns a pointer to the statement.
ast::Statement* AddStatement(ast::Statement* statement);
/// Appends a new statement to the top of the statement stack, and attaches
/// source location information from the given instruction. Does nothing if
/// the statement is null.
/// @param statement the new statement
/// @returns a pointer to the statement.
ast::Statement* AddStatementForInstruction(
ast::Statement* statement,
const spvtools::opt::Instruction& inst);
/// Sets the source information for the given instruction to the given
/// node, if the node doesn't already have a source record. Does nothing
/// if `node` is null.
/// @param node the AST node
/// Returns the source record for the given instruction.
/// @param inst the SPIR-V instruction
void ApplySourceForInstruction(ast::Node* node,
const spvtools::opt::Instruction& inst);
/// @return the Source record, or a default one
Source GetSourceForInst(const spvtools::opt::Instruction& inst) const;
/// @returns the last statetment in the top of the statement stack.
ast::Statement* LastStatement();

View File

@ -73,6 +73,7 @@ namespace {
class FakeStmt : public ast::Statement {
public:
explicit FakeStmt(Source source) : ast::Statement(source) {}
FakeStmt* Clone(ast::CloneContext*) const override { return nullptr; }
bool IsValid() const override { return true; }
void to_str(std::ostream& out, size_t) const override { out << "Fake"; }
@ -80,6 +81,7 @@ class FakeStmt : public ast::Statement {
class FakeExpr : public ast::Expression {
public:
explicit FakeExpr(Source source) : ast::Expression(source) {}
FakeExpr* Clone(ast::CloneContext*) const override { return nullptr; }
bool IsValid() const override { return true; }
void to_str(std::ostream&, size_t) const override {}
@ -106,8 +108,7 @@ class TypeDeterminerTestWithParam : public TypeDeterminerHelper,
public testing::TestWithParam<T> {};
TEST_F(TypeDeterminerTest, Error_WithEmptySource) {
FakeStmt s;
s.set_source(Source{});
FakeStmt s(Source{});
EXPECT_FALSE(td()->DetermineResultType(&s));
EXPECT_EQ(td()->error(),
@ -115,8 +116,7 @@ TEST_F(TypeDeterminerTest, Error_WithEmptySource) {
}
TEST_F(TypeDeterminerTest, Stmt_Error_Unknown) {
FakeStmt s;
s.set_source(Source{Source::Location{2, 30}});
FakeStmt s(Source{Source::Location{2, 30}});
EXPECT_FALSE(td()->DetermineResultType(&s));
EXPECT_EQ(td()->error(),
@ -432,8 +432,7 @@ TEST_F(TypeDeterminerTest, Stmt_VariableDecl_ModuleScope) {
}
TEST_F(TypeDeterminerTest, Expr_Error_Unknown) {
FakeExpr e;
e.set_source(Source{Source::Location{2, 30}});
FakeExpr e(Source{Source::Location{2, 30}});
EXPECT_FALSE(td()->DetermineResultType(&e));
EXPECT_EQ(td()->error(), "2:30: unknown expression for type determination");