blob: 9890f27d6ed57ab5b43e8ead57ab3df880b6c098 [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
Steven Moreland9fccf582018-08-27 20:36:27 -070017#pragma once
Christopher Wileyf600a552015-09-12 14:07:44 -070018
Casey Dahlin60a49162015-09-17 14:23:10 -070019#include <memory>
Christopher Wileyf600a552015-09-12 14:07:44 -070020#include <string>
21#include <vector>
22
Christopher Wileyf600a552015-09-12 14:07:44 -070023namespace android {
24namespace aidl {
Christopher Wileyf600a552015-09-12 14:07:44 -070025class CodeWriter;
Christopher Wileyf944e792015-09-29 10:00:46 -070026} // namespace aidl
27} // namespace android
Christopher Wileyf600a552015-09-12 14:07:44 -070028
Christopher Wileyf944e792015-09-29 10:00:46 -070029namespace android {
30namespace aidl {
31namespace cpp {
32
33class AstNode {
Casey Dahlin34b86102015-09-16 16:03:06 -070034 public:
Christopher Wileyf944e792015-09-29 10:00:46 -070035 AstNode() = default;
36 virtual ~AstNode() = default;
Jiyong Parkd800fef2020-07-22 18:09:43 +090037
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
Casey Dahlin34b86102015-09-16 16:03:06 -070044 virtual void Write(CodeWriter* to) const = 0;
Jiyong Park176905e2018-07-04 22:29:41 +090045 std::string ToString();
Christopher Wileyf944e792015-09-29 10:00:46 -070046}; // class AstNode
Casey Dahlin34b86102015-09-16 16:03:06 -070047
Christopher Wileyf944e792015-09-29 10:00:46 -070048class Declaration : public AstNode {
Casey Dahlin34b86102015-09-16 16:03:06 -070049 public:
Christopher Wileyf944e792015-09-29 10:00:46 -070050 Declaration() = default;
51 virtual ~Declaration() = default;
Christopher Wileyf944e792015-09-29 10:00:46 -070052}; // class Declaration
Casey Dahlin34b86102015-09-16 16:03:06 -070053
Steven Moreland5557f1c2018-07-02 13:50:23 -070054class 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_;
Steven Moreland5557f1c2018-07-02 13:50:23 -070062}; // class LiteralDecl
63
Christopher Wileyf944e792015-09-29 10:00:46 -070064class ClassDecl : public Declaration {
Casey Dahlin60a49162015-09-17 14:23:10 -070065 public:
Devin Moore53fc99c2020-08-12 08:07:52 -070066 ClassDecl(const std::string& name, const std::string& parent,
67 const std::vector<std::string>& template_params);
68 ClassDecl(const std::string& name, const std::string& parent,
69 const std::vector<std::string>& template_params,
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);
72 virtual ~ClassDecl() = default;
Casey Dahlin60a49162015-09-17 14:23:10 -070073
74 void Write(CodeWriter* to) const override;
75
Christopher Wiley0c732db2015-09-29 14:36:44 -070076 void AddPublic(std::unique_ptr<Declaration> member);
77 void AddPrivate(std::unique_ptr<Declaration> member);
78
Casey Dahlin60a49162015-09-17 14:23:10 -070079 private:
80 std::string name_;
81 std::string parent_;
Devin Moore53fc99c2020-08-12 08:07:52 -070082 std::vector<std::string> template_params_;
Christopher Wileyf944e792015-09-29 10:00:46 -070083 std::vector<std::unique_ptr<Declaration>> public_members_;
84 std::vector<std::unique_ptr<Declaration>> private_members_;
Christopher Wileyf944e792015-09-29 10:00:46 -070085}; // class ClassDecl
Casey Dahlin60a49162015-09-17 14:23:10 -070086
Christopher Wileya7a5c102015-09-29 16:26:52 -070087class Enum : public Declaration {
88 public:
Daniel Norman85aed542019-08-21 12:01:14 -070089 Enum(const std::string& name, const std::string& base_type, bool is_class);
Christopher Wileya7a5c102015-09-29 16:26:52 -070090 virtual ~Enum() = default;
91
Christopher Wileyfd7dc032016-02-02 17:58:39 -080092 bool HasValues() const { return !fields_.empty(); }
Christopher Wileya7a5c102015-09-29 16:26:52 -070093 void Write(CodeWriter* to) const override;
94
95 void AddValue(const std::string& key, const std::string& value);
96
97 private:
98 struct EnumField {
99 EnumField(const std::string& k, const std::string& v);
100 const std::string key;
101 const std::string value;
102 };
103
104 std::string enum_name_;
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800105 std::string underlying_type_;
Daniel Norman85aed542019-08-21 12:01:14 -0700106 bool is_class_;
Christopher Wileya7a5c102015-09-29 16:26:52 -0700107 std::vector<EnumField> fields_;
Christopher Wileya7a5c102015-09-29 16:26:52 -0700108}; // class Enum
109
Christopher Wiley23285262015-10-09 15:06:14 -0700110class ArgList : public AstNode {
111 public:
Christopher Wileyade4b452015-10-10 11:06:03 -0700112 ArgList() = default;
Christopher Wiley23285262015-10-09 15:06:14 -0700113 explicit ArgList(const std::string& single_argument);
114 explicit ArgList(const std::vector<std::string>& arg_list);
Christopher Wileyf02facf2015-11-12 08:54:08 -0800115 explicit ArgList(std::vector<std::unique_ptr<AstNode>> arg_list);
Chih-Hung Hsiehf5cbb682018-09-25 13:43:32 -0700116 ArgList(ArgList&& arg_list) noexcept;
Christopher Wiley23285262015-10-09 15:06:14 -0700117 virtual ~ArgList() = default;
118
119 void Write(CodeWriter* to) const override;
120
121 private:
Christopher Wileyf02facf2015-11-12 08:54:08 -0800122 std::vector<std::unique_ptr<AstNode>> arguments_;
Christopher Wiley23285262015-10-09 15:06:14 -0700123}; // class ArgList
124
Christopher Wileyf944e792015-09-29 10:00:46 -0700125class ConstructorDecl : public Declaration {
Casey Dahlina834dd42015-09-23 11:52:15 -0700126 public:
Christopher Wileyb23149d2015-10-14 13:52:21 -0700127 enum Modifiers {
128 IS_VIRTUAL = 1 << 0,
129 IS_DEFAULT = 1 << 1,
130 IS_EXPLICIT = 1 << 2,
131 };
132
Christopher Wileyf944e792015-09-29 10:00:46 -0700133 ConstructorDecl(const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700134 ArgList&& arg_list);
Christopher Wileya7a5c102015-09-29 16:26:52 -0700135 ConstructorDecl(const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700136 ArgList&& arg_list,
Christopher Wileyb23149d2015-10-14 13:52:21 -0700137 uint32_t modifiers);
Casey Dahlina834dd42015-09-23 11:52:15 -0700138
Christopher Wileyf944e792015-09-29 10:00:46 -0700139 virtual ~ConstructorDecl() = default;
Casey Dahlina834dd42015-09-23 11:52:15 -0700140
141 void Write(CodeWriter* to) const override;
142
143 private:
144 const std::string name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700145 const ArgList arguments_;
Christopher Wileyb23149d2015-10-14 13:52:21 -0700146 const uint32_t modifiers_;
Christopher Wileyda695992015-10-05 11:31:41 -0700147}; // class ConstructorDecl
Casey Dahlina834dd42015-09-23 11:52:15 -0700148
Christopher Wiley11a9d792016-02-24 17:20:33 -0800149class MacroDecl : public Declaration {
150 public:
151 MacroDecl(const std::string& name, ArgList&& arg_list);
152 virtual ~MacroDecl() = default;
153
154 void Write(CodeWriter* to) const override;
155
156 private:
157 const std::string name_;
158 const ArgList arguments_;
Christopher Wiley11a9d792016-02-24 17:20:33 -0800159}; // class MacroDecl
160
Christopher Wileyf944e792015-09-29 10:00:46 -0700161class MethodDecl : public Declaration {
Casey Dahlin88924d62015-09-17 16:28:24 -0700162 public:
Christopher Wiley0c732db2015-09-29 14:36:44 -0700163 enum Modifiers {
164 IS_CONST = 1 << 0,
165 IS_VIRTUAL = 1 << 1,
166 IS_OVERRIDE = 1 << 2,
167 IS_PURE_VIRTUAL = 1 << 3,
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700168 IS_STATIC = 1 << 4,
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900169 IS_FINAL = 1 << 5,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700170 };
171
172 MethodDecl(const std::string& return_type,
173 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700174 ArgList&& arg_list);
Christopher Wileyf944e792015-09-29 10:00:46 -0700175 MethodDecl(const std::string& return_type,
176 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700177 ArgList&& arg_list,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700178 uint32_t modifiers);
Christopher Wileyf944e792015-09-29 10:00:46 -0700179 virtual ~MethodDecl() = default;
Casey Dahlin88924d62015-09-17 16:28:24 -0700180
181 void Write(CodeWriter* to) const override;
182
183 private:
184 const std::string return_type_;
185 const std::string name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700186 const ArgList arguments_;
Christopher Wiley0c732db2015-09-29 14:36:44 -0700187 bool is_const_ = false;
188 bool is_virtual_ = false;
189 bool is_override_ = false;
190 bool is_pure_virtual_ = false;
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700191 bool is_static_ = true;
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900192 bool is_final_ = false;
Christopher Wileyda695992015-10-05 11:31:41 -0700193}; // class MethodDecl
194
195class StatementBlock : public Declaration {
196 public:
197 StatementBlock() = default;
198 virtual ~StatementBlock() = default;
199
200 void AddStatement(std::unique_ptr<AstNode> statement);
Christopher Wiley23285262015-10-09 15:06:14 -0700201 void AddStatement(AstNode* statement); // Takes ownership
Christopher Wileyda695992015-10-05 11:31:41 -0700202 void AddLiteral(const std::string& expression, bool add_semicolon = true);
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700203 bool Empty() const { return statements_.empty(); }
Christopher Wileyda695992015-10-05 11:31:41 -0700204
205 void Write(CodeWriter* to) const override;
206
207 private:
208 std::vector<std::unique_ptr<AstNode>> statements_;
Christopher Wileyda695992015-10-05 11:31:41 -0700209}; // class StatementBlock
210
Christopher Wileyf9688b02015-10-08 17:17:50 -0700211class ConstructorImpl : public Declaration {
212 public:
213 ConstructorImpl(const std::string& class_name,
214 ArgList&& arg_list,
215 const std::vector<std::string>& initializer_list);
216 virtual ~ConstructorImpl() = default;
217
Steven Morelanda57d0a62019-07-30 09:41:14 -0700218 // ConstructorImpl retains ownership of the statement block.
219 StatementBlock* GetStatementBlock();
220
Christopher Wileyf9688b02015-10-08 17:17:50 -0700221 void Write(CodeWriter* to) const override;
222
223 private:
224 std::string class_name_;
225 ArgList arguments_;
226 std::vector<std::string> initializer_list_;
227 StatementBlock body_;
Christopher Wileyf9688b02015-10-08 17:17:50 -0700228}; // class ConstructorImpl
229
Christopher Wileyda695992015-10-05 11:31:41 -0700230class MethodImpl : public Declaration {
231 public:
232 // Passing an empty class name causes the method to be declared as a normal
233 // function (ie. no ClassName:: qualifier).
Devin Moore53fc99c2020-08-12 08:07:52 -0700234 MethodImpl(const std::string& return_type, const std::string& class_name,
235 const std::string& method_name, const std::vector<std::string>& template_params,
236 ArgList&& arg_list, bool is_const_method = false);
Christopher Wileyda695992015-10-05 11:31:41 -0700237 virtual ~MethodImpl() = default;
238
Christopher Wileyf9688b02015-10-08 17:17:50 -0700239 // MethodImpl retains ownership of the statement block.
240 StatementBlock* GetStatementBlock();
241
Christopher Wileyda695992015-10-05 11:31:41 -0700242 void Write(CodeWriter* to) const override;
243
244 private:
245 std::string return_type_;
246 std::string method_name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700247 const ArgList arguments_;
Christopher Wileyda695992015-10-05 11:31:41 -0700248 StatementBlock statements_;
249 bool is_const_method_ = false;
Devin Moore53fc99c2020-08-12 08:07:52 -0700250 std::vector<std::string> template_params_;
Christopher Wileyda695992015-10-05 11:31:41 -0700251}; // class MethodImpl
252
253class 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_;
Christopher Wileyda695992015-10-05 11:31:41 -0700270}; // class SwitchStatement
271
Christopher Wiley23285262015-10-09 15:06:14 -0700272class 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_;
Christopher Wiley23285262015-10-09 15:06:14 -0700282}; // class Assignment
283
284class MethodCall : public AstNode {
285 public:
286 MethodCall(const std::string& method_name,
287 const std::string& single_argument);
Christopher Wileyade4b452015-10-10 11:06:03 -0700288 MethodCall(const std::string& method_name, ArgList&& arg_list);
Christopher Wiley23285262015-10-09 15:06:14 -0700289 ~MethodCall() = default;
290 void Write(CodeWriter* to) const override;
291
292 private:
293 const std::string method_name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700294 const ArgList arguments_;
Christopher Wiley23285262015-10-09 15:06:14 -0700295}; // class MethodCall
296
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700297class IfStatement : public AstNode {
298 public:
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -0700299 explicit IfStatement(AstNode* expression,
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700300 bool invert_expression = false);
301 virtual ~IfStatement() = default;
302 StatementBlock* OnTrue() { return &on_true_; }
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_;
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700311}; // class IfStatement
312
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700313class Statement : public AstNode {
Christopher Wileyda695992015-10-05 11:31:41 -0700314 public:
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700315 explicit Statement(std::unique_ptr<AstNode> expression);
316 explicit Statement(AstNode* expression); // Takes possession.
317 explicit Statement(const std::string& expression);
318 ~Statement() = default;
Christopher Wileyda695992015-10-05 11:31:41 -0700319 void Write(CodeWriter* to) const override;
320
321 private:
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700322 std::unique_ptr<AstNode> expression_;
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700323}; // class Statement
Casey Dahlin88924d62015-09-17 16:28:24 -0700324
Christopher Wileyd55db282015-10-20 18:16:47 -0700325class 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_;
Christopher Wileyd55db282015-10-20 18:16:47 -0700335}; // class Comparison
336
Christopher Wiley23285262015-10-09 15:06:14 -0700337class 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_;
Christopher Wiley23285262015-10-09 15:06:14 -0700345}; // class LiteralExpression
346
Christopher Wileyf944e792015-09-29 10:00:46 -0700347class CppNamespace : public Declaration {
Casey Dahlin34b86102015-09-16 16:03:06 -0700348 public:
349 CppNamespace(const std::string& name,
Christopher Wileyf944e792015-09-29 10:00:46 -0700350 std::vector<std::unique_ptr<Declaration>> declarations);
Christopher Wiley0c732db2015-09-29 14:36:44 -0700351 CppNamespace(const std::string& name,
352 std::unique_ptr<Declaration> declaration);
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -0700353 explicit CppNamespace(const std::string& name);
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700354 virtual ~CppNamespace() = default;
Casey Dahlin34b86102015-09-16 16:03:06 -0700355
356 void Write(CodeWriter* to) const override;
357
358 private:
Christopher Wileyf944e792015-09-29 10:00:46 -0700359 std::vector<std::unique_ptr<Declaration>> declarations_;
Casey Dahlin34b86102015-09-16 16:03:06 -0700360 std::string name_;
Casey Dahlin34b86102015-09-16 16:03:06 -0700361}; // class CppNamespace
362
Christopher Wileyf944e792015-09-29 10:00:46 -0700363class Document : public AstNode {
Casey Dahlin34b86102015-09-16 16:03:06 -0700364 public:
Christopher Wileyf944e792015-09-29 10:00:46 -0700365 Document(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700366 std::vector<std::unique_ptr<Declaration>> declarations);
Casey Dahlin34b86102015-09-16 16:03:06 -0700367
368 void Write(CodeWriter* to) const override;
369
370 private:
371 std::vector<std::string> include_list_;
Steven Morelandf3da0892018-10-05 14:52:01 -0700372 std::vector<std::unique_ptr<Declaration>> declarations_;
Christopher Wileyf944e792015-09-29 10:00:46 -0700373}; // class Document
Casey Dahlin34b86102015-09-16 16:03:06 -0700374
Christopher Wileyf944e792015-09-29 10:00:46 -0700375class CppHeader final : public Document {
Christopher Wileyf600a552015-09-12 14:07:44 -0700376 public:
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700377 CppHeader(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700378 std::vector<std::unique_ptr<Declaration>> declarations);
Casey Dahlin34b86102015-09-16 16:03:06 -0700379 void Write(CodeWriter* to) const override;
Christopher Wileyf600a552015-09-12 14:07:44 -0700380}; // class CppHeader
381
Christopher Wileyf944e792015-09-29 10:00:46 -0700382class CppSource final : public Document {
Christopher Wileyf600a552015-09-12 14:07:44 -0700383 public:
Casey Dahlin34b86102015-09-16 16:03:06 -0700384 CppSource(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700385 std::vector<std::unique_ptr<Declaration>> declarations);
Casey Dahlin34b86102015-09-16 16:03:06 -0700386}; // class CppSource
Christopher Wileyf600a552015-09-12 14:07:44 -0700387
Christopher Wileyf944e792015-09-29 10:00:46 -0700388} // namespace cpp
Christopher Wileyf600a552015-09-12 14:07:44 -0700389} // namespace aidl
390} // namespace android