P4C
The P4 Compiler
Loading...
Searching...
No Matches
typeMap.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 FRONTENDS_P4_TYPEMAP_H_
18#define FRONTENDS_P4_TYPEMAP_H_
19
20#include "absl/container/flat_hash_map.h"
21#include "absl/container/flat_hash_set.h"
22#include "frontends/common/programMap.h"
23#include "frontends/p4/typeChecking/typeSubstitution.h"
24
25namespace P4 {
41class TypeMap final : public ProgramMap {
42 // We want to have the same canonical type for two
43 // different tuples, lists, stacks, or p4lists with the same signature.
44 std::vector<const IR::Type *> canonicalTuples;
45 std::vector<const IR::Type *> canonicalStacks;
46 std::vector<const IR::Type *> canonicalP4lists;
47 std::vector<const IR::Type *> canonicalLists;
48
49 // Map each node to its canonical type
50 absl::flat_hash_map<const IR::Node *, const IR::Type *, Util::Hash> typeMap;
51 // All left-values in the program.
52 absl::flat_hash_set<const IR::Expression *, Util::Hash> leftValues;
53 // All compile-time constants. A compile-time constant
54 // is not necessarily a constant - it could be a directionless
55 // parameter as well.
56 absl::flat_hash_set<const IR::Expression *, Util::Hash> constants;
57 // For each type variable in the program the actual
58 // type that is substituted for it.
59 TypeVariableSubstitution allTypeVariables;
60
61 // checks some preconditions before setting the type
62 void checkPrecondition(const IR::Node *element, const IR::Type *type) const;
63
64 public:
65 TypeMap() : ProgramMap("TypeMap"), strictStruct(false) {}
66
70 void setStrictStruct(bool value) { strictStruct = value; }
71 bool contains(const IR::Node *element) { return typeMap.count(element) != 0; }
72 void setType(const IR::Node *element, const IR::Type *type);
73 const IR::Type *getType(const IR::Node *element, bool notNull = false) const;
74 // unwraps a TypeType into its contents
75 const IR::Type *getTypeType(const IR::Node *element, bool notNull) const;
76 void dbprint(std::ostream &out) const;
77 void clear();
78 bool isLeftValue(const IR::Expression *expression) const {
79 return leftValues.count(expression) > 0;
80 }
81 bool isCompileTimeConstant(const IR::Expression *expression) const;
82 size_t size() const { return typeMap.size(); }
83
84 void setLeftValue(const IR::Expression *expression);
85 void cloneExpressionProperties(const IR::Expression *to, const IR::Expression *from);
86 void setCompileTimeConstant(const IR::Expression *expression);
87 void addSubstitutions(const TypeVariableSubstitution *tvs);
88 const IR::Type *getSubstitution(const IR::ITypeVar *var) {
89 return allTypeVariables.lookup(var);
90 }
91 const TypeVariableSubstitution *getSubstitutions() const { return &allTypeVariables; }
92
96 bool equivalent(const IR::Type *left, const IR::Type *right, bool strict = false) const;
100 bool implicitlyConvertibleTo(const IR::Type *from, const IR::Type *to) const;
101
102 // Used for tuples and stacks only
103 const IR::Type *getCanonical(const IR::Type *type);
107 int widthBits(const IR::Type *type, const IR::Node *errorPosition, bool max) const;
108
110 bool typeIsEmpty(const IR::Type *type) const;
111};
112} // namespace P4
113
114#endif /* FRONTENDS_P4_TYPEMAP_H_ */
Definition node.h:94
Definition programMap.h:28
Definition typeMap.h:41
bool equivalent(const IR::Type *left, const IR::Type *right, bool strict=false) const
Definition typeMap.cpp:140
int widthBits(const IR::Type *type, const IR::Node *errorPosition, bool max) const
Definition typeMap.cpp:356
bool implicitlyConvertibleTo(const IR::Type *from, const IR::Type *to) const
Definition typeMap.cpp:303
bool typeIsEmpty(const IR::Type *type) const
True is type occupies no storage.
Definition typeMap.cpp:21
bool strictStruct
Definition typeMap.h:69
Definition typeSubstitution.h:73
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24