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