Enhance MLIRContext and operations with the ability to register diagnostic
handlers and to feed them with errors and warnings produced by the compiler.
Enhance Operation to be able to get its own MLIRContext on demand, simplifying
some clients.  Change the verifier to emit certain errors with the diagnostic
handler.

This is steps towards reworking the verifier and diagnostic propagation but is
itself not particularly useful.  More to come.

PiperOrigin-RevId: 206948643
diff --git a/lib/IR/Statement.cpp b/lib/IR/Statement.cpp
index b55b597..3bbcdd4 100644
--- a/lib/IR/Statement.cpp
+++ b/lib/IR/Statement.cpp
@@ -177,6 +177,20 @@
   free(this);
 }
 
+/// Return the context this operation is associated with.
+MLIRContext *OperationStmt::getContext() const {
+  // If we have a result or operand type, that is a constant time way to get
+  // to the context.
+  if (getNumResults())
+    return getResult(0)->getType()->getContext();
+  if (getNumOperands())
+    return getOperand(0)->getType()->getContext();
+
+  // In the very odd case where we have no operands or results, fall back to
+  // doing a find.
+  return findFunction()->getContext();
+}
+
 /// This drops all operand uses from this statement, which is an essential
 /// step in breaking cyclic dependences between references when they are to
 /// be deleted.