1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <map>
20 #include <variant>
21 
22 #include "enum_def.h"
23 #include "field_list.h"
24 #include "fields/packet_field.h"
25 #include "parent_def.h"
26 
27 class PacketDef : public ParentDef {
28  public:
29   PacketDef(std::string name, FieldList fields);
30   PacketDef(std::string name, FieldList fields, PacketDef* parent);
31 
32   PacketField* GetNewField(const std::string& name, ParseLocation loc) const;
33 
34   void GenParserDefinition(std::ostream& s) const;
35 
36   void GenTestingParserFromBytes(std::ostream& s) const;
37 
38   void GenParserDefinitionPybind11(std::ostream& s) const;
39 
40   void GenParserFieldGetter(std::ostream& s, const PacketField* field) const;
41 
42   void GenValidator(std::ostream& s) const;
43 
44   void GenParserToString(std::ostream& s) const;
45 
46   TypeDef::Type GetDefinitionType() const;
47 
48   void GenBuilderDefinition(std::ostream& s) const;
49 
50   void GenBuilderDefinitionPybind11(std::ostream& s) const;
51 
52   void GenTestDefine(std::ostream& s) const;
53 
54   void GenFuzzTestDefine(std::ostream& s) const;
55 
56   FieldList GetParametersToValidate() const;
57 
58   void GenBuilderCreate(std::ostream& s) const;
59 
60   void GenBuilderCreatePybind11(std::ostream& s) const;
61 
62   void GenBuilderParameterChecker(std::ostream& s) const;
63 
64   void GenBuilderConstructor(std::ostream& s) const;
65 
66   void GenTestingFromView(std::ostream& s) const;
67 
68   void GenRustChildEnums(std::ostream& s) const;
69 
70   void GenRustStructDeclarations(std::ostream& s) const;
71 
72   bool GenRustStructFieldNameAndType(std::ostream& s) const;
73 
74   void GenRustStructFieldNames(std::ostream& s) const;
75 
76   void GenRustStructImpls(std::ostream& s) const;
77 
78   void GenRustAccessStructImpls(std::ostream& s) const;
79 
80   void GenRustBuilderStructImpls(std::ostream& s) const;
81 
82   void GenRustBuilderTest(std::ostream& s) const;
83 
84   void GenRustDef(std::ostream& s) const;
85 };
86