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