P4C
The P4 Compiler
Loading...
Searching...
No Matches
constantTypeSubstitution.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
8http://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 FRONTENDS_P4_TYPECHECKING_CONSTANTTYPESUBSTITUTION_H_
18#define FRONTENDS_P4_TYPECHECKING_CONSTANTTYPESUBSTITUTION_H_
19
20#include "frontends/common/resolveReferences/resolveReferences.h"
21#include "ir/visitor.h"
22#include "typeChecker.h"
23#include "typeSubstitution.h"
24
25namespace P4 {
26
27// Used to set the type of Constants after type inference
30 TypeMap *typeMap;
32
33 public:
36 : subst(subst), typeMap(typeMap), tc(tc) {
37 CHECK_NULL(subst);
38 CHECK_NULL(typeMap);
39 CHECK_NULL(tc);
40 LOG3("ConstantTypeSubstitution " << subst);
41 }
42
43 const IR::Node *postorder(IR::Constant *cst) override {
44 auto cstType = typeMap->getType(getOriginal(), true);
45 if (!cstType->is<IR::ITypeVar>()) return cst;
46 auto repl = cstType;
47 while (repl->is<IR::ITypeVar>()) {
48 auto next = subst->get(repl->to<IR::ITypeVar>());
49 BUG_CHECK(next != repl, "Cycle in substitutions: %1%", next);
50 if (!next) break;
51 repl = next;
52 }
53 if (repl != cstType) {
54 // We may replace a type variable with another one
55 LOG2("Inferred type " << repl << " for " << cst);
56 cst = new IR::Constant(cst->srcInfo, repl, cst->value, cst->base);
57 } else {
58 LOG2("No type inferred for " << cst << " repl is " << repl);
59 }
60 return cst;
61 }
62
63 const IR::Expression *convert(const IR::Expression *expr, const Visitor::Context *ctxt) {
64 auto result = expr->apply(*this, ctxt)->to<IR::Expression>();
65 if (result != expr && (::P4::errorCount() == 0)) tc->learn(result, this, ctxt);
66 return result;
67 }
69 const Visitor::Context *ctxt) {
70 auto result = vec->apply(*this, ctxt)->to<IR::Vector<IR::Expression>>();
71 if (result != vec) tc->learn(result, this, ctxt);
72 return result;
73 }
74 const IR::Vector<IR::Argument> *convert(const IR::Vector<IR::Argument> *vec,
75 const Visitor::Context *ctxt) {
76 auto result = vec->apply(*this, ctxt)->to<IR::Vector<IR::Argument>>();
77 if (result != vec) tc->learn(result, this, ctxt);
78 return result;
79 }
80};
81
82} // namespace P4
83
84#endif // FRONTENDS_P4_TYPECHECKING_CONSTANTTYPESUBSTITUTION_H_
Definition constantTypeSubstitution.h:28
Definition node.h:95
Definition vector.h:58
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:33
Definition visitor.h:424
Definition typeChecker.h:80
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
unsigned errorCount()
Definition error.h:35
T * to() noexcept
Definition rtti.h:226
Definition visitor.h:47