P4C
The P4 Compiler
Loading...
Searching...
No Matches
typeSubstitution.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_P4_TYPECHECKING_TYPESUBSTITUTION_H_
9#define FRONTENDS_P4_TYPECHECKING_TYPESUBSTITUTION_H_
10
11#include <map>
12#include <sstream>
13
14#include "ir/ir.h"
15#include "lib/exceptions.h"
16
17namespace P4 {
18
19/* Maps objects of type T to types. */
20template <class T>
21class TypeSubstitution : public IHasDbPrint {
22 protected:
24
25 public:
26 TypeSubstitution() = default;
27 TypeSubstitution(const TypeSubstitution &other) = default;
28 TypeSubstitution &operator=(const TypeSubstitution &other) = default;
29
31 bool isIdentity() const { return binding.size() == 0; }
32 const IR::Type *lookup(T t) const { return ::P4::get(binding, t); }
33 const IR::Type *get(T t) const { return ::P4::get(binding, t); }
34
35 bool containsKey(T key) const { return binding.find(key) != binding.end(); }
36
37 /* This can fail if id is already bound.
38 * @return true on success. */
39 virtual bool setBinding(T id, const IR::Type *type) {
40 CHECK_NULL(id);
41 CHECK_NULL(type);
42 auto it = binding.find(id);
43 if (it != binding.end()) {
44 if (it->second != type) return false;
45 return true;
46 }
47 binding.emplace(id, type);
48 return true;
49 }
50
51 void dbprint(std::ostream &out) const override {
52 if (isIdentity()) out << "Empty substitution";
53 bool first = true;
54 for (auto it : binding) {
55 if (!first) out << std::endl;
56 out << dbp(it.first) << " -> " << dbp(it.second);
57 first = false;
58 }
59 }
60
61 void clear() { binding.clear(); }
62};
63
64class TypeVariableSubstitution final : public TypeSubstitution<const IR::ITypeVar *> {
65 public:
66 TypeVariableSubstitution() = default;
67 TypeVariableSubstitution(const TypeVariableSubstitution &other) = default;
68 bool setBindings(const IR::Node *errorLocation, const IR::TypeParameters *params,
69 const IR::Vector<IR::Type> *args);
73 cstring compose(const IR::ITypeVar *var, const IR::Type *substitution);
74 // In this variant of compose all variables in 'other' that are
75 // assigned to are disjoint from all variables already in 'this'.
76 void simpleCompose(const TypeVariableSubstitution *other);
77 void debugValidate();
78 bool setBinding(const IR::ITypeVar *id, const IR::Type *type) override {
79 auto result = TypeSubstitution::setBinding(id, type);
80 debugValidate();
81 return result;
82 }
83};
84
85} // namespace P4
86
87#endif /* FRONTENDS_P4_TYPECHECKING_TYPESUBSTITUTION_H_ */
Definition stringify.h:33
Definition node.h:53
Definition ir/vector.h:59
bool isIdentity() const
Definition typeSubstitution.h:31
cstring compose(const IR::ITypeVar *var, const IR::Type *substitution)
Definition typeSubstitution.cpp:17
Definition cstring.h:85
Definition ordered_map.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13