P4C
The P4 Compiler
Loading...
Searching...
No Matches
typeSubstitution.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef TYPECHECKING_TYPESUBSTITUTION_H_
18#define TYPECHECKING_TYPESUBSTITUTION_H_
19
20#include <map>
21#include <sstream>
22
23#include "ir/ir.h"
24#include "lib/exceptions.h"
25
26namespace P4 {
27
28/* Maps objects of type T to types. */
29template <class T>
31 protected:
33
34 public:
35 TypeSubstitution() = default;
36 TypeSubstitution(const TypeSubstitution &other) = default;
37 TypeSubstitution &operator=(const TypeSubstitution &other) = default;
38
40 bool isIdentity() const { return binding.size() == 0; }
41 const IR::Type *lookup(T t) const { return ::P4::get(binding, t); }
42 const IR::Type *get(T t) const { return ::P4::get(binding, t); }
43
44 bool containsKey(T key) const { return binding.find(key) != binding.end(); }
45
46 /* This can fail if id is already bound.
47 * @return true on success. */
48 virtual bool setBinding(T id, const IR::Type *type) {
49 CHECK_NULL(id);
50 CHECK_NULL(type);
51 auto it = binding.find(id);
52 if (it != binding.end()) {
53 if (it->second != type) return false;
54 return true;
55 }
56 binding.emplace(id, type);
57 return true;
58 }
59
60 void dbprint(std::ostream &out) const {
61 if (isIdentity()) out << "Empty substitution";
62 bool first = true;
63 for (auto it : binding) {
64 if (!first) out << std::endl;
65 out << dbp(it.first) << " " << it.first << " -> " << dbp(it.second) << " " << it.second;
66 first = false;
67 }
68 }
69
70 void clear() { binding.clear(); }
71};
72
73class TypeVariableSubstitution final : public TypeSubstitution<const IR::ITypeVar *> {
74 public:
75 TypeVariableSubstitution() = default;
77 bool setBindings(const IR::Node *errorLocation, const IR::TypeParameters *params,
78 const IR::Vector<IR::Type> *args);
82 cstring compose(const IR::ITypeVar *var, const IR::Type *substitution);
83 // In this variant of compose all variables in 'other' that are
84 // assigned to are disjoint from all variables already in 'this'.
85 void simpleCompose(const TypeVariableSubstitution *other);
86 void debugValidate();
87 bool setBinding(const IR::ITypeVar *id, const IR::Type *type) override {
88 auto result = TypeSubstitution::setBinding(id, type);
89 debugValidate();
90 return result;
91 }
92};
93
94} // namespace P4
95
96#endif /* TYPECHECKING_TYPESUBSTITUTION_H_ */
Definition stringify.h:33
Definition node.h:95
Definition vector.h:59
Definition typeSubstitution.h:30
bool isIdentity() const
Definition typeSubstitution.h:40
Definition typeSubstitution.h:73
cstring compose(const IR::ITypeVar *var, const IR::Type *substitution)
Definition typeSubstitution.cpp:28
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:24