Replace ConstructorExpression::(Is|As)* with Castable

Change-Id: I18e9768fef36b79cb0e65c6eb79fd147013c54f0
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34317
Reviewed-by: dan sinclair <dsinclair@chromium.org>
diff --git a/src/ast/builder.h b/src/ast/builder.h
index 1080ead..f80a4f6 100644
--- a/src/ast/builder.h
+++ b/src/ast/builder.h
@@ -35,6 +35,7 @@
 #include "src/ast/type/u32_type.h"
 #include "src/ast/type/vector_type.h"
 #include "src/ast/type/void_type.h"
+#include "src/ast/type_constructor_expression.h"
 #include "src/ast/uint_literal.h"
 #include "src/ast/variable.h"
 #include "src/context.h"
diff --git a/src/ast/constructor_expression.cc b/src/ast/constructor_expression.cc
index 1f19ed8..3326656 100644
--- a/src/ast/constructor_expression.cc
+++ b/src/ast/constructor_expression.cc
@@ -16,9 +16,6 @@
 
 #include <assert.h>
 
-#include "src/ast/scalar_constructor_expression.h"
-#include "src/ast/type_constructor_expression.h"
-
 namespace tint {
 namespace ast {
 
@@ -31,23 +28,5 @@
 ConstructorExpression::ConstructorExpression(const Source& source)
     : Base(source) {}
 
-bool ConstructorExpression::IsScalarConstructor() const {
-  return false;
-}
-
-bool ConstructorExpression::IsTypeConstructor() const {
-  return false;
-}
-
-ScalarConstructorExpression* ConstructorExpression::AsScalarConstructor() {
-  assert(IsScalarConstructor());
-  return static_cast<ScalarConstructorExpression*>(this);
-}
-
-TypeConstructorExpression* ConstructorExpression::AsTypeConstructor() {
-  assert(IsTypeConstructor());
-  return static_cast<TypeConstructorExpression*>(this);
-}
-
 }  // namespace ast
 }  // namespace tint
diff --git a/src/ast/constructor_expression.h b/src/ast/constructor_expression.h
index 3c9e375..9e3e9f2 100644
--- a/src/ast/constructor_expression.h
+++ b/src/ast/constructor_expression.h
@@ -20,25 +20,12 @@
 namespace tint {
 namespace ast {
 
-class ScalarConstructorExpression;
-class TypeConstructorExpression;
-
 /// Base class for constructor style expressions
 class ConstructorExpression
     : public Castable<ConstructorExpression, Expression> {
  public:
   ~ConstructorExpression() override;
 
-  /// @returns true if this is a scalar constructor
-  virtual bool IsScalarConstructor() const;
-  /// @returns true if this is a type constructor
-  virtual bool IsTypeConstructor() const;
-
-  /// @returns this as a scalar constructor expression
-  ScalarConstructorExpression* AsScalarConstructor();
-  /// @returns this as a type constructor expression
-  TypeConstructorExpression* AsTypeConstructor();
-
  protected:
   /// Constructor
   ConstructorExpression();
diff --git a/src/ast/scalar_constructor_expression.cc b/src/ast/scalar_constructor_expression.cc
index 02fee32..2c05b4d 100644
--- a/src/ast/scalar_constructor_expression.cc
+++ b/src/ast/scalar_constructor_expression.cc
@@ -31,10 +31,6 @@
 
 ScalarConstructorExpression::~ScalarConstructorExpression() = default;
 
-bool ScalarConstructorExpression::IsScalarConstructor() const {
-  return true;
-}
-
 bool ScalarConstructorExpression::IsValid() const {
   return literal_ != nullptr;
 }
diff --git a/src/ast/scalar_constructor_expression.h b/src/ast/scalar_constructor_expression.h
index d780fef..273f7bc 100644
--- a/src/ast/scalar_constructor_expression.h
+++ b/src/ast/scalar_constructor_expression.h
@@ -41,9 +41,6 @@
   ScalarConstructorExpression(ScalarConstructorExpression&&);
   ~ScalarConstructorExpression() override;
 
-  /// @returns true if this is a scalar constructor
-  bool IsScalarConstructor() const override;
-
   /// Set the literal value
   /// @param literal the literal
   void set_literal(Literal* literal) { literal_ = literal; }
diff --git a/src/ast/type_constructor_expression.cc b/src/ast/type_constructor_expression.cc
index 8f1d8a8..b0f111f 100644
--- a/src/ast/type_constructor_expression.cc
+++ b/src/ast/type_constructor_expression.cc
@@ -33,10 +33,6 @@
 
 TypeConstructorExpression::~TypeConstructorExpression() = default;
 
-bool TypeConstructorExpression::IsTypeConstructor() const {
-  return true;
-}
-
 bool TypeConstructorExpression::IsValid() const {
   if (values_.empty()) {
     return true;
diff --git a/src/ast/type_constructor_expression.h b/src/ast/type_constructor_expression.h
index ae911e8..4ed73ee 100644
--- a/src/ast/type_constructor_expression.h
+++ b/src/ast/type_constructor_expression.h
@@ -44,9 +44,6 @@
   TypeConstructorExpression(TypeConstructorExpression&&);
   ~TypeConstructorExpression() override;
 
-  /// @returns true if this is a type constructor
-  bool IsTypeConstructor() const override;
-
   /// Set the type
   /// @param type the type
   void set_type(type::Type* type) { type_ = type; }
diff --git a/src/ast/type_constructor_expression_test.cc b/src/ast/type_constructor_expression_test.cc
index c576fed..24eb0e6 100644
--- a/src/ast/type_constructor_expression_test.cc
+++ b/src/ast/type_constructor_expression_test.cc
@@ -53,7 +53,7 @@
 
 TEST_F(TypeConstructorExpressionTest, IsTypeConstructor) {
   TypeConstructorExpression t;
-  EXPECT_TRUE(t.IsTypeConstructor());
+  EXPECT_TRUE(t.Is<ast::TypeConstructorExpression>());
 }
 
 TEST_F(TypeConstructorExpressionTest, IsValid) {
diff --git a/src/inspector/inspector.cc b/src/inspector/inspector.cc
index 655ed3a..a9401a4 100644
--- a/src/inspector/inspector.cc
+++ b/src/inspector/inspector.cc
@@ -131,13 +131,14 @@
     }
 
     auto* constructor = expression->As<ast::ConstructorExpression>();
-    if (!constructor->IsScalarConstructor()) {
+    if (!constructor->Is<ast::ScalarConstructorExpression>()) {
       // This is invalid WGSL, but handling gracefully.
       result[constant_id] = Scalar();
       continue;
     }
 
-    auto* literal = constructor->AsScalarConstructor()->literal();
+    auto* literal =
+        constructor->As<ast::ScalarConstructorExpression>()->literal();
     if (!literal) {
       // This is invalid WGSL, but handling gracefully.
       result[constant_id] = Scalar();
diff --git a/src/transform/bound_array_accessors_transform.cc b/src/transform/bound_array_accessors_transform.cc
index 135518e..a1ea02d 100644
--- a/src/transform/bound_array_accessors_transform.cc
+++ b/src/transform/bound_array_accessors_transform.cc
@@ -150,9 +150,9 @@
     }
   } else if (expr->Is<ast::IdentifierExpression>()) {
     /* nop */
-  } else if (auto* c = expr->As<ast::ConstructorExpression>()) {
-    if (c->IsTypeConstructor()) {
-      for (auto* e : c->AsTypeConstructor()->values()) {
+  } else if (expr->Is<ast::ConstructorExpression>()) {
+    if (auto* c = expr->As<ast::TypeConstructorExpression>()) {
+      for (auto* e : c->values()) {
         if (!ProcessExpression(e)) {
           return false;
         }
diff --git a/src/type_determiner.cc b/src/type_determiner.cc
index 6d3a38e..b0eaa9a 100644
--- a/src/type_determiner.cc
+++ b/src/type_determiner.cc
@@ -831,8 +831,7 @@
 }
 
 bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) {
-  if (expr->IsTypeConstructor()) {
-    auto* ty = expr->AsTypeConstructor();
+  if (auto* ty = expr->As<ast::TypeConstructorExpression>()) {
     for (auto* value : ty->values()) {
       if (!DetermineResultType(value)) {
         return false;
@@ -840,7 +839,8 @@
     }
     expr->set_result_type(ty->type());
   } else {
-    expr->set_result_type(expr->AsScalarConstructor()->literal()->type());
+    expr->set_result_type(
+        expr->As<ast::ScalarConstructorExpression>()->literal()->type());
   }
   return true;
 }
diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc
index 62b46e5..c5c61b5 100644
--- a/src/writer/hlsl/generator_impl.cc
+++ b/src/writer/hlsl/generator_impl.cc
@@ -904,10 +904,11 @@
 bool GeneratorImpl::EmitConstructor(std::ostream& pre,
                                     std::ostream& out,
                                     ast::ConstructorExpression* expr) {
-  if (expr->IsScalarConstructor()) {
-    return EmitScalarConstructor(pre, out, expr->AsScalarConstructor());
+  if (auto* scalar = expr->As<ast::ScalarConstructorExpression>()) {
+    return EmitScalarConstructor(pre, out, scalar);
   }
-  return EmitTypeConstructor(pre, out, expr->AsTypeConstructor());
+  return EmitTypeConstructor(pre, out,
+                             expr->As<ast::TypeConstructorExpression>());
 }
 
 bool GeneratorImpl::EmitScalarConstructor(
diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc
index 92e4433..a1a9d0d 100644
--- a/src/writer/msl/generator_impl.cc
+++ b/src/writer/msl/generator_impl.cc
@@ -882,10 +882,10 @@
 }
 
 bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) {
-  if (expr->IsScalarConstructor()) {
-    return EmitScalarConstructor(expr->AsScalarConstructor());
+  if (auto* scalar = expr->As<ast::ScalarConstructorExpression>()) {
+    return EmitScalarConstructor(scalar);
   }
-  return EmitTypeConstructor(expr->AsTypeConstructor());
+  return EmitTypeConstructor(expr->As<ast::TypeConstructorExpression>());
 }
 
 bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) {
diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc
index ef9dc6f..1f2bdc2 100644
--- a/src/writer/spirv/builder.cc
+++ b/src/writer/spirv/builder.cc
@@ -1157,12 +1157,11 @@
     ast::Variable* var,
     ast::ConstructorExpression* expr,
     bool is_global_init) {
-  if (expr->IsScalarConstructor()) {
-    return GenerateLiteralIfNeeded(var, expr->AsScalarConstructor()->literal());
+  if (auto* scalar = expr->As<ast::ScalarConstructorExpression>()) {
+    return GenerateLiteralIfNeeded(var, scalar->literal());
   }
-  if (expr->IsTypeConstructor()) {
-    return GenerateTypeConstructorExpression(expr->AsTypeConstructor(),
-                                             is_global_init);
+  if (auto* type = expr->As<ast::TypeConstructorExpression>()) {
+    return GenerateTypeConstructorExpression(type, is_global_init);
   }
 
   error_ = "unknown constructor expression";
@@ -1178,7 +1177,7 @@
     return true;
   }
 
-  auto* tc = constructor->AsTypeConstructor();
+  auto* tc = constructor->As<ast::TypeConstructorExpression>();
   auto* result_type = tc->type()->UnwrapAll();
   for (size_t i = 0; i < tc->values().size(); ++i) {
     auto* e = tc->values()[i];
diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc
index 6942a34..0761d80 100644
--- a/src/writer/wgsl/generator_impl.cc
+++ b/src/writer/wgsl/generator_impl.cc
@@ -286,10 +286,10 @@
 }
 
 bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) {
-  if (expr->IsScalarConstructor()) {
-    return EmitScalarConstructor(expr->AsScalarConstructor());
+  if (auto* scalar = expr->As<ast::ScalarConstructorExpression>()) {
+    return EmitScalarConstructor(scalar);
   }
-  return EmitTypeConstructor(expr->AsTypeConstructor());
+  return EmitTypeConstructor(expr->As<ast::TypeConstructorExpression>());
 }
 
 bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {