1 /*
2  * Copyright 2016 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 #ifndef VTS_COMPILATION_TOOLS_VTSC_CODE_GEN_PROFILER_PROFILERCODEGENBASE_H_
18 #define VTS_COMPILATION_TOOLS_VTSC_CODE_GEN_PROFILER_PROFILERCODEGENBASE_H_
19 
20 #include <android-base/macros.h>
21 #include <hidl-util/FQName.h>
22 #include <hidl-util/Formatter.h>
23 #include <iostream>
24 #include <string>
25 
26 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
27 
28 namespace android {
29 namespace vts {
30 /**
31  * Base class that generates the profiler code for HAL interfaces.
32  * It takes the input of a vts proto (i.e. ComponentSpecificationMessage) and
33  * generates the header and source file of the corresponding profiler.
34  *
35  * All the profiler generator for a particular HAL type (e.g. Hidl Hal,
36  * Legacy Hal etc.) should derive from this class.
37  */
38 class ProfilerCodeGenBase {
39  public:
ProfilerCodeGenBase()40   ProfilerCodeGenBase(){};
41 
~ProfilerCodeGenBase()42   virtual ~ProfilerCodeGenBase(){};
43 
44   // Generates both the header and source file for profiler.
45   void GenerateAll(Formatter& header_out, Formatter& source_out,
46     const ComponentSpecificationMessage& message);
47 
48   // Generates the header file for profiler.
49   virtual void GenerateHeaderFile(Formatter &out,
50     const ComponentSpecificationMessage &message);
51 
52   // Generates the source file for profiler.
53   virtual void GenerateSourceFile(Formatter &out,
54     const ComponentSpecificationMessage &message);
55 
56  protected:
57   // Generates the profiler code for scalar type.
58   virtual void GenerateProfilerForScalarVariable(Formatter& out,
59     const VariableSpecificationMessage& val, const std::string& arg_name,
60     const std::string& arg_value) = 0;
61 
62   // Generates the profiler code for string type.
63   virtual void GenerateProfilerForStringVariable(Formatter& out,
64     const VariableSpecificationMessage& val, const std::string& arg_name,
65     const std::string& arg_value) = 0;
66 
67   // Generates the profiler code for enum type.
68   virtual void GenerateProfilerForEnumVariable(Formatter& out,
69     const VariableSpecificationMessage& val, const std::string& arg_name,
70     const std::string& arg_value) = 0;
71 
72   // Generates the profiler code for vector type.
73   virtual void GenerateProfilerForVectorVariable(Formatter& out,
74     const VariableSpecificationMessage& val, const std::string& arg_name,
75     const std::string& arg_value) = 0;
76 
77   // Generates the profiler code for array type.
78   virtual void GenerateProfilerForArrayVariable(Formatter& out,
79     const VariableSpecificationMessage& val, const std::string& arg_name,
80     const std::string& arg_value) = 0;
81 
82   // Generates the profiler code for struct type.
83   virtual void GenerateProfilerForStructVariable(Formatter& out,
84     const VariableSpecificationMessage& val, const std::string& arg_name,
85     const std::string& arg_value) = 0;
86 
87   // Generates the profiler code for union type.
88   virtual void GenerateProfilerForUnionVariable(Formatter& out,
89     const VariableSpecificationMessage& val, const std::string& arg_name,
90     const std::string& arg_value) = 0;
91 
92   // Generates the profiler code for safe union type.
93   virtual void GenerateProfilerForSafeUnionVariable(
94       Formatter& out, const VariableSpecificationMessage& val,
95       const std::string& arg_name, const std::string& arg_value) = 0;
96 
97   // Generates the profiler code for hidl callback type.
98   virtual void GenerateProfilerForHidlCallbackVariable(Formatter& out,
99       const VariableSpecificationMessage& val, const std::string& arg_name,
100       const std::string& arg_value) = 0;
101 
102   // Generates the profiler code for hidl interface type.
103   virtual void GenerateProfilerForHidlInterfaceVariable(Formatter& out,
104       const VariableSpecificationMessage& val, const std::string& arg_name,
105       const std::string& arg_value) = 0;
106 
107   // Generates the profiler code for mask type.
108   virtual void GenerateProfilerForMaskVariable(
109       Formatter& out, const VariableSpecificationMessage& val,
110       const std::string& arg_name, const std::string& arg_value) = 0;
111 
112   // Generates the profiler code for handle type.
113   virtual void GenerateProfilerForHandleVariable(
114       Formatter& out, const VariableSpecificationMessage& val,
115       const std::string& arg_name, const std::string& arg_value) = 0;
116 
117   // Generates the profiler code for hidl memory type.
118   virtual void GenerateProfilerForHidlMemoryVariable(Formatter& out,
119       const VariableSpecificationMessage& val, const std::string& arg_name,
120       const std::string& arg_value) = 0;
121 
122   // Generates the profiler code for pointer type.
123   virtual void GenerateProfilerForPointerVariable(Formatter& out,
124       const VariableSpecificationMessage& val, const std::string& arg_name,
125       const std::string& arg_value) = 0;
126 
127   // Generates the profiler code for fmq sync type.
128   virtual void GenerateProfilerForFMQSyncVariable(Formatter& out,
129       const VariableSpecificationMessage& val, const std::string& arg_name,
130       const std::string& arg_value) = 0;
131 
132   // Generates the profiler code for fmq unsync type.
133   virtual void GenerateProfilerForFMQUnsyncVariable(Formatter& out,
134       const VariableSpecificationMessage& val, const std::string& arg_name,
135       const std::string& arg_value) = 0;
136 
137   // Generates the profiler code for method.
138   virtual void GenerateProfilerForMethod(Formatter& out,
139     const FunctionSpecificationMessage& method) = 0;
140 
141   // Generates the necessary "#include" code for header file of profiler.
142   virtual void GenerateHeaderIncludeFiles(Formatter& out,
143     const ComponentSpecificationMessage& message) = 0;
144   // Generates the necessary "#include" code for source file of profiler.
145   virtual void GenerateSourceIncludeFiles(Formatter& out,
146     const ComponentSpecificationMessage& message) = 0;
147   // Generates the necessary "using" code for profiler.
148   virtual void GenerateUsingDeclaration(Formatter& out,
149     const ComponentSpecificationMessage& message) = 0;
150   // Generates the necessary "#define" code for profiler.
GenerateMacros(Formatter &,const ComponentSpecificationMessage &)151   virtual void GenerateMacros(Formatter&,
152       const ComponentSpecificationMessage&) {};
153   // Generates sanity check for profiler. These codes will be generated at the
154   // beginning of the main profiler function.
GenerateProfilerSanityCheck(Formatter &,const ComponentSpecificationMessage &)155   virtual void GenerateProfilerSanityCheck(
156       Formatter&, const ComponentSpecificationMessage&){};
157   // Generate local variable definition. These codes will be generated after
158   // the sanity check code.
GenerateLocalVariableDefinition(Formatter &,const ComponentSpecificationMessage &)159   virtual void GenerateLocalVariableDefinition(Formatter&,
160     const ComponentSpecificationMessage&) {};
161 
162   // Generates the profiler code for a typed variable.
163   virtual void GenerateProfilerForTypedVariable(Formatter& out,
164     const VariableSpecificationMessage& val, const std::string& arg_name,
165     const std::string& arg_value);
166 
167   // Generates the profiler method declaration for a user defined type.
168   // (e.g. attributes within an interface).
169   // The method signature is:
170   // void profile__UDTName(VariableSpecificationMessage* arg_name,
171   //                       UDTName arg_val_name);
172   virtual void GenerateProfilerMethodDeclForAttribute(Formatter& out,
173     const VariableSpecificationMessage& attribute);
174 
175   // Generates the profiler method implementation for a user defined type.
176   virtual void GenerateProfilerMethodImplForAttribute(Formatter& out,
177     const VariableSpecificationMessage& attribute);
178 
179   //**********   Utility functions   *****************
180   virtual void GenerateOpenNameSpaces(Formatter& out,
181       const ComponentSpecificationMessage& message);
182   virtual void GenerateCloseNameSpaces(Formatter& out,
183       const ComponentSpecificationMessage& message);
184 
185   // Utility functions that check whether the given message uses any hidl native
186   // types (e.g. fmq, hidl_memory, hidl_handle).
187   bool IncludeHidlNativeType(const ComponentSpecificationMessage& message,
188                              const VariableType& type);
189   bool IncludeHidlNativeType(const VariableSpecificationMessage& val,
190                              const VariableType& type);
191 
192   std::string input_vts_file_path_;
193   DISALLOW_COPY_AND_ASSIGN (ProfilerCodeGenBase);
194 };
195 
196 }  // namespace vts
197 }  // namespace android
198 
199 #endif  // VTS_COMPILATION_TOOLS_VTSC_CODE_GEN_PROFILER_PROFILERCODEGENBASE_H_
200