P4C
The P4 Compiler
Loading...
Searching...
No Matches
typeSpecConverter.h
1/*
2Copyright 2018-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 CONTROL_PLANE_TYPESPECCONVERTER_H_
18#define CONTROL_PLANE_TYPESPECCONVERTER_H_
19
20#include <map>
21#include <string>
22
23#include "ir/ir.h"
24#include "ir/visitor.h"
25#pragma GCC diagnostic push
26#pragma GCC diagnostic ignored "-Wunused-parameter"
27#pragma GCC diagnostic ignored "-Wpedantic"
28#include "p4/config/v1/p4info.pb.h"
29#pragma GCC diagnostic pop
30
31namespace p4 {
32
33class P4DataTypeSpec;
34class P4TypeInfo;
35
36} // namespace p4
37
38namespace P4 {
39
40class TypeMap;
41class ReferenceMap;
42
43namespace ControlPlaneAPI {
44
47 private:
48 const P4::ReferenceMap *refMap;
49 P4::TypeMap *typeMap;
52 ::p4::config::v1::P4TypeInfo *p4RtTypeInfo;
55 std::map<const IR::Type *, ::p4::config::v1::P4DataTypeSpec *> map;
56
57 TypeSpecConverter(const P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
58 ::p4::config::v1::P4TypeInfo *p4RtTypeInfo);
59
60 // fallback for unsupported types, should be unreachable
61 bool preorder(const IR::Type *type) override;
62
63 // anonymous types
64 bool preorder(const IR::Type_Bits *type) override;
65 bool preorder(const IR::Type_Varbits *type) override;
66 bool preorder(const IR::Type_Boolean *type) override;
67 bool preorder(const IR::Type_BaseList *type) override;
68 bool preorder(const IR::Type_Stack *type) override;
69
70 bool preorder(const IR::Type_Name *type) override;
71 bool preorder(const IR::Type_Newtype *type) override;
72
73 // these methods do not update the "map", but update p4RtTypeInfo if it is
74 // not null.
75 bool preorder(const IR::Type_Struct *type) override;
76 bool preorder(const IR::Type_Header *type) override;
77 bool preorder(const IR::Type_HeaderUnion *type) override;
78 bool preorder(const IR::Type_Enum *type) override;
79 bool preorder(const IR::Type_SerEnum *type) override;
80 bool preorder(const IR::Type_Error *type) override;
81
82 public:
86 static const ::p4::config::v1::P4DataTypeSpec *convert(const P4::ReferenceMap *refMap,
87 P4::TypeMap *typeMap,
88 const IR::Type *type,
89 ::p4::config::v1::P4TypeInfo *typeInfo);
90};
91
94 enum Type { kBit, kString };
95 Type type; // Supported controller types are `string` and `bit<W>`.
96 int width; // 0 if type == kString.
97};
98
101 std::string original_type_uri;
102 ControllerType controller_type;
103};
104
108bool hasTranslationAnnotation(const IR::Type *type, TranslationAnnotation *payload);
109
112cstring getTypeName(const IR::Type *type, TypeMap *typeMap);
113
114} // namespace ControlPlaneAPI
115
116} // namespace P4
117
118#endif // CONTROL_PLANE_TYPESPECCONVERTER_H_
Generates the appropriate p4.P4DataTypeSpec for a given IR::Type node.
Definition typeSpecConverter.h:46
static const ::p4::config::v1::P4DataTypeSpec * convert(const P4::ReferenceMap *refMap, P4::TypeMap *typeMap, const IR::Type *type, ::p4::config::v1::P4TypeInfo *typeInfo)
Definition typeSpecConverter.cpp:387
Definition visitor.h:400
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition cstring.h:85
cstring getTypeName(const IR::Type *type, TypeMap *typeMap)
Definition typeSpecConverter.cpp:78
bool hasTranslationAnnotation(const IR::Type *type, TranslationAnnotation *payload)
Definition typeSpecConverter.cpp:43
Payload of @p4runtime_translation annotation.
Definition typeSpecConverter.h:100
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
See section "User-defined types" in P4RT specification.
Definition typeSpecConverter.h:93