diff --git a/ir/base.def b/ir/base.def index aaaa1f4b4c9..55672794acc 100644 --- a/ir/base.def +++ b/ir/base.def @@ -332,17 +332,36 @@ class Annotation { Vector, IndexedVector> body; - inline auto &getUnparsed() { return std::get(body); } - inline const auto &getUnparsed() const { return std::get(body); } - inline auto &getExpr() { return std::get(body); } - inline const auto &getExpr() const { return std::get(body); } + inline auto &getUnparsed() { + BUG_CHECK(annotationKind() == Kind::Unparsed, "Annotation has been parsed already."); + return std::get(body); + } + inline const auto &getUnparsed() const { + BUG_CHECK(annotationKind() == Kind::Unparsed, "Annotation has been parsed already."); + return std::get(body); + } + inline auto &getExpr() { + BUG_CHECK(annotationKind() == Kind::Unstructured || annotationKind() == Kind::StructuredExpressionList, "Annotation does not contain an expression list."); + return std::get(body); + } + inline const auto &getExpr() const { + BUG_CHECK(annotationKind() == Kind::Unstructured || annotationKind() == Kind::StructuredExpressionList, "Annotation does not contain an expression list."); + return std::get(body); + } inline Expression getExpr(size_t idx) const { + BUG_CHECK(annotationKind() == Kind::Unstructured || annotationKind() == Kind::StructuredExpressionList, "Annotation does not contain an expression list."); const auto &expr = getExpr(); BUG_CHECK(idx < expr.size(), "invalid annotation expression index"); return expr[idx]; } - inline auto &getKV() { return std::get(body); } - inline const auto &getKV() const { return std::get(body); } + inline auto &getKV() { + BUG_CHECK(annotationKind() == Kind::StructuredKVList, "Annotation does not contain a key-value list."); + return std::get(body); + } + inline const auto &getKV() const { + BUG_CHECK(annotationKind() == Kind::StructuredKVList, "Annotation does not contain a key-value list."); + return std::get(body); + } /// If this is true this is a structured annotation, and there are some /// constraints on its contents.