blob: 9f9414aed7d06222f353f5c5f74633a8b3db23d9 [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
Elliott Hughes0a620672015-12-04 13:53:18 -080023#include <android-base/macros.h>
Christopher Wileyf600a552015-09-12 14:07:44 -070024
25namespace android {
26namespace aidl {
Christopher Wileyf600a552015-09-12 14:07:44 -070027class CodeWriter;
Christopher Wileyf944e792015-09-29 10:00:46 -070028} // namespace aidl
29} // namespace android
Christopher Wileyf600a552015-09-12 14:07:44 -070030
Christopher Wileyf944e792015-09-29 10:00:46 -070031namespace android {
32namespace aidl {
33namespace cpp {
34
35class AstNode {
Casey Dahlin34b86102015-09-16 16:03:06 -070036 public:
Christopher Wileyf944e792015-09-29 10:00:46 -070037 AstNode() = default;
38 virtual ~AstNode() = default;
Casey Dahlin34b86102015-09-16 16:03:06 -070039 virtual void Write(CodeWriter* to) const = 0;
Jiyong Park176905e2018-07-04 22:29:41 +090040 std::string ToString();
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
Steven Moreland5557f1c2018-07-02 13:50:23 -070052class LiteralDecl : public Declaration {
53 public:
54 explicit LiteralDecl(const std::string& expression);
55 ~LiteralDecl() = default;
56 void Write(CodeWriter* to) const override;
57
58 private:
59 const std::string expression_;
60
61 DISALLOW_COPY_AND_ASSIGN(LiteralDecl);
62}; // class LiteralDecl
63
Christopher Wileyf944e792015-09-29 10:00:46 -070064class ClassDecl : public Declaration {
Casey Dahlin60a49162015-09-17 14:23:10 -070065 public:
Christopher Wileyf944e792015-09-29 10:00:46 -070066 ClassDecl(const std::string& name,
Christopher Wiley0c732db2015-09-29 14:36:44 -070067 const std::string& parent);
68 ClassDecl(const std::string& name,
Christopher Wileyf944e792015-09-29 10:00:46 -070069 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;
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_;
Christopher Wileyf944e792015-09-29 10:00:46 -070082 std::vector<std::unique_ptr<Declaration>> public_members_;
83 std::vector<std::unique_ptr<Declaration>> private_members_;
Casey Dahlin60a49162015-09-17 14:23:10 -070084
Christopher Wileyf944e792015-09-29 10:00:46 -070085 DISALLOW_COPY_AND_ASSIGN(ClassDecl);
86}; // class ClassDecl
Casey Dahlin60a49162015-09-17 14:23:10 -070087
Christopher Wileya7a5c102015-09-29 16:26:52 -070088class Enum : public Declaration {
89 public:
Christopher Wileyfd7dc032016-02-02 17:58:39 -080090 Enum(const std::string& name, const std::string& base_type);
Christopher Wiley23285262015-10-09 15:06:14 -070091 explicit Enum(const std::string& name);
Christopher Wileya7a5c102015-09-29 16:26:52 -070092 virtual ~Enum() = default;
93
Christopher Wileyfd7dc032016-02-02 17:58:39 -080094 bool HasValues() const { return !fields_.empty(); }
Christopher Wileya7a5c102015-09-29 16:26:52 -070095 void Write(CodeWriter* to) const override;
96
97 void AddValue(const std::string& key, const std::string& value);
98
99 private:
100 struct EnumField {
101 EnumField(const std::string& k, const std::string& v);
102 const std::string key;
103 const std::string value;
104 };
105
106 std::string enum_name_;
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800107 std::string underlying_type_;
Christopher Wileya7a5c102015-09-29 16:26:52 -0700108 std::vector<EnumField> fields_;
109
110 DISALLOW_COPY_AND_ASSIGN(Enum);
111}; // class Enum
112
Christopher Wiley23285262015-10-09 15:06:14 -0700113class ArgList : public AstNode {
114 public:
Christopher Wileyade4b452015-10-10 11:06:03 -0700115 ArgList() = default;
Christopher Wiley23285262015-10-09 15:06:14 -0700116 explicit ArgList(const std::string& single_argument);
117 explicit ArgList(const std::vector<std::string>& arg_list);
Christopher Wileyf02facf2015-11-12 08:54:08 -0800118 explicit ArgList(std::vector<std::unique_ptr<AstNode>> arg_list);
Chih-Hung Hsiehf5cbb682018-09-25 13:43:32 -0700119 ArgList(ArgList&& arg_list) noexcept;
Christopher Wiley23285262015-10-09 15:06:14 -0700120 virtual ~ArgList() = default;
121
122 void Write(CodeWriter* to) const override;
123
124 private:
Christopher Wileyf02facf2015-11-12 08:54:08 -0800125 std::vector<std::unique_ptr<AstNode>> arguments_;
Christopher Wiley23285262015-10-09 15:06:14 -0700126
127 DISALLOW_COPY_AND_ASSIGN(ArgList);
128}; // class ArgList
129
Christopher Wileyf944e792015-09-29 10:00:46 -0700130class ConstructorDecl : public Declaration {
Casey Dahlina834dd42015-09-23 11:52:15 -0700131 public:
Christopher Wileyb23149d2015-10-14 13:52:21 -0700132 enum Modifiers {
133 IS_VIRTUAL = 1 << 0,
134 IS_DEFAULT = 1 << 1,
135 IS_EXPLICIT = 1 << 2,
136 };
137
Christopher Wileyf944e792015-09-29 10:00:46 -0700138 ConstructorDecl(const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700139 ArgList&& arg_list);
Christopher Wileya7a5c102015-09-29 16:26:52 -0700140 ConstructorDecl(const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700141 ArgList&& arg_list,
Christopher Wileyb23149d2015-10-14 13:52:21 -0700142 uint32_t modifiers);
Casey Dahlina834dd42015-09-23 11:52:15 -0700143
Christopher Wileyf944e792015-09-29 10:00:46 -0700144 virtual ~ConstructorDecl() = default;
Casey Dahlina834dd42015-09-23 11:52:15 -0700145
146 void Write(CodeWriter* to) const override;
147
148 private:
149 const std::string name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700150 const ArgList arguments_;
Christopher Wileyb23149d2015-10-14 13:52:21 -0700151 const uint32_t modifiers_;
Casey Dahlina834dd42015-09-23 11:52:15 -0700152
Christopher Wileyf944e792015-09-29 10:00:46 -0700153 DISALLOW_COPY_AND_ASSIGN(ConstructorDecl);
Christopher Wileyda695992015-10-05 11:31:41 -0700154}; // class ConstructorDecl
Casey Dahlina834dd42015-09-23 11:52:15 -0700155
Christopher Wiley11a9d792016-02-24 17:20:33 -0800156class MacroDecl : public Declaration {
157 public:
158 MacroDecl(const std::string& name, ArgList&& arg_list);
159 virtual ~MacroDecl() = default;
160
161 void Write(CodeWriter* to) const override;
162
163 private:
164 const std::string name_;
165 const ArgList arguments_;
166
167 DISALLOW_COPY_AND_ASSIGN(MacroDecl);
168}; // class MacroDecl
169
Christopher Wileyf944e792015-09-29 10:00:46 -0700170class MethodDecl : public Declaration {
Casey Dahlin88924d62015-09-17 16:28:24 -0700171 public:
Christopher Wiley0c732db2015-09-29 14:36:44 -0700172 enum Modifiers {
173 IS_CONST = 1 << 0,
174 IS_VIRTUAL = 1 << 1,
175 IS_OVERRIDE = 1 << 2,
176 IS_PURE_VIRTUAL = 1 << 3,
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700177 IS_STATIC = 1 << 4,
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900178 IS_FINAL = 1 << 5,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700179 };
180
181 MethodDecl(const std::string& return_type,
182 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700183 ArgList&& arg_list);
Christopher Wileyf944e792015-09-29 10:00:46 -0700184 MethodDecl(const std::string& return_type,
185 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700186 ArgList&& arg_list,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700187 uint32_t modifiers);
Christopher Wileyf944e792015-09-29 10:00:46 -0700188 virtual ~MethodDecl() = default;
Casey Dahlin88924d62015-09-17 16:28:24 -0700189
190 void Write(CodeWriter* to) const override;
191
192 private:
193 const std::string return_type_;
194 const std::string name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700195 const ArgList arguments_;
Christopher Wiley0c732db2015-09-29 14:36:44 -0700196 bool is_const_ = false;
197 bool is_virtual_ = false;
198 bool is_override_ = false;
199 bool is_pure_virtual_ = false;
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700200 bool is_static_ = true;
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900201 bool is_final_ = false;
Casey Dahlin88924d62015-09-17 16:28:24 -0700202
Christopher Wileyf944e792015-09-29 10:00:46 -0700203 DISALLOW_COPY_AND_ASSIGN(MethodDecl);
Christopher Wileyda695992015-10-05 11:31:41 -0700204}; // class MethodDecl
205
206class StatementBlock : public Declaration {
207 public:
208 StatementBlock() = default;
209 virtual ~StatementBlock() = default;
210
211 void AddStatement(std::unique_ptr<AstNode> statement);
Christopher Wiley23285262015-10-09 15:06:14 -0700212 void AddStatement(AstNode* statement); // Takes ownership
Christopher Wileyda695992015-10-05 11:31:41 -0700213 void AddLiteral(const std::string& expression, bool add_semicolon = true);
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700214 bool Empty() const { return statements_.empty(); }
Christopher Wileyda695992015-10-05 11:31:41 -0700215
216 void Write(CodeWriter* to) const override;
217
218 private:
219 std::vector<std::unique_ptr<AstNode>> statements_;
220
221 DISALLOW_COPY_AND_ASSIGN(StatementBlock);
222}; // class StatementBlock
223
Christopher Wileyf9688b02015-10-08 17:17:50 -0700224class ConstructorImpl : public Declaration {
225 public:
226 ConstructorImpl(const std::string& class_name,
227 ArgList&& arg_list,
228 const std::vector<std::string>& initializer_list);
229 virtual ~ConstructorImpl() = default;
230
231 void Write(CodeWriter* to) const override;
232
233 private:
234 std::string class_name_;
235 ArgList arguments_;
236 std::vector<std::string> initializer_list_;
237 StatementBlock body_;
238
239 DISALLOW_COPY_AND_ASSIGN(ConstructorImpl);
240}; // class ConstructorImpl
241
Christopher Wileyda695992015-10-05 11:31:41 -0700242class MethodImpl : public Declaration {
243 public:
244 // Passing an empty class name causes the method to be declared as a normal
245 // function (ie. no ClassName:: qualifier).
246 MethodImpl(const std::string& return_type,
247 const std::string& class_name,
248 const std::string& method_name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700249 ArgList&& arg_list,
Christopher Wileyda695992015-10-05 11:31:41 -0700250 bool is_const_method = false);
251 virtual ~MethodImpl() = default;
252
Christopher Wileyf9688b02015-10-08 17:17:50 -0700253 // MethodImpl retains ownership of the statement block.
254 StatementBlock* GetStatementBlock();
255
Christopher Wileyda695992015-10-05 11:31:41 -0700256 void Write(CodeWriter* to) const override;
257
258 private:
259 std::string return_type_;
260 std::string method_name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700261 const ArgList arguments_;
Christopher Wileyda695992015-10-05 11:31:41 -0700262 StatementBlock statements_;
263 bool is_const_method_ = false;
264
265 DISALLOW_COPY_AND_ASSIGN(MethodImpl);
266}; // class MethodImpl
267
268class SwitchStatement : public AstNode {
269 public:
270 explicit SwitchStatement(const std::string& expression);
271 virtual ~SwitchStatement() = default;
272
273 // Add a case statement and return a pointer code block corresponding
274 // to the case. The switch statement will add a break statement
275 // after the code block by default to prevent accidental fall-through.
276 // Returns nullptr on duplicate value expressions (by strcmp, not value
277 // equivalence).
278 StatementBlock* AddCase(const std::string& value_expression);
279 void Write(CodeWriter* to) const override;
280
281 private:
282 const std::string switch_expression_;
283 std::vector<std::string> case_values_;
284 std::vector<std::unique_ptr<StatementBlock>> case_logic_;
285
286 DISALLOW_COPY_AND_ASSIGN(SwitchStatement);
287}; // class SwitchStatement
288
Christopher Wiley23285262015-10-09 15:06:14 -0700289class Assignment : public AstNode {
290 public:
291 Assignment(const std::string& left, const std::string& right);
292 Assignment(const std::string& left, AstNode* right);
293 ~Assignment() = default;
294 void Write(CodeWriter* to) const override;
295
296 private:
297 const std::string lhs_;
298 std::unique_ptr<AstNode> rhs_;
299
300 DISALLOW_COPY_AND_ASSIGN(Assignment);
301}; // class Assignment
302
303class MethodCall : public AstNode {
304 public:
305 MethodCall(const std::string& method_name,
306 const std::string& single_argument);
Christopher Wileyade4b452015-10-10 11:06:03 -0700307 MethodCall(const std::string& method_name, ArgList&& arg_list);
Christopher Wiley23285262015-10-09 15:06:14 -0700308 ~MethodCall() = default;
309 void Write(CodeWriter* to) const override;
310
311 private:
312 const std::string method_name_;
Christopher Wileyade4b452015-10-10 11:06:03 -0700313 const ArgList arguments_;
Christopher Wiley23285262015-10-09 15:06:14 -0700314
315 DISALLOW_COPY_AND_ASSIGN(MethodCall);
316}; // class MethodCall
317
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700318class IfStatement : public AstNode {
319 public:
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -0700320 explicit IfStatement(AstNode* expression,
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700321 bool invert_expression = false);
322 virtual ~IfStatement() = default;
323 StatementBlock* OnTrue() { return &on_true_; }
324 StatementBlock* OnFalse() { return &on_false_; }
325 void Write(CodeWriter* to) const override;
326
327 private:
328 std::unique_ptr<AstNode> expression_;
329 bool invert_expression_ = false;
330 StatementBlock on_true_;
331 StatementBlock on_false_;
332
333 DISALLOW_COPY_AND_ASSIGN(IfStatement);
334}; // class IfStatement
335
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700336class Statement : public AstNode {
Christopher Wileyda695992015-10-05 11:31:41 -0700337 public:
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700338 explicit Statement(std::unique_ptr<AstNode> expression);
339 explicit Statement(AstNode* expression); // Takes possession.
340 explicit Statement(const std::string& expression);
341 ~Statement() = default;
Christopher Wileyda695992015-10-05 11:31:41 -0700342 void Write(CodeWriter* to) const override;
343
344 private:
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700345 std::unique_ptr<AstNode> expression_;
Christopher Wileyda695992015-10-05 11:31:41 -0700346
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700347 DISALLOW_COPY_AND_ASSIGN(Statement);
348}; // class Statement
Casey Dahlin88924d62015-09-17 16:28:24 -0700349
Christopher Wileyd55db282015-10-20 18:16:47 -0700350class Comparison : public AstNode {
351 public:
352 Comparison(AstNode* lhs, const std::string& comparison, AstNode* rhs);
353 ~Comparison() = default;
354 void Write(CodeWriter* to) const override;
355
356 private:
357 std::unique_ptr<AstNode> left_;
358 std::unique_ptr<AstNode> right_;
359 const std::string operator_;
360
361 DISALLOW_COPY_AND_ASSIGN(Comparison);
362}; // class Comparison
363
Christopher Wiley23285262015-10-09 15:06:14 -0700364class LiteralExpression : public AstNode {
365 public:
366 explicit LiteralExpression(const std::string& expression);
367 ~LiteralExpression() = default;
368 void Write(CodeWriter* to) const override;
369
370 private:
371 const std::string expression_;
372
373 DISALLOW_COPY_AND_ASSIGN(LiteralExpression);
374}; // class LiteralExpression
375
Christopher Wileyf944e792015-09-29 10:00:46 -0700376class CppNamespace : public Declaration {
Casey Dahlin34b86102015-09-16 16:03:06 -0700377 public:
378 CppNamespace(const std::string& name,
Christopher Wileyf944e792015-09-29 10:00:46 -0700379 std::vector<std::unique_ptr<Declaration>> declarations);
Christopher Wiley0c732db2015-09-29 14:36:44 -0700380 CppNamespace(const std::string& name,
381 std::unique_ptr<Declaration> declaration);
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -0700382 explicit CppNamespace(const std::string& name);
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700383 virtual ~CppNamespace() = default;
Casey Dahlin34b86102015-09-16 16:03:06 -0700384
385 void Write(CodeWriter* to) const override;
386
387 private:
Christopher Wileyf944e792015-09-29 10:00:46 -0700388 std::vector<std::unique_ptr<Declaration>> declarations_;
Casey Dahlin34b86102015-09-16 16:03:06 -0700389 std::string name_;
390
391 DISALLOW_COPY_AND_ASSIGN(CppNamespace);
392}; // class CppNamespace
393
Christopher Wileyf944e792015-09-29 10:00:46 -0700394class Document : public AstNode {
Casey Dahlin34b86102015-09-16 16:03:06 -0700395 public:
Christopher Wileyf944e792015-09-29 10:00:46 -0700396 Document(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700397 std::vector<std::unique_ptr<Declaration>> declarations);
Casey Dahlin34b86102015-09-16 16:03:06 -0700398
399 void Write(CodeWriter* to) const override;
400
401 private:
402 std::vector<std::string> include_list_;
Steven Morelandf3da0892018-10-05 14:52:01 -0700403 std::vector<std::unique_ptr<Declaration>> declarations_;
Casey Dahlin34b86102015-09-16 16:03:06 -0700404
Christopher Wileyf944e792015-09-29 10:00:46 -0700405 DISALLOW_COPY_AND_ASSIGN(Document);
406}; // class Document
Casey Dahlin34b86102015-09-16 16:03:06 -0700407
Christopher Wileyf944e792015-09-29 10:00:46 -0700408class CppHeader final : public Document {
Christopher Wileyf600a552015-09-12 14:07:44 -0700409 public:
Steven Morelandf3da0892018-10-05 14:52:01 -0700410 CppHeader(const std::string& include_guard, const std::vector<std::string>& include_list,
411 std::vector<std::unique_ptr<Declaration>> declarations);
Casey Dahlin34b86102015-09-16 16:03:06 -0700412 void Write(CodeWriter* to) const override;
Christopher Wileyf600a552015-09-12 14:07:44 -0700413
414 private:
415 const std::string include_guard_;
Christopher Wileyf600a552015-09-12 14:07:44 -0700416
417 DISALLOW_COPY_AND_ASSIGN(CppHeader);
418}; // class CppHeader
419
Christopher Wileyf944e792015-09-29 10:00:46 -0700420class CppSource final : public Document {
Christopher Wileyf600a552015-09-12 14:07:44 -0700421 public:
Casey Dahlin34b86102015-09-16 16:03:06 -0700422 CppSource(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700423 std::vector<std::unique_ptr<Declaration>> declarations);
Christopher Wileyf600a552015-09-12 14:07:44 -0700424
425 private:
Casey Dahlin34b86102015-09-16 16:03:06 -0700426 DISALLOW_COPY_AND_ASSIGN(CppSource);
427}; // class CppSource
Christopher Wileyf600a552015-09-12 14:07:44 -0700428
Christopher Wileyf944e792015-09-29 10:00:46 -0700429} // namespace cpp
Christopher Wileyf600a552015-09-12 14:07:44 -0700430} // namespace aidl
431} // namespace android