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