Expression results should not be aliases.

This CL updates the expression class to set the alias value instead of
an alias as the result type.

Change-Id: If19ae394a09ba0dc76380514e53a488bbb5a7292
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/20140
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-04-22 00:23:49 +00:00
parent 50e738eca4
commit 21244880f8
5 changed files with 69 additions and 1 deletions

View File

@ -451,6 +451,7 @@ source_set("tint_unittests_core_src") {
"src/ast/decorated_variable_test.cc",
"src/ast/else_statement_test.cc",
"src/ast/entry_point_test.cc",
"src/ast/expression_test.cc",
"src/ast/fallthrough_statement_test.cc",
"src/ast/float_literal_test.cc",
"src/ast/function_test.cc",

View File

@ -267,6 +267,7 @@ set(TINT_TEST_SRCS
ast/decorated_variable_test.cc
ast/else_statement_test.cc
ast/entry_point_test.cc
ast/expression_test.cc
ast/fallthrough_statement_test.cc
ast/float_literal_test.cc
ast/function_test.cc

View File

@ -24,6 +24,7 @@
#include "src/ast/constructor_expression.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/member_accessor_expression.h"
#include "src/ast/type/alias_type.h"
#include "src/ast/unary_derivative_expression.h"
#include "src/ast/unary_method_expression.h"
#include "src/ast/unary_op_expression.h"
@ -37,6 +38,14 @@ Expression::Expression(const Source& source) : Node(source) {}
Expression::~Expression() = default;
void Expression::set_result_type(type::Type* type) {
// The expression result should never be an alias type
while (type->IsAlias()) {
type = type->AsAlias()->type();
}
result_type_ = type;
}
bool Expression::IsArrayAccessor() const {
return false;
}

View File

@ -43,7 +43,7 @@ class Expression : public Node {
/// Sets the resulting type of this expression
/// @param type the result type to set
void set_result_type(type::Type* type) { result_type_ = type; }
void set_result_type(type::Type* type);
/// @returns the resulting type from this expression
type::Type* result_type() const { return result_type_; }

View File

@ -0,0 +1,57 @@
// 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.
#include "src/ast/expression.h"
#include "gtest/gtest.h"
#include "src/ast/type/alias_type.h"
#include "src/ast/type/i32_type.h"
namespace tint {
namespace ast {
namespace {
class Expr : public Expression {
public:
Expr() : Expression() {}
bool IsValid() const override { return true; }
void to_str(std::ostream&, size_t) const override {}
};
using ExpressionTest = testing::Test;
TEST_F(ExpressionTest, set_result_type) {
type::I32Type i32;
Expr e;
e.set_result_type(&i32);
ASSERT_NE(e.result_type(), nullptr);
EXPECT_TRUE(e.result_type()->IsI32());
}
TEST_F(ExpressionTest, set_result_type_alias) {
type::I32Type i32;
type::AliasType a("a", &i32);
type::AliasType b("b", &a);
Expr e;
e.set_result_type(&b);
ASSERT_NE(e.result_type(), nullptr);
EXPECT_TRUE(e.result_type()->IsI32());
}
} // namespace
} // namespace ast
} // namespace tint