blob: cd16ed951c60704b6a2a6bdfb53cbb1148d8f682 [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
Jooyung Han14af79b2020-10-12 03:30:41 +090095 void AddValue(const std::string& key, const std::string& value, const std::string& comment = "");
Christopher Wileya7a5c102015-09-29 16:26:52 -070096
97 private:
98 struct EnumField {
Jooyung Han14af79b2020-10-12 03:30:41 +090099 EnumField(const std::string& k, const std::string& v, const std::string& c);
Christopher Wileya7a5c102015-09-29 16:26:52 -0700100 const std::string key;
101 const std::string value;
Jooyung Han14af79b2020-10-12 03:30:41 +0900102 const std::string comment;
Christopher Wileya7a5c102015-09-29 16:26:52 -0700103 };
104
105 std::string enum_name_;
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800106 std::string underlying_type_;
Daniel Norman85aed542019-08-21 12:01:14 -0700107 bool is_class_;
Christopher Wileya7a5c102015-09-29 16:26:52 -0700108 std::vector<EnumField> fields_;
Christopher Wileya7a5c102015-09-29 16:26:52 -0700109}; // class Enum
110
Christopher Wiley23285262015-10-09 15:06:14 -0700111class ArgList : public AstNode {
112 public:
Christopher Wileyade4b452015-10-10 11:06:03 -0700113 ArgList() = default;
Christopher Wiley23285262015-10-09 15:06:14 -0700114 explicit ArgList(const std::string& single_argument);
115 explicit ArgList(const std::vector<std::string>& arg_list);
Christopher Wileyf02facf2015-11-12 08:54:08 -0800116 explicit ArgList(std::vector<std::unique_ptr<AstNode>> arg_list);
Chih-Hung Hsiehf5cbb682018-09-25 13:43:32 -0700117 ArgList(ArgList&& arg_list) noexcept;
Christopher Wiley23285262015-10-09 15:06:14 -0700118 virtual ~ArgList() = default;
119
120 void Write(CodeWriter* to) const override;
121
122 private:
Christopher Wileyf02facf2015-11-12 08:54:08 -0800123 std::vector<std::unique_ptr<AstNode>> arguments_;
Christopher Wiley23285262015-10-09 15:06:14 -0700124}; // class ArgList
125
Christopher Wileyf944e792015-09-29 10:00:46 -0700126class ConstructorDecl : public Declaration {
Casey Dahlina834dd42015-09-23 11:52:15 -0700127 public:
Christopher Wileyb23149d2015-10-14 13:52:21 -0700128 enum Modifiers {
129 IS_VIRTUAL = 1 << 0,
130 IS_DEFAULT = 1 << 1,
131 IS_EXPLICIT = 1 << 2,
132 };
133
Christopher Wileyf944e792015-09-29 10:00:46 -0700134 ConstructorDecl(const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700135 ArgList&& arg_list);
Christopher Wileya7a5c102015-09-29 16:26:52 -0700136 ConstructorDecl(const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700137 ArgList&& arg_list,
Christopher Wileyb23149d2015-10-14 13:52:21 -0700138 uint32_t modifiers);
Casey Dahlina834dd42015-09-23 11:52:15 -0700139
Christopher Wileyf944e792015-09-29 10:00:46 -0700140 virtual ~ConstructorDecl() = default;
Casey Dahlina834dd42015-09-23 11:52:15 -0700141
142 void Write(CodeWriter* to) const override;
143
144 private:
145 const std::string name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700146 const ArgList arguments_;
Christopher Wileyb23149d2015-10-14 13:52:21 -0700147 const uint32_t modifiers_;
Christopher Wileyda695992015-10-05 11:31:41 -0700148}; // class ConstructorDecl
Casey Dahlina834dd42015-09-23 11:52:15 -0700149
Christopher Wiley11a9d792016-02-24 17:20:33 -0800150class MacroDecl : public Declaration {
151 public:
152 MacroDecl(const std::string& name, ArgList&& arg_list);
153 virtual ~MacroDecl() = default;
154
155 void Write(CodeWriter* to) const override;
156
157 private:
158 const std::string name_;
159 const ArgList arguments_;
Christopher Wiley11a9d792016-02-24 17:20:33 -0800160}; // class MacroDecl
161
Christopher Wileyf944e792015-09-29 10:00:46 -0700162class MethodDecl : public Declaration {
Casey Dahlin88924d62015-09-17 16:28:24 -0700163 public:
Christopher Wiley0c732db2015-09-29 14:36:44 -0700164 enum Modifiers {
165 IS_CONST = 1 << 0,
166 IS_VIRTUAL = 1 << 1,
167 IS_OVERRIDE = 1 << 2,
168 IS_PURE_VIRTUAL = 1 << 3,
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700169 IS_STATIC = 1 << 4,
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900170 IS_FINAL = 1 << 5,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700171 };
172
173 MethodDecl(const std::string& return_type,
174 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700175 ArgList&& arg_list);
Christopher Wileyf944e792015-09-29 10:00:46 -0700176 MethodDecl(const std::string& return_type,
177 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700178 ArgList&& arg_list,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700179 uint32_t modifiers);
Christopher Wileyf944e792015-09-29 10:00:46 -0700180 virtual ~MethodDecl() = default;
Casey Dahlin88924d62015-09-17 16:28:24 -0700181
182 void Write(CodeWriter* to) const override;
183
184 private:
185 const std::string return_type_;
186 const std::string name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700187 const ArgList arguments_;
Christopher Wiley0c732db2015-09-29 14:36:44 -0700188 bool is_const_ = false;
189 bool is_virtual_ = false;
190 bool is_override_ = false;
191 bool is_pure_virtual_ = false;
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700192 bool is_static_ = true;
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900193 bool is_final_ = false;
Christopher Wileyda695992015-10-05 11:31:41 -0700194}; // class MethodDecl
195
196class StatementBlock : public Declaration {
197 public:
198 StatementBlock() = default;
199 virtual ~StatementBlock() = default;
200
201 void AddStatement(std::unique_ptr<AstNode> statement);
Christopher Wiley23285262015-10-09 15:06:14 -0700202 void AddStatement(AstNode* statement); // Takes ownership
Christopher Wileyda695992015-10-05 11:31:41 -0700203 void AddLiteral(const std::string& expression, bool add_semicolon = true);
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700204 bool Empty() const { return statements_.empty(); }
Christopher Wileyda695992015-10-05 11:31:41 -0700205
206 void Write(CodeWriter* to) const override;
207
208 private:
209 std::vector<std::unique_ptr<AstNode>> statements_;
Christopher Wileyda695992015-10-05 11:31:41 -0700210}; // class StatementBlock
211
Christopher Wileyf9688b02015-10-08 17:17:50 -0700212class ConstructorImpl : public Declaration {
213 public:
214 ConstructorImpl(const std::string& class_name,
215 ArgList&& arg_list,
216 const std::vector<std::string>& initializer_list);
217 virtual ~ConstructorImpl() = default;
218
Steven Morelanda57d0a62019-07-30 09:41:14 -0700219 // ConstructorImpl retains ownership of the statement block.
220 StatementBlock* GetStatementBlock();
221
Christopher Wileyf9688b02015-10-08 17:17:50 -0700222 void Write(CodeWriter* to) const override;
223
224 private:
225 std::string class_name_;
226 ArgList arguments_;
227 std::vector<std::string> initializer_list_;
228 StatementBlock body_;
Christopher Wileyf9688b02015-10-08 17:17:50 -0700229}; // class ConstructorImpl
230
Christopher Wileyda695992015-10-05 11:31:41 -0700231class MethodImpl : public Declaration {
232 public:
233 // Passing an empty class name causes the method to be declared as a normal
234 // function (ie. no ClassName:: qualifier).
Devin Moore53fc99c2020-08-12 08:07:52 -0700235 MethodImpl(const std::string& return_type, const std::string& class_name,
236 const std::string& method_name, const std::vector<std::string>& template_params,
237 ArgList&& arg_list, bool is_const_method = false);
Christopher Wileyda695992015-10-05 11:31:41 -0700238 virtual ~MethodImpl() = default;
239
Christopher Wileyf9688b02015-10-08 17:17:50 -0700240 // MethodImpl retains ownership of the statement block.
241 StatementBlock* GetStatementBlock();
242
Christopher Wileyda695992015-10-05 11:31:41 -0700243 void Write(CodeWriter* to) const override;
244
245 private:
246 std::string return_type_;
247 std::string method_name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700248 const ArgList arguments_;
Christopher Wileyda695992015-10-05 11:31:41 -0700249 StatementBlock statements_;
250 bool is_const_method_ = false;
Devin Moore53fc99c2020-08-12 08:07:52 -0700251 std::vector<std::string> template_params_;
Christopher Wileyda695992015-10-05 11:31:41 -0700252}; // class MethodImpl
253
254class SwitchStatement : public AstNode {
255 public:
256 explicit SwitchStatement(const std::string& expression);
257 virtual ~SwitchStatement() = default;
258
259 // Add a case statement and return a pointer code block corresponding
260 // to the case. The switch statement will add a break statement
261 // after the code block by default to prevent accidental fall-through.
262 // Returns nullptr on duplicate value expressions (by strcmp, not value
263 // equivalence).
264 StatementBlock* AddCase(const std::string& value_expression);
265 void Write(CodeWriter* to) const override;
266
267 private:
268 const std::string switch_expression_;
269 std::vector<std::string> case_values_;
270 std::vector<std::unique_ptr<StatementBlock>> case_logic_;
Christopher Wileyda695992015-10-05 11:31:41 -0700271}; // class SwitchStatement
272
Christopher Wiley23285262015-10-09 15:06:14 -0700273class Assignment : public AstNode {
274 public:
275 Assignment(const std::string& left, const std::string& right);
276 Assignment(const std::string& left, AstNode* right);
277 ~Assignment() = default;
278 void Write(CodeWriter* to) const override;
279
280 private:
281 const std::string lhs_;
282 std::unique_ptr<AstNode> rhs_;
Christopher Wiley23285262015-10-09 15:06:14 -0700283}; // class Assignment
284
285class MethodCall : public AstNode {
286 public:
287 MethodCall(const std::string& method_name,
288 const std::string& single_argument);
Christopher Wileyade4b452015-10-10 11:06:03 -0700289 MethodCall(const std::string& method_name, ArgList&& arg_list);
Christopher Wiley23285262015-10-09 15:06:14 -0700290 ~MethodCall() = default;
291 void Write(CodeWriter* to) const override;
292
293 private:
294 const std::string method_name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700295 const ArgList arguments_;
Christopher Wiley23285262015-10-09 15:06:14 -0700296}; // class MethodCall
297
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700298class IfStatement : public AstNode {
299 public:
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -0700300 explicit IfStatement(AstNode* expression,
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700301 bool invert_expression = false);
302 virtual ~IfStatement() = default;
303 StatementBlock* OnTrue() { return &on_true_; }
304 StatementBlock* OnFalse() { return &on_false_; }
305 void Write(CodeWriter* to) const override;
306
307 private:
308 std::unique_ptr<AstNode> expression_;
309 bool invert_expression_ = false;
310 StatementBlock on_true_;
311 StatementBlock on_false_;
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700312}; // class IfStatement
313
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700314class Statement : public AstNode {
Christopher Wileyda695992015-10-05 11:31:41 -0700315 public:
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700316 explicit Statement(std::unique_ptr<AstNode> expression);
317 explicit Statement(AstNode* expression); // Takes possession.
318 explicit Statement(const std::string& expression);
319 ~Statement() = default;
Christopher Wileyda695992015-10-05 11:31:41 -0700320 void Write(CodeWriter* to) const override;
321
322 private:
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700323 std::unique_ptr<AstNode> expression_;
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700324}; // class Statement
Casey Dahlin88924d62015-09-17 16:28:24 -0700325
Christopher Wileyd55db282015-10-20 18:16:47 -0700326class Comparison : public AstNode {
327 public:
328 Comparison(AstNode* lhs, const std::string& comparison, AstNode* rhs);
329 ~Comparison() = default;
330 void Write(CodeWriter* to) const override;
331
332 private:
333 std::unique_ptr<AstNode> left_;
334 std::unique_ptr<AstNode> right_;
335 const std::string operator_;
Christopher Wileyd55db282015-10-20 18:16:47 -0700336}; // class Comparison
337
Christopher Wiley23285262015-10-09 15:06:14 -0700338class LiteralExpression : public AstNode {
339 public:
340 explicit LiteralExpression(const std::string& expression);
341 ~LiteralExpression() = default;
342 void Write(CodeWriter* to) const override;
343
344 private:
345 const std::string expression_;
Christopher Wiley23285262015-10-09 15:06:14 -0700346}; // class LiteralExpression
347
Christopher Wileyf944e792015-09-29 10:00:46 -0700348class CppNamespace : public Declaration {
Casey Dahlin34b86102015-09-16 16:03:06 -0700349 public:
350 CppNamespace(const std::string& name,
Christopher Wileyf944e792015-09-29 10:00:46 -0700351 std::vector<std::unique_ptr<Declaration>> declarations);
Christopher Wiley0c732db2015-09-29 14:36:44 -0700352 CppNamespace(const std::string& name,
353 std::unique_ptr<Declaration> declaration);
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -0700354 explicit CppNamespace(const std::string& name);
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700355 virtual ~CppNamespace() = default;
Casey Dahlin34b86102015-09-16 16:03:06 -0700356
357 void Write(CodeWriter* to) const override;
358
359 private:
Christopher Wileyf944e792015-09-29 10:00:46 -0700360 std::vector<std::unique_ptr<Declaration>> declarations_;
Casey Dahlin34b86102015-09-16 16:03:06 -0700361 std::string name_;
Casey Dahlin34b86102015-09-16 16:03:06 -0700362}; // class CppNamespace
363
Christopher Wileyf944e792015-09-29 10:00:46 -0700364class Document : public AstNode {
Casey Dahlin34b86102015-09-16 16:03:06 -0700365 public:
Christopher Wileyf944e792015-09-29 10:00:46 -0700366 Document(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700367 std::vector<std::unique_ptr<Declaration>> declarations);
Casey Dahlin34b86102015-09-16 16:03:06 -0700368
369 void Write(CodeWriter* to) const override;
370
371 private:
372 std::vector<std::string> include_list_;
Steven Morelandf3da0892018-10-05 14:52:01 -0700373 std::vector<std::unique_ptr<Declaration>> declarations_;
Christopher Wileyf944e792015-09-29 10:00:46 -0700374}; // class Document
Casey Dahlin34b86102015-09-16 16:03:06 -0700375
Christopher Wileyf944e792015-09-29 10:00:46 -0700376class CppHeader final : public Document {
Christopher Wileyf600a552015-09-12 14:07:44 -0700377 public:
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700378 CppHeader(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700379 std::vector<std::unique_ptr<Declaration>> declarations);
Casey Dahlin34b86102015-09-16 16:03:06 -0700380 void Write(CodeWriter* to) const override;
Christopher Wileyf600a552015-09-12 14:07:44 -0700381}; // class CppHeader
382
Christopher Wileyf944e792015-09-29 10:00:46 -0700383class CppSource final : public Document {
Christopher Wileyf600a552015-09-12 14:07:44 -0700384 public:
Casey Dahlin34b86102015-09-16 16:03:06 -0700385 CppSource(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700386 std::vector<std::unique_ptr<Declaration>> declarations);
Casey Dahlin34b86102015-09-16 16:03:06 -0700387}; // class CppSource
Christopher Wileyf600a552015-09-12 14:07:44 -0700388
Christopher Wileyf944e792015-09-29 10:00:46 -0700389} // namespace cpp
Christopher Wileyf600a552015-09-12 14:07:44 -0700390} // namespace aidl
391} // namespace android