P4C
The P4 Compiler
Loading...
Searching...
No Matches
typeSubstitutionVisitor.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_TYPESUBSTITUTIONVISITOR_H_
18#define TYPECHECKING_TYPESUBSTITUTIONVISITOR_H_
19
20#include "frontends/p4/typeMap.h"
21#include "ir/ir.h"
22#include "ir/visitor.h"
23#include "lib/log.h"
24#include "typeSubstitution.h"
25
26namespace P4 {
27
32class TypeOccursVisitor : public Inspector {
33 public:
34 const IR::ITypeVar *toFind;
35 bool occurs;
36
37 explicit TypeOccursVisitor(const IR::ITypeVar *toFind) : toFind(toFind), occurs(false) {
38 setName("TypeOccurs");
39 }
40 bool preorder(const IR::Type_Var *typeVariable) override;
41 bool preorder(const IR::Type_InfInt *infint) override;
42};
43
44/* Replaces TypeVariables with other types */
45class TypeVariableSubstitutionVisitor : public Transform {
46 protected:
47 const TypeVariableSubstitution *bindings;
48 bool replace; // If true variables that map to variables are just replaced
49 // in the TypeParameterList of the replaced object; else they
50 // are removed.
51 const IR::Node *replacement(const IR::ITypeVar *original, const IR::Node *node);
52
53 public:
54 explicit TypeVariableSubstitutionVisitor(const TypeVariableSubstitution *bindings,
55 bool replace = false)
56 : bindings(bindings), replace(replace) {
57 setName("TypeVariableSubstitution");
58 }
59
60 const IR::Node *preorder(IR::TypeParameters *tps) override;
61 const IR::Node *preorder(IR::Type_Any *tv) override {
62 return replacement(getOriginal<IR::Type_Any>(), tv);
63 }
64 const IR::Node *preorder(IR::Type_Var *tv) override {
65 return replacement(getOriginal<IR::Type_Var>(), tv);
66 }
67 const IR::Node *preorder(IR::Type_InfInt *ti) override {
68 return replacement(getOriginal<IR::Type_InfInt>(), ti);
69 }
70};
71
72class TypeSubstitutionVisitor : public TypeVariableSubstitutionVisitor {
73 TypeMap *typeMap;
74
75 public:
76 TypeSubstitutionVisitor(TypeMap *typeMap, TypeVariableSubstitution *ts)
77 : TypeVariableSubstitutionVisitor(ts), typeMap(typeMap) {
78 CHECK_NULL(typeMap);
79 setName("TypeSubstitutionVisitor");
80 }
81 const IR::Node *postorder(IR::PathExpression *path) override {
82 // We want fresh nodes for variables, etc.
83 return new IR::PathExpression(path->path->clone());
84 }
85 const IR::Node *postorder(IR::Type_Name *type) override {
86 auto actual = typeMap->getTypeType(getOriginal<IR::Type_Name>(), true);
87 if (auto tv = actual->to<IR::ITypeVar>()) {
88 LOG3("Replacing " << tv);
89 return replacement(tv, type);
90 }
91 return type;
92 }
93};
94
95} // namespace P4
96
97#endif /* TYPECHECKING_TYPESUBSTITUTIONVISITOR_H_ */
Definition node.h:94
Definition visitor.h:413
Definition visitor.h:437
Definition typeMap.h:41
Definition typeSubstitution.h:73
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24