blob: b78bc6b4d1a7686d9f9e128055194c660dba2351 [file] [log] [blame]
Christopher Wileyf600a552015-09-12 14:07:44 -07001/*
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
Christopher Wileyf944e792015-09-29 10:00:46 -070017#ifndef AIDL_AST_CPP_H_
18#define AIDL_AST_CPP_H_
Christopher Wileyf600a552015-09-12 14:07:44 -070019
Casey Dahlin60a49162015-09-17 14:23:10 -070020#include <memory>
Christopher Wileyf600a552015-09-12 14:07:44 -070021#include <string>
22#include <vector>
23
24#include <base/macros.h>
25
26namespace android {
27namespace aidl {
Christopher Wileyf600a552015-09-12 14:07:44 -070028class CodeWriter;
Christopher Wileyf944e792015-09-29 10:00:46 -070029} // namespace aidl
30} // namespace android
Christopher Wileyf600a552015-09-12 14:07:44 -070031
Christopher Wileyf944e792015-09-29 10:00:46 -070032namespace android {
33namespace aidl {
34namespace cpp {
35
36class AstNode {
Casey Dahlin34b86102015-09-16 16:03:06 -070037 public:
Christopher Wileyf944e792015-09-29 10:00:46 -070038 AstNode() = default;
39 virtual ~AstNode() = default;
Casey Dahlin34b86102015-09-16 16:03:06 -070040 virtual void Write(CodeWriter* to) const = 0;
Christopher Wileyf944e792015-09-29 10:00:46 -070041}; // class AstNode
Casey Dahlin34b86102015-09-16 16:03:06 -070042
Christopher Wileyf944e792015-09-29 10:00:46 -070043class Declaration : public AstNode {
Casey Dahlin34b86102015-09-16 16:03:06 -070044 public:
Christopher Wileyf944e792015-09-29 10:00:46 -070045 Declaration() = default;
46 virtual ~Declaration() = default;
Casey Dahlin34b86102015-09-16 16:03:06 -070047
48 private:
Christopher Wileyf944e792015-09-29 10:00:46 -070049 DISALLOW_COPY_AND_ASSIGN(Declaration);
50}; // class Declaration
Casey Dahlin34b86102015-09-16 16:03:06 -070051
Christopher Wileyf944e792015-09-29 10:00:46 -070052class ClassDecl : public Declaration {
Casey Dahlin60a49162015-09-17 14:23:10 -070053 public:
Christopher Wileyf944e792015-09-29 10:00:46 -070054 ClassDecl(const std::string& name,
Christopher Wiley0c732db2015-09-29 14:36:44 -070055 const std::string& parent);
56 ClassDecl(const std::string& name,
Christopher Wileyf944e792015-09-29 10:00:46 -070057 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;
Casey Dahlin60a49162015-09-17 14:23:10 -070061
62 void Write(CodeWriter* to) const override;
63
Christopher Wiley0c732db2015-09-29 14:36:44 -070064 void AddPublic(std::unique_ptr<Declaration> member);
65 void AddPrivate(std::unique_ptr<Declaration> member);
66
Casey Dahlin60a49162015-09-17 14:23:10 -070067 private:
68 std::string name_;
69 std::string parent_;
Christopher Wileyf944e792015-09-29 10:00:46 -070070 std::vector<std::unique_ptr<Declaration>> public_members_;
71 std::vector<std::unique_ptr<Declaration>> private_members_;
Casey Dahlin60a49162015-09-17 14:23:10 -070072
Christopher Wileyf944e792015-09-29 10:00:46 -070073 DISALLOW_COPY_AND_ASSIGN(ClassDecl);
74}; // class ClassDecl
Casey Dahlin60a49162015-09-17 14:23:10 -070075
Christopher Wileya7a5c102015-09-29 16:26:52 -070076class Enum : public Declaration {
77 public:
Christopher Wiley23285262015-10-09 15:06:14 -070078 explicit Enum(const std::string& name);
Christopher Wileya7a5c102015-09-29 16:26:52 -070079 virtual ~Enum() = default;
80
81 void Write(CodeWriter* to) const override;
82
83 void AddValue(const std::string& key, const std::string& value);
84
85 private:
86 struct EnumField {
87 EnumField(const std::string& k, const std::string& v);
88 const std::string key;
89 const std::string value;
90 };
91
92 std::string enum_name_;
93 std::vector<EnumField> fields_;
94
95 DISALLOW_COPY_AND_ASSIGN(Enum);
96}; // class Enum
97
Christopher Wiley23285262015-10-09 15:06:14 -070098class ArgList : public AstNode {
99 public:
Christopher Wileyade4b452015-10-10 11:06:03 -0700100 ArgList() = default;
Christopher Wiley23285262015-10-09 15:06:14 -0700101 explicit ArgList(const std::string& single_argument);
102 explicit ArgList(const std::vector<std::string>& arg_list);
Christopher Wileyade4b452015-10-10 11:06:03 -0700103 ArgList(ArgList&& arg_list);
Christopher Wiley23285262015-10-09 15:06:14 -0700104 virtual ~ArgList() = default;
105
106 void Write(CodeWriter* to) const override;
107
108 private:
109 std::vector<std::string> arguments_;
110
111 DISALLOW_COPY_AND_ASSIGN(ArgList);
112}; // class ArgList
113
Christopher Wileyf944e792015-09-29 10:00:46 -0700114class ConstructorDecl : public Declaration {
Casey Dahlina834dd42015-09-23 11:52:15 -0700115 public:
Christopher Wileyb23149d2015-10-14 13:52:21 -0700116 enum Modifiers {
117 IS_VIRTUAL = 1 << 0,
118 IS_DEFAULT = 1 << 1,
119 IS_EXPLICIT = 1 << 2,
120 };
121
Christopher Wileyf944e792015-09-29 10:00:46 -0700122 ConstructorDecl(const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700123 ArgList&& arg_list);
Christopher Wileya7a5c102015-09-29 16:26:52 -0700124 ConstructorDecl(const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700125 ArgList&& arg_list,
Christopher Wileyb23149d2015-10-14 13:52:21 -0700126 uint32_t modifiers);
Casey Dahlina834dd42015-09-23 11:52:15 -0700127
Christopher Wileyf944e792015-09-29 10:00:46 -0700128 virtual ~ConstructorDecl() = default;
Casey Dahlina834dd42015-09-23 11:52:15 -0700129
130 void Write(CodeWriter* to) const override;
131
132 private:
133 const std::string name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700134 const ArgList arguments_;
Christopher Wileyb23149d2015-10-14 13:52:21 -0700135 const uint32_t modifiers_;
Casey Dahlina834dd42015-09-23 11:52:15 -0700136
Christopher Wileyf944e792015-09-29 10:00:46 -0700137 DISALLOW_COPY_AND_ASSIGN(ConstructorDecl);
Christopher Wileyda695992015-10-05 11:31:41 -0700138}; // class ConstructorDecl
Casey Dahlina834dd42015-09-23 11:52:15 -0700139
Christopher Wileyf944e792015-09-29 10:00:46 -0700140class MethodDecl : public Declaration {
Casey Dahlin88924d62015-09-17 16:28:24 -0700141 public:
Christopher Wiley0c732db2015-09-29 14:36:44 -0700142 enum Modifiers {
143 IS_CONST = 1 << 0,
144 IS_VIRTUAL = 1 << 1,
145 IS_OVERRIDE = 1 << 2,
146 IS_PURE_VIRTUAL = 1 << 3,
147 };
148
149 MethodDecl(const std::string& return_type,
150 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700151 ArgList&& arg_list);
Christopher Wileyf944e792015-09-29 10:00:46 -0700152 MethodDecl(const std::string& return_type,
153 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700154 ArgList&& arg_list,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700155 uint32_t modifiers);
Christopher Wileyf944e792015-09-29 10:00:46 -0700156 virtual ~MethodDecl() = default;
Casey Dahlin88924d62015-09-17 16:28:24 -0700157
158 void Write(CodeWriter* to) const override;
159
160 private:
161 const std::string return_type_;
162 const std::string name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700163 const ArgList arguments_;
Christopher Wiley0c732db2015-09-29 14:36:44 -0700164 bool is_const_ = false;
165 bool is_virtual_ = false;
166 bool is_override_ = false;
167 bool is_pure_virtual_ = false;
Casey Dahlin88924d62015-09-17 16:28:24 -0700168
Christopher Wileyf944e792015-09-29 10:00:46 -0700169 DISALLOW_COPY_AND_ASSIGN(MethodDecl);
Christopher Wileyda695992015-10-05 11:31:41 -0700170}; // class MethodDecl
171
172class StatementBlock : public Declaration {
173 public:
174 StatementBlock() = default;
175 virtual ~StatementBlock() = default;
176
177 void AddStatement(std::unique_ptr<AstNode> statement);
Christopher Wiley23285262015-10-09 15:06:14 -0700178 void AddStatement(AstNode* statement); // Takes ownership
Christopher Wileyda695992015-10-05 11:31:41 -0700179 void AddLiteral(const std::string& expression, bool add_semicolon = true);
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700180 bool Empty() const { return statements_.empty(); }
Christopher Wileyda695992015-10-05 11:31:41 -0700181
182 void Write(CodeWriter* to) const override;
183
184 private:
185 std::vector<std::unique_ptr<AstNode>> statements_;
186
187 DISALLOW_COPY_AND_ASSIGN(StatementBlock);
188}; // class StatementBlock
189
Christopher Wileyf9688b02015-10-08 17:17:50 -0700190class ConstructorImpl : public Declaration {
191 public:
192 ConstructorImpl(const std::string& class_name,
193 ArgList&& arg_list,
194 const std::vector<std::string>& initializer_list);
195 virtual ~ConstructorImpl() = default;
196
197 void Write(CodeWriter* to) const override;
198
199 private:
200 std::string class_name_;
201 ArgList arguments_;
202 std::vector<std::string> initializer_list_;
203 StatementBlock body_;
204
205 DISALLOW_COPY_AND_ASSIGN(ConstructorImpl);
206}; // class ConstructorImpl
207
Christopher Wileyda695992015-10-05 11:31:41 -0700208class MethodImpl : public Declaration {
209 public:
210 // Passing an empty class name causes the method to be declared as a normal
211 // function (ie. no ClassName:: qualifier).
212 MethodImpl(const std::string& return_type,
213 const std::string& class_name,
214 const std::string& method_name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700215 ArgList&& arg_list,
Christopher Wileyda695992015-10-05 11:31:41 -0700216 bool is_const_method = false);
217 virtual ~MethodImpl() = default;
218
Christopher Wileyf9688b02015-10-08 17:17:50 -0700219 // MethodImpl retains ownership of the statement block.
220 StatementBlock* GetStatementBlock();
221
Christopher Wileyda695992015-10-05 11:31:41 -0700222 void Write(CodeWriter* to) const override;
223
224 private:
225 std::string return_type_;
226 std::string method_name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700227 const ArgList arguments_;
Christopher Wileyda695992015-10-05 11:31:41 -0700228 StatementBlock statements_;
229 bool is_const_method_ = false;
230
231 DISALLOW_COPY_AND_ASSIGN(MethodImpl);
232}; // class MethodImpl
233
234class SwitchStatement : public AstNode {
235 public:
236 explicit SwitchStatement(const std::string& expression);
237 virtual ~SwitchStatement() = default;
238
239 // Add a case statement and return a pointer code block corresponding
240 // to the case. The switch statement will add a break statement
241 // after the code block by default to prevent accidental fall-through.
242 // Returns nullptr on duplicate value expressions (by strcmp, not value
243 // equivalence).
244 StatementBlock* AddCase(const std::string& value_expression);
245 void Write(CodeWriter* to) const override;
246
247 private:
248 const std::string switch_expression_;
249 std::vector<std::string> case_values_;
250 std::vector<std::unique_ptr<StatementBlock>> case_logic_;
251
252 DISALLOW_COPY_AND_ASSIGN(SwitchStatement);
253}; // class SwitchStatement
254
Christopher Wiley23285262015-10-09 15:06:14 -0700255class Assignment : public AstNode {
256 public:
257 Assignment(const std::string& left, const std::string& right);
258 Assignment(const std::string& left, AstNode* right);
259 ~Assignment() = default;
260 void Write(CodeWriter* to) const override;
261
262 private:
263 const std::string lhs_;
264 std::unique_ptr<AstNode> rhs_;
265
266 DISALLOW_COPY_AND_ASSIGN(Assignment);
267}; // class Assignment
268
269class MethodCall : public AstNode {
270 public:
271 MethodCall(const std::string& method_name,
272 const std::string& single_argument);
Christopher Wileyade4b452015-10-10 11:06:03 -0700273 MethodCall(const std::string& method_name, ArgList&& arg_list);
Christopher Wiley23285262015-10-09 15:06:14 -0700274 ~MethodCall() = default;
275 void Write(CodeWriter* to) const override;
276
277 private:
278 const std::string method_name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700279 const ArgList arguments_;
Christopher Wiley23285262015-10-09 15:06:14 -0700280
281 DISALLOW_COPY_AND_ASSIGN(MethodCall);
282}; // class MethodCall
283
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700284class IfStatement : public AstNode {
285 public:
286 IfStatement(AstNode* expression,
287 bool invert_expression = false);
288 virtual ~IfStatement() = default;
289 StatementBlock* OnTrue() { return &on_true_; }
290 StatementBlock* OnFalse() { return &on_false_; }
291 void Write(CodeWriter* to) const override;
292
293 private:
294 std::unique_ptr<AstNode> expression_;
295 bool invert_expression_ = false;
296 StatementBlock on_true_;
297 StatementBlock on_false_;
298
299 DISALLOW_COPY_AND_ASSIGN(IfStatement);
300}; // class IfStatement
301
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700302class Statement : public AstNode {
Christopher Wileyda695992015-10-05 11:31:41 -0700303 public:
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700304 explicit Statement(std::unique_ptr<AstNode> expression);
305 explicit Statement(AstNode* expression); // Takes possession.
306 explicit Statement(const std::string& expression);
307 ~Statement() = default;
Christopher Wileyda695992015-10-05 11:31:41 -0700308 void Write(CodeWriter* to) const override;
309
310 private:
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700311 std::unique_ptr<AstNode> expression_;
Christopher Wileyda695992015-10-05 11:31:41 -0700312
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700313 DISALLOW_COPY_AND_ASSIGN(Statement);
314}; // class Statement
Casey Dahlin88924d62015-09-17 16:28:24 -0700315
Christopher Wileyd55db282015-10-20 18:16:47 -0700316class Comparison : public AstNode {
317 public:
318 Comparison(AstNode* lhs, const std::string& comparison, AstNode* rhs);
319 ~Comparison() = default;
320 void Write(CodeWriter* to) const override;
321
322 private:
323 std::unique_ptr<AstNode> left_;
324 std::unique_ptr<AstNode> right_;
325 const std::string operator_;
326
327 DISALLOW_COPY_AND_ASSIGN(Comparison);
328}; // class Comparison
329
Christopher Wiley23285262015-10-09 15:06:14 -0700330class LiteralExpression : public AstNode {
331 public:
332 explicit LiteralExpression(const std::string& expression);
333 ~LiteralExpression() = default;
334 void Write(CodeWriter* to) const override;
335
336 private:
337 const std::string expression_;
338
339 DISALLOW_COPY_AND_ASSIGN(LiteralExpression);
340}; // class LiteralExpression
341
Christopher Wileyf944e792015-09-29 10:00:46 -0700342class CppNamespace : public Declaration {
Casey Dahlin34b86102015-09-16 16:03:06 -0700343 public:
344 CppNamespace(const std::string& name,
Christopher Wileyf944e792015-09-29 10:00:46 -0700345 std::vector<std::unique_ptr<Declaration>> declarations);
Christopher Wiley0c732db2015-09-29 14:36:44 -0700346 CppNamespace(const std::string& name,
347 std::unique_ptr<Declaration> declaration);
348 CppNamespace(const std::string& name);
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700349 virtual ~CppNamespace() = default;
Casey Dahlin34b86102015-09-16 16:03:06 -0700350
351 void Write(CodeWriter* to) const override;
352
353 private:
Christopher Wileyf944e792015-09-29 10:00:46 -0700354 std::vector<std::unique_ptr<Declaration>> declarations_;
Casey Dahlin34b86102015-09-16 16:03:06 -0700355 std::string name_;
356
357 DISALLOW_COPY_AND_ASSIGN(CppNamespace);
358}; // class CppNamespace
359
Christopher Wileyf944e792015-09-29 10:00:46 -0700360class Document : public AstNode {
Casey Dahlin34b86102015-09-16 16:03:06 -0700361 public:
Christopher Wileyf944e792015-09-29 10:00:46 -0700362 Document(const std::vector<std::string>& include_list,
363 std::unique_ptr<CppNamespace> a_namespace);
Casey Dahlin34b86102015-09-16 16:03:06 -0700364
365 void Write(CodeWriter* to) const override;
366
367 private:
368 std::vector<std::string> include_list_;
369 std::unique_ptr<CppNamespace> namespace_;
370
Christopher Wileyf944e792015-09-29 10:00:46 -0700371 DISALLOW_COPY_AND_ASSIGN(Document);
372}; // class Document
Casey Dahlin34b86102015-09-16 16:03:06 -0700373
Christopher Wileyf944e792015-09-29 10:00:46 -0700374class CppHeader final : public Document {
Christopher Wileyf600a552015-09-12 14:07:44 -0700375 public:
376 CppHeader(const std::string& include_guard,
377 const std::vector<std::string>& include_list,
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700378 std::unique_ptr<CppNamespace> a_namespace);
Casey Dahlin34b86102015-09-16 16:03:06 -0700379 void Write(CodeWriter* to) const override;
Christopher Wileyf600a552015-09-12 14:07:44 -0700380
381 private:
382 const std::string include_guard_;
Christopher Wileyf600a552015-09-12 14:07:44 -0700383
384 DISALLOW_COPY_AND_ASSIGN(CppHeader);
385}; // class CppHeader
386
Christopher Wileyf944e792015-09-29 10:00:46 -0700387class CppSource final : public Document {
Christopher Wileyf600a552015-09-12 14:07:44 -0700388 public:
Casey Dahlin34b86102015-09-16 16:03:06 -0700389 CppSource(const std::vector<std::string>& include_list,
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700390 std::unique_ptr<CppNamespace> a_namespace);
Christopher Wileyf600a552015-09-12 14:07:44 -0700391
392 private:
Casey Dahlin34b86102015-09-16 16:03:06 -0700393 DISALLOW_COPY_AND_ASSIGN(CppSource);
394}; // class CppSource
Christopher Wileyf600a552015-09-12 14:07:44 -0700395
Christopher Wileyf944e792015-09-29 10:00:46 -0700396} // namespace cpp
Christopher Wileyf600a552015-09-12 14:07:44 -0700397} // namespace aidl
398} // namespace android
399
Christopher Wileyf944e792015-09-29 10:00:46 -0700400#endif // AIDL_AST_CPP_H_