17#ifndef TYPECHECKING_TYPECONSTRAINTS_H_
18#define TYPECHECKING_TYPECONSTRAINTS_H_
21#include "lib/castable.h"
22#include "lib/error_helper.h"
23#include "typeSubstitution.h"
24#include "typeSubstitutionVisitor.h"
25#include "typeUnification.h"
31 absl::flat_hash_set<const IR::Type_Var *, Util::Hash> explained;
35 std::string explanation;
39 return Inspector::init_apply(node);
41 void postorder(
const IR::Type_Var *tv)
override {
42 auto [_, inserted] = explained.emplace(tv);
46 auto val = subst->lookup(tv);
48 explanation +=
"Where '" + tv->toString() +
"' is bound to '" + val->toString() +
"'\n";
50 erec.setCalledBy(
this);
52 explanation += erec.explanation;
61 std::vector<const IR::Node *> errArguments;
75 std::string explain(
size_t index, Explain *explainer)
const {
76 const auto *node = errArguments.at(index);
77 node->apply(*explainer);
78 return explainer->explanation;
80 std::string localError(Explain *explainer)
const;
83 void setError(std::string_view format, std::initializer_list<const IR::Node *> nodes) {
84 errFormat = cstring(format);
87 template <
typename... Args>
90 Args &&...args)
const {
95 boost::format fmt(format);
96 return reportErrorImpl(subst,
97 " ---- Actual error:\n" +
98 ::P4::error_helper(fmt, std::forward<Args>(args)...).toString());
109 const IR::Type *left;
110 const IR::Type *right;
121 void validate()
const {
124 if (left->is<IR::Type_Name>() || right->is<IR::Type_Name>())
125 BUG(
"type names should not appear in unification: %1% and %2%", left, right);
129 virtual BinaryConstraint *create(
const IR::Type *left,
const IR::Type *right)
const = 0;
142 void dbprint(std::ostream &out)
const override {
143 out <<
"Constraint:" << dbp(left) <<
" == " << dbp(right);
147 return reportError(subst,
"Cannot unify type '%1%' with type '%2%'", right, left);
149 BinaryConstraint *create(
const IR::Type *left,
const IR::Type *right)
const override {
165 void dbprint(std::ostream &out)
const override {
166 out <<
"Constraint:" << dbp(left) <<
" := " << dbp(right);
170 return reportError(subst,
"Cannot cast implicitly type '%1%' to type '%2%'", right, left);
172 BinaryConstraint *create(
const IR::Type *left,
const IR::Type *right)
const override {
194 absl::flat_hash_set<const IR::ITypeVar *, Util::Hash> unifiableTypeVariables;
195 std::vector<const TypeConstraint *> constraints;
206 definedVariables(definedVariables),
208 replaceVariables(definedVariables) {}
210 void addUnifiableTypeVariable(
const IR::ITypeVar *typeVariable) {
211 LOG3(
"Adding unifiable type variable " << typeVariable);
212 unifiableTypeVariables.insert(typeVariable);
220 LOG3(
"Adding constraint " << constraint);
221 constraints.push_back(constraint);
223 void addEqualityConstraint(
const IR::Node *source,
const IR::Type *left,
const IR::Type *right);
224 void addImplicitCastConstraint(
const IR::Node *source,
const IR::Type *left,
225 const IR::Type *right);
234 void dbprint(std::ostream &out)
const;
Base class for EqualityConstraint and CanBeImplicitlyCastConstraint.
Definition typeConstraints.h:107
The right type can be implicitly cast to the left type.
Definition typeConstraints.h:157
Requires two types to be equal.
Definition typeConstraints.h:135
Creates a string that describes the values of current type variables.
Definition typeConstraints.h:30
Definition stringify.h:33
Definition typeConstraints.h:56
bool reportError(const TypeVariableSubstitution *subst, const char *format, Args &&...args) const
Definition typeConstraints.h:89
const IR::Node * origin
Place in source code which originated the contraint. May be nullptr.
Definition typeConstraints.h:70
const TypeConstraint * derivedFrom
Constraint which produced this one. May be nullptr.
Definition typeConstraints.h:68
Definition typeConstraints.h:180
bool isUnifiableTypeVariable(const IR::Type *type)
Definition typeConstraints.cpp:53
Definition typeUnification.h:39
Definition typeSubstitution.h:73
Definition typeSubstitutionVisitor.h:45
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24