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