• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015, 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 AIDL_AST_CPP_H_
18 #define AIDL_AST_CPP_H_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <android-base/macros.h>
25 
26 namespace android {
27 namespace aidl {
28 class CodeWriter;
29 }  // namespace aidl
30 }  // namespace android
31 
32 namespace android {
33 namespace aidl {
34 namespace cpp {
35 
36 class AstNode {
37  public:
38   AstNode() = default;
39   virtual ~AstNode() = default;
40   virtual void Write(CodeWriter* to) const = 0;
41 };  // class AstNode
42 
43 class Declaration : public AstNode {
44  public:
45   Declaration() = default;
46   virtual ~Declaration() = default;
47 
48  private:
49   DISALLOW_COPY_AND_ASSIGN(Declaration);
50 };  // class Declaration
51 
52 class ClassDecl : public Declaration {
53  public:
54   ClassDecl(const std::string& name,
55             const std::string& parent);
56   ClassDecl(const std::string& name,
57             const std::string& parent,
58             std::vector<std::unique_ptr<Declaration>> public_members,
59             std::vector<std::unique_ptr<Declaration>> private_members);
60   virtual ~ClassDecl() = default;
61 
62   void Write(CodeWriter* to) const override;
63 
64   void AddPublic(std::unique_ptr<Declaration> member);
65   void AddPrivate(std::unique_ptr<Declaration> member);
66 
67  private:
68   std::string name_;
69   std::string parent_;
70   std::vector<std::unique_ptr<Declaration>> public_members_;
71   std::vector<std::unique_ptr<Declaration>> private_members_;
72 
73   DISALLOW_COPY_AND_ASSIGN(ClassDecl);
74 };  // class ClassDecl
75 
76 class Enum : public Declaration {
77  public:
78   Enum(const std::string& name, const std::string& base_type);
79   explicit Enum(const std::string& name);
80   virtual ~Enum() = default;
81 
HasValues()82   bool HasValues() const { return !fields_.empty(); }
83   void Write(CodeWriter* to) const override;
84 
85   void AddValue(const std::string& key, const std::string& value);
86 
87  private:
88   struct EnumField {
89     EnumField(const std::string& k, const std::string& v);
90     const std::string key;
91     const std::string value;
92   };
93 
94   std::string enum_name_;
95   std::string underlying_type_;
96   std::vector<EnumField> fields_;
97 
98   DISALLOW_COPY_AND_ASSIGN(Enum);
99 };  // class Enum
100 
101 class ArgList : public AstNode {
102  public:
103   ArgList() = default;
104   explicit ArgList(const std::string& single_argument);
105   explicit ArgList(const std::vector<std::string>& arg_list);
106   explicit ArgList(std::vector<std::unique_ptr<AstNode>> arg_list);
107   ArgList(ArgList&& arg_list);
108   virtual ~ArgList() = default;
109 
110   void Write(CodeWriter* to) const override;
111 
112  private:
113   std::vector<std::unique_ptr<AstNode>> arguments_;
114 
115   DISALLOW_COPY_AND_ASSIGN(ArgList);
116 };  // class ArgList
117 
118 class ConstructorDecl : public Declaration {
119  public:
120   enum Modifiers {
121     IS_VIRTUAL = 1 << 0,
122     IS_DEFAULT = 1 << 1,
123     IS_EXPLICIT = 1 << 2,
124   };
125 
126   ConstructorDecl(const std::string& name,
127                   ArgList&& arg_list);
128   ConstructorDecl(const std::string& name,
129                   ArgList&& arg_list,
130                   uint32_t modifiers);
131 
132   virtual ~ConstructorDecl() = default;
133 
134   void Write(CodeWriter* to) const override;
135 
136  private:
137   const std::string name_;
138   const ArgList arguments_;
139   const uint32_t modifiers_;
140 
141   DISALLOW_COPY_AND_ASSIGN(ConstructorDecl);
142 };  // class ConstructorDecl
143 
144 class MethodDecl : public Declaration {
145  public:
146   enum Modifiers {
147     IS_CONST = 1 << 0,
148     IS_VIRTUAL = 1 << 1,
149     IS_OVERRIDE = 1 << 2,
150     IS_PURE_VIRTUAL = 1 << 3,
151   };
152 
153   MethodDecl(const std::string& return_type,
154              const std::string& name,
155              ArgList&& arg_list);
156   MethodDecl(const std::string& return_type,
157              const std::string& name,
158              ArgList&& arg_list,
159              uint32_t modifiers);
160   virtual ~MethodDecl() = default;
161 
162   void Write(CodeWriter* to) const override;
163 
164  private:
165   const std::string return_type_;
166   const std::string name_;
167   const ArgList arguments_;
168   bool is_const_ = false;
169   bool is_virtual_ = false;
170   bool is_override_ = false;
171   bool is_pure_virtual_ = false;
172 
173   DISALLOW_COPY_AND_ASSIGN(MethodDecl);
174 };  // class MethodDecl
175 
176 class StatementBlock : public Declaration {
177  public:
178   StatementBlock() = default;
179   virtual ~StatementBlock() = default;
180 
181   void AddStatement(std::unique_ptr<AstNode> statement);
182   void AddStatement(AstNode* statement);  // Takes ownership
183   void AddLiteral(const std::string& expression, bool add_semicolon = true);
Empty()184   bool Empty() const { return statements_.empty(); }
185 
186   void Write(CodeWriter* to) const override;
187 
188  private:
189   std::vector<std::unique_ptr<AstNode>> statements_;
190 
191   DISALLOW_COPY_AND_ASSIGN(StatementBlock);
192 };  // class StatementBlock
193 
194 class ConstructorImpl : public Declaration {
195  public:
196   ConstructorImpl(const std::string& class_name,
197                   ArgList&& arg_list,
198                   const std::vector<std::string>& initializer_list);
199   virtual ~ConstructorImpl() = default;
200 
201   void Write(CodeWriter* to) const override;
202 
203  private:
204   std::string class_name_;
205   ArgList arguments_;
206   std::vector<std::string> initializer_list_;
207   StatementBlock body_;
208 
209   DISALLOW_COPY_AND_ASSIGN(ConstructorImpl);
210 };  // class ConstructorImpl
211 
212 class MethodImpl : public Declaration {
213  public:
214   // Passing an empty class name causes the method to be declared as a normal
215   // function (ie. no ClassName:: qualifier).
216   MethodImpl(const std::string& return_type,
217              const std::string& class_name,
218              const std::string& method_name,
219              ArgList&& arg_list,
220              bool is_const_method = false);
221   virtual ~MethodImpl() = default;
222 
223   // MethodImpl retains ownership of the statement block.
224   StatementBlock* GetStatementBlock();
225 
226   void Write(CodeWriter* to) const override;
227 
228  private:
229   std::string return_type_;
230   std::string method_name_;
231   const ArgList arguments_;
232   StatementBlock statements_;
233   bool is_const_method_ = false;
234 
235   DISALLOW_COPY_AND_ASSIGN(MethodImpl);
236 };  // class MethodImpl
237 
238 class SwitchStatement : public AstNode {
239  public:
240   explicit SwitchStatement(const std::string& expression);
241   virtual ~SwitchStatement() = default;
242 
243   // Add a case statement and return a pointer code block corresponding
244   // to the case.  The switch statement will add a break statement
245   // after the code block by default to prevent accidental fall-through.
246   // Returns nullptr on duplicate value expressions (by strcmp, not value
247   // equivalence).
248   StatementBlock* AddCase(const std::string& value_expression);
249   void Write(CodeWriter* to) const override;
250 
251  private:
252   const std::string switch_expression_;
253   std::vector<std::string> case_values_;
254   std::vector<std::unique_ptr<StatementBlock>> case_logic_;
255 
256   DISALLOW_COPY_AND_ASSIGN(SwitchStatement);
257 };  // class SwitchStatement
258 
259 class Assignment : public AstNode {
260  public:
261   Assignment(const std::string& left, const std::string& right);
262   Assignment(const std::string& left, AstNode* right);
263   ~Assignment() = default;
264   void Write(CodeWriter* to) const override;
265 
266  private:
267   const std::string lhs_;
268   std::unique_ptr<AstNode> rhs_;
269 
270   DISALLOW_COPY_AND_ASSIGN(Assignment);
271 };  // class Assignment
272 
273 class MethodCall : public AstNode {
274  public:
275   MethodCall(const std::string& method_name,
276              const std::string& single_argument);
277   MethodCall(const std::string& method_name, ArgList&& arg_list);
278   ~MethodCall() = default;
279   void Write(CodeWriter* to) const override;
280 
281  private:
282   const std::string method_name_;
283   const ArgList arguments_;
284 
285   DISALLOW_COPY_AND_ASSIGN(MethodCall);
286 };  // class MethodCall
287 
288 class IfStatement : public AstNode {
289  public:
290   IfStatement(AstNode* expression,
291               bool invert_expression = false);
292   virtual ~IfStatement() = default;
OnTrue()293   StatementBlock* OnTrue() { return &on_true_; }
OnFalse()294   StatementBlock* OnFalse() { return &on_false_; }
295   void Write(CodeWriter* to) const override;
296 
297  private:
298   std::unique_ptr<AstNode> expression_;
299   bool invert_expression_ = false;
300   StatementBlock on_true_;
301   StatementBlock on_false_;
302 
303   DISALLOW_COPY_AND_ASSIGN(IfStatement);
304 };  // class IfStatement
305 
306 class Statement : public AstNode {
307  public:
308   explicit Statement(std::unique_ptr<AstNode> expression);
309   explicit Statement(AstNode* expression);  // Takes possession.
310   explicit Statement(const std::string& expression);
311   ~Statement() = default;
312   void Write(CodeWriter* to) const override;
313 
314  private:
315   std::unique_ptr<AstNode> expression_;
316 
317   DISALLOW_COPY_AND_ASSIGN(Statement);
318 };  // class Statement
319 
320 class Comparison : public AstNode {
321  public:
322   Comparison(AstNode* lhs, const std::string& comparison, AstNode* rhs);
323   ~Comparison() = default;
324   void Write(CodeWriter* to) const override;
325 
326  private:
327   std::unique_ptr<AstNode> left_;
328   std::unique_ptr<AstNode> right_;
329   const std::string operator_;
330 
331   DISALLOW_COPY_AND_ASSIGN(Comparison);
332 };  // class Comparison
333 
334 class LiteralExpression : public AstNode {
335  public:
336   explicit LiteralExpression(const std::string& expression);
337   ~LiteralExpression() = default;
338   void Write(CodeWriter* to) const override;
339 
340  private:
341   const std::string expression_;
342 
343   DISALLOW_COPY_AND_ASSIGN(LiteralExpression);
344 };  // class LiteralExpression
345 
346 class CppNamespace : public Declaration {
347  public:
348   CppNamespace(const std::string& name,
349                std::vector<std::unique_ptr<Declaration>> declarations);
350   CppNamespace(const std::string& name,
351                std::unique_ptr<Declaration> declaration);
352   CppNamespace(const std::string& name);
353   virtual ~CppNamespace() = default;
354 
355   void Write(CodeWriter* to) const override;
356 
357  private:
358   std::vector<std::unique_ptr<Declaration>> declarations_;
359   std::string name_;
360 
361   DISALLOW_COPY_AND_ASSIGN(CppNamespace);
362 };  // class CppNamespace
363 
364 class Document : public AstNode {
365  public:
366   Document(const std::vector<std::string>& include_list,
367            std::unique_ptr<CppNamespace> a_namespace);
368 
369   void Write(CodeWriter* to) const override;
370 
371  private:
372   std::vector<std::string> include_list_;
373   std::unique_ptr<CppNamespace> namespace_;
374 
375   DISALLOW_COPY_AND_ASSIGN(Document);
376 };  // class Document
377 
378 class CppHeader final : public Document {
379  public:
380   CppHeader(const std::string& include_guard,
381             const std::vector<std::string>& include_list,
382             std::unique_ptr<CppNamespace> a_namespace);
383   void Write(CodeWriter* to) const override;
384 
385  private:
386   const std::string include_guard_;
387 
388   DISALLOW_COPY_AND_ASSIGN(CppHeader);
389 };  // class CppHeader
390 
391 class CppSource final : public Document {
392  public:
393   CppSource(const std::vector<std::string>& include_list,
394             std::unique_ptr<CppNamespace> a_namespace);
395 
396  private:
397   DISALLOW_COPY_AND_ASSIGN(CppSource);
398 };  // class CppSource
399 
400 }  // namespace cpp
401 }  // namespace aidl
402 }  // namespace android
403 
404 #endif // AIDL_AST_CPP_H_
405