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