Add TINT_UNREACHABLE() and TINT_ICE() helper macros

Appends an error message with the tint compiler source location to the
provided diagnositic list, and then calls the global error handler if
one is set.
Tests and the sample app now register an error handler to print the
diagnostic list to stderr and abort when NDEBUG is not defined.

All uses of assert(false) have been fixed up to use these macros.

Change-Id: I2f63e51ed86ac23883301d280070bd1a357c6cb2
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/41620
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc
index 674709d..754327a 100644
--- a/src/writer/wgsl/generator_impl.cc
+++ b/src/writer/wgsl/generator_impl.cc
@@ -58,6 +58,7 @@
 #include "src/ast/variable.h"
 #include "src/ast/variable_decl_statement.h"
 #include "src/ast/workgroup_decoration.h"
+#include "src/debug.h"
 #include "src/program.h"
 #include "src/semantic/function.h"
 #include "src/semantic/variable.h"
@@ -120,7 +121,8 @@
         return false;
       }
     } else {
-      assert(false /* unreachable */);
+      TINT_UNREACHABLE(diagnostics_);
+      return false;
     }
 
     if (decl != program_->AST().GlobalDeclarations().back()) {
@@ -136,7 +138,7 @@
   auto* func =
       program_->AST().Functions().Find(program_->Symbols().Get(name), stage);
   if (func == nullptr) {
-    error_ = "Unable to find requested entry point: " + name;
+    diagnostics_.add_error("Unable to find requested entry point: " + name);
     return false;
   }
   return Generate(func);
@@ -155,7 +157,7 @@
       return false;
     }
   } else {
-    error_ = "unknown constructed type: " + ty->type_name();
+    diagnostics_.add_error("unknown constructed type: " + ty->type_name());
     return false;
   }
 
@@ -188,7 +190,7 @@
     return EmitUnaryOp(u);
   }
 
-  error_ = "unknown expression type";
+  diagnostics_.add_error("unknown expression type");
   return false;
 }
 
@@ -300,7 +302,7 @@
   } else if (auto* ul = lit->As<ast::UintLiteral>()) {
     out_ << ul->value() << "u";
   } else {
-    error_ = "unknown literal type";
+    diagnostics_.add_error("unknown literal type");
     return false;
   }
   return true;
@@ -360,7 +362,7 @@
 bool GeneratorImpl::EmitImageFormat(const type::ImageFormat fmt) {
   switch (fmt) {
     case type::ImageFormat::kNone:
-      error_ = "unknown image format";
+      diagnostics_.add_error("unknown image format");
       return false;
     default:
       out_ << fmt;
@@ -379,7 +381,7 @@
     } else if (ac->IsReadWrite()) {
       out_ << "read_write";
     } else {
-      error_ = "invalid access control";
+      diagnostics_.add_error("invalid access control");
       return false;
     }
     out_ << ")]]" << std::endl;
@@ -444,7 +446,7 @@
     } else if (texture->Is<type::StorageTexture>()) {
       out_ << "storage_";
     } else {
-      error_ = "unknown texture type";
+      diagnostics_.add_error("unknown texture type");
       return false;
     }
 
@@ -471,7 +473,7 @@
         out_ << "cube_array";
         break;
       default:
-        error_ = "unknown texture dimension";
+        diagnostics_.add_error("unknown texture dimension");
         return false;
     }
 
@@ -506,7 +508,7 @@
   } else if (type->Is<type::Void>()) {
     out_ << "void";
   } else {
-    error_ = "unknown type in EmitType: " + type->type_name();
+    diagnostics_.add_error("unknown type in EmitType: " + type->type_name());
     return false;
   }
 
@@ -605,7 +607,7 @@
     } else if (auto* constant = deco->As<ast::ConstantIdDecoration>()) {
       out_ << "constant_id(" << constant->value() << ")";
     } else {
-      error_ = "unknown variable decoration";
+      diagnostics_.add_error("unknown variable decoration");
       return false;
     }
   }
@@ -678,7 +680,7 @@
       out_ << "%";
       break;
     case ast::BinaryOp::kNone:
-      error_ = "missing binary operation type";
+      diagnostics_.add_error("missing binary operation type");
       return false;
   }
   out_ << " ";
@@ -789,7 +791,7 @@
     return EmitVariable(v->variable());
   }
 
-  error_ = "unknown statement type: " + program_->str(stmt);
+  diagnostics_.add_error("unknown statement type: " + program_->str(stmt));
   return false;
 }