1f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley/* 2f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * Copyright (C) 2015, The Android Open Source Project 3f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * 4f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * Licensed under the Apache License, Version 2.0 (the "License"); 5f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * you may not use this file except in compliance with the License. 6f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * You may obtain a copy of the License at 7f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * 8f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * http://www.apache.org/licenses/LICENSE-2.0 9f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * 10f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * Unless required by applicable law or agreed to in writing, software 11f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * distributed under the License is distributed on an "AS IS" BASIS, 12f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * See the License for the specific language governing permissions and 14f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley * limitations under the License. 15f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley */ 16f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley 17f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley#ifndef AIDL_AST_CPP_H_ 18f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley#define AIDL_AST_CPP_H_ 19f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley 2060a4916eea290a9a62e1f867d1bd9813df5f37fcCasey Dahlin#include <memory> 21f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley#include <string> 22f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley#include <vector> 23f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley 240a62067f35e957493bc37c4b42dfdcfc16353831Elliott Hughes#include <android-base/macros.h> 25f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley 26f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wileynamespace android { 27f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wileynamespace aidl { 28f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wileyclass CodeWriter; 29f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley} // namespace aidl 30f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley} // namespace android 31f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley 32f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wileynamespace android { 33f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wileynamespace aidl { 34f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wileynamespace cpp { 35f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley 36f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wileyclass AstNode { 37f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley public: 38f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley AstNode() = default; 39f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley virtual ~AstNode() = default; 4034b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin virtual void Write(CodeWriter* to) const = 0; 41f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley}; // class AstNode 42f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley 43f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wileyclass Declaration : public AstNode { 4434b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin public: 45f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley Declaration() = default; 46f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley virtual ~Declaration() = default; 4734b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin 4834b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin private: 49f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley DISALLOW_COPY_AND_ASSIGN(Declaration); 50f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley}; // class Declaration 5134b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin 52f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wileyclass ClassDecl : public Declaration { 5360a4916eea290a9a62e1f867d1bd9813df5f37fcCasey Dahlin public: 54f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley ClassDecl(const std::string& name, 550c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley const std::string& parent); 560c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley ClassDecl(const std::string& name, 57f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley const std::string& parent, 58f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley std::vector<std::unique_ptr<Declaration>> public_members, 59f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley std::vector<std::unique_ptr<Declaration>> private_members); 60f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley virtual ~ClassDecl() = default; 6160a4916eea290a9a62e1f867d1bd9813df5f37fcCasey Dahlin 6260a4916eea290a9a62e1f867d1bd9813df5f37fcCasey Dahlin void Write(CodeWriter* to) const override; 6360a4916eea290a9a62e1f867d1bd9813df5f37fcCasey Dahlin 640c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley void AddPublic(std::unique_ptr<Declaration> member); 650c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley void AddPrivate(std::unique_ptr<Declaration> member); 660c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley 6760a4916eea290a9a62e1f867d1bd9813df5f37fcCasey Dahlin private: 6860a4916eea290a9a62e1f867d1bd9813df5f37fcCasey Dahlin std::string name_; 6960a4916eea290a9a62e1f867d1bd9813df5f37fcCasey Dahlin std::string parent_; 70f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley std::vector<std::unique_ptr<Declaration>> public_members_; 71f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley std::vector<std::unique_ptr<Declaration>> private_members_; 7260a4916eea290a9a62e1f867d1bd9813df5f37fcCasey Dahlin 73f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley DISALLOW_COPY_AND_ASSIGN(ClassDecl); 74f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley}; // class ClassDecl 7560a4916eea290a9a62e1f867d1bd9813df5f37fcCasey Dahlin 76a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wileyclass Enum : public Declaration { 77a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley public: 78fd7dc03fdd1e0cd558df43a155ab1644cbe2b553Christopher Wiley Enum(const std::string& name, const std::string& base_type); 7923285262ca6f45149d23851995f57bf50e323544Christopher Wiley explicit Enum(const std::string& name); 80a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley virtual ~Enum() = default; 81a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley 82fd7dc03fdd1e0cd558df43a155ab1644cbe2b553Christopher Wiley bool HasValues() const { return !fields_.empty(); } 83a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley void Write(CodeWriter* to) const override; 84a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley 85a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley void AddValue(const std::string& key, const std::string& value); 86a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley 87a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley private: 88a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley struct EnumField { 89a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley EnumField(const std::string& k, const std::string& v); 90a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley const std::string key; 91a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley const std::string value; 92a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley }; 93a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley 94a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley std::string enum_name_; 95fd7dc03fdd1e0cd558df43a155ab1644cbe2b553Christopher Wiley std::string underlying_type_; 96a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley std::vector<EnumField> fields_; 97a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley 98a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley DISALLOW_COPY_AND_ASSIGN(Enum); 99a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley}; // class Enum 100a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley 10123285262ca6f45149d23851995f57bf50e323544Christopher Wileyclass ArgList : public AstNode { 10223285262ca6f45149d23851995f57bf50e323544Christopher Wiley public: 103ade4b45d8d9e922aca70d0995f2fb8a170de838bChristopher Wiley ArgList() = default; 10423285262ca6f45149d23851995f57bf50e323544Christopher Wiley explicit ArgList(const std::string& single_argument); 10523285262ca6f45149d23851995f57bf50e323544Christopher Wiley explicit ArgList(const std::vector<std::string>& arg_list); 106f02facfe96f5ff340e9a51876f2d33e314cebccaChristopher Wiley explicit ArgList(std::vector<std::unique_ptr<AstNode>> arg_list); 107ade4b45d8d9e922aca70d0995f2fb8a170de838bChristopher Wiley ArgList(ArgList&& arg_list); 10823285262ca6f45149d23851995f57bf50e323544Christopher Wiley virtual ~ArgList() = default; 10923285262ca6f45149d23851995f57bf50e323544Christopher Wiley 11023285262ca6f45149d23851995f57bf50e323544Christopher Wiley void Write(CodeWriter* to) const override; 11123285262ca6f45149d23851995f57bf50e323544Christopher Wiley 11223285262ca6f45149d23851995f57bf50e323544Christopher Wiley private: 113f02facfe96f5ff340e9a51876f2d33e314cebccaChristopher Wiley std::vector<std::unique_ptr<AstNode>> arguments_; 11423285262ca6f45149d23851995f57bf50e323544Christopher Wiley 11523285262ca6f45149d23851995f57bf50e323544Christopher Wiley DISALLOW_COPY_AND_ASSIGN(ArgList); 11623285262ca6f45149d23851995f57bf50e323544Christopher Wiley}; // class ArgList 11723285262ca6f45149d23851995f57bf50e323544Christopher Wiley 118f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wileyclass ConstructorDecl : public Declaration { 119a834dd4650b3196893440ee49c7ff6b303b45a92Casey Dahlin public: 120b23149dbc77744864b78edeb9f9cbb2f94f8c06dChristopher Wiley enum Modifiers { 121b23149dbc77744864b78edeb9f9cbb2f94f8c06dChristopher Wiley IS_VIRTUAL = 1 << 0, 122b23149dbc77744864b78edeb9f9cbb2f94f8c06dChristopher Wiley IS_DEFAULT = 1 << 1, 123b23149dbc77744864b78edeb9f9cbb2f94f8c06dChristopher Wiley IS_EXPLICIT = 1 << 2, 124b23149dbc77744864b78edeb9f9cbb2f94f8c06dChristopher Wiley }; 125b23149dbc77744864b78edeb9f9cbb2f94f8c06dChristopher Wiley 126f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley ConstructorDecl(const std::string& name, 127ade4b45d8d9e922aca70d0995f2fb8a170de838bChristopher Wiley ArgList&& arg_list); 128a7a5c10eb8c52639052e144804936432654dc9d8Christopher Wiley ConstructorDecl(const std::string& name, 129ade4b45d8d9e922aca70d0995f2fb8a170de838bChristopher Wiley ArgList&& arg_list, 130b23149dbc77744864b78edeb9f9cbb2f94f8c06dChristopher Wiley uint32_t modifiers); 131a834dd4650b3196893440ee49c7ff6b303b45a92Casey Dahlin 132f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley virtual ~ConstructorDecl() = default; 133a834dd4650b3196893440ee49c7ff6b303b45a92Casey Dahlin 134a834dd4650b3196893440ee49c7ff6b303b45a92Casey Dahlin void Write(CodeWriter* to) const override; 135a834dd4650b3196893440ee49c7ff6b303b45a92Casey Dahlin 136a834dd4650b3196893440ee49c7ff6b303b45a92Casey Dahlin private: 137a834dd4650b3196893440ee49c7ff6b303b45a92Casey Dahlin const std::string name_; 138ade4b45d8d9e922aca70d0995f2fb8a170de838bChristopher Wiley const ArgList arguments_; 139b23149dbc77744864b78edeb9f9cbb2f94f8c06dChristopher Wiley const uint32_t modifiers_; 140a834dd4650b3196893440ee49c7ff6b303b45a92Casey Dahlin 141f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley DISALLOW_COPY_AND_ASSIGN(ConstructorDecl); 142da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley}; // class ConstructorDecl 143a834dd4650b3196893440ee49c7ff6b303b45a92Casey Dahlin 144f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wileyclass MethodDecl : public Declaration { 14588924d60d8758c87ad48e6d227839eb354cf369eCasey Dahlin public: 1460c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley enum Modifiers { 1470c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley IS_CONST = 1 << 0, 1480c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley IS_VIRTUAL = 1 << 1, 1490c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley IS_OVERRIDE = 1 << 2, 1500c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley IS_PURE_VIRTUAL = 1 << 3, 1510c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley }; 1520c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley 1530c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley MethodDecl(const std::string& return_type, 1540c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley const std::string& name, 155ade4b45d8d9e922aca70d0995f2fb8a170de838bChristopher Wiley ArgList&& arg_list); 156f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley MethodDecl(const std::string& return_type, 157f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley const std::string& name, 158ade4b45d8d9e922aca70d0995f2fb8a170de838bChristopher Wiley ArgList&& arg_list, 1590c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley uint32_t modifiers); 160f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley virtual ~MethodDecl() = default; 16188924d60d8758c87ad48e6d227839eb354cf369eCasey Dahlin 16288924d60d8758c87ad48e6d227839eb354cf369eCasey Dahlin void Write(CodeWriter* to) const override; 16388924d60d8758c87ad48e6d227839eb354cf369eCasey Dahlin 16488924d60d8758c87ad48e6d227839eb354cf369eCasey Dahlin private: 16588924d60d8758c87ad48e6d227839eb354cf369eCasey Dahlin const std::string return_type_; 16688924d60d8758c87ad48e6d227839eb354cf369eCasey Dahlin const std::string name_; 167ade4b45d8d9e922aca70d0995f2fb8a170de838bChristopher Wiley const ArgList arguments_; 1680c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley bool is_const_ = false; 1690c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley bool is_virtual_ = false; 1700c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley bool is_override_ = false; 1710c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley bool is_pure_virtual_ = false; 17288924d60d8758c87ad48e6d227839eb354cf369eCasey Dahlin 173f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley DISALLOW_COPY_AND_ASSIGN(MethodDecl); 174da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley}; // class MethodDecl 175da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 176da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wileyclass StatementBlock : public Declaration { 177da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley public: 178da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley StatementBlock() = default; 179da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley virtual ~StatementBlock() = default; 180da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 181da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley void AddStatement(std::unique_ptr<AstNode> statement); 18223285262ca6f45149d23851995f57bf50e323544Christopher Wiley void AddStatement(AstNode* statement); // Takes ownership 183da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley void AddLiteral(const std::string& expression, bool add_semicolon = true); 1840eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley bool Empty() const { return statements_.empty(); } 185da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 186da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley void Write(CodeWriter* to) const override; 187da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 188da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley private: 189da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley std::vector<std::unique_ptr<AstNode>> statements_; 190da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 191da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley DISALLOW_COPY_AND_ASSIGN(StatementBlock); 192da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley}; // class StatementBlock 193da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 194f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wileyclass ConstructorImpl : public Declaration { 195f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley public: 196f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley ConstructorImpl(const std::string& class_name, 197f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley ArgList&& arg_list, 198f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley const std::vector<std::string>& initializer_list); 199f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley virtual ~ConstructorImpl() = default; 200f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley 201f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley void Write(CodeWriter* to) const override; 202f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley 203f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley private: 204f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley std::string class_name_; 205f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley ArgList arguments_; 206f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley std::vector<std::string> initializer_list_; 207f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley StatementBlock body_; 208f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley 209f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley DISALLOW_COPY_AND_ASSIGN(ConstructorImpl); 210f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley}; // class ConstructorImpl 211f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley 212da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wileyclass MethodImpl : public Declaration { 213da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley public: 214da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley // Passing an empty class name causes the method to be declared as a normal 215da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley // function (ie. no ClassName:: qualifier). 216da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley MethodImpl(const std::string& return_type, 217da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley const std::string& class_name, 218da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley const std::string& method_name, 219ade4b45d8d9e922aca70d0995f2fb8a170de838bChristopher Wiley ArgList&& arg_list, 220da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley bool is_const_method = false); 221da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley virtual ~MethodImpl() = default; 222da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 223f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley // MethodImpl retains ownership of the statement block. 224f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley StatementBlock* GetStatementBlock(); 225f9688b04c9e34063e8b2dffab4827e02f4f104faChristopher Wiley 226da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley void Write(CodeWriter* to) const override; 227da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 228da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley private: 229da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley std::string return_type_; 230da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley std::string method_name_; 231ade4b45d8d9e922aca70d0995f2fb8a170de838bChristopher Wiley const ArgList arguments_; 232da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley StatementBlock statements_; 233da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley bool is_const_method_ = false; 234da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 235da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley DISALLOW_COPY_AND_ASSIGN(MethodImpl); 236da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley}; // class MethodImpl 237da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 238da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wileyclass SwitchStatement : public AstNode { 239da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley public: 240da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley explicit SwitchStatement(const std::string& expression); 241da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley virtual ~SwitchStatement() = default; 242da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 243da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley // Add a case statement and return a pointer code block corresponding 244da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley // to the case. The switch statement will add a break statement 245da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley // after the code block by default to prevent accidental fall-through. 246da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley // Returns nullptr on duplicate value expressions (by strcmp, not value 247da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley // equivalence). 248da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley StatementBlock* AddCase(const std::string& value_expression); 249da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley void Write(CodeWriter* to) const override; 250da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 251da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley private: 252da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley const std::string switch_expression_; 253da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley std::vector<std::string> case_values_; 254da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley std::vector<std::unique_ptr<StatementBlock>> case_logic_; 255da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 256da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley DISALLOW_COPY_AND_ASSIGN(SwitchStatement); 257da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley}; // class SwitchStatement 258da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 25923285262ca6f45149d23851995f57bf50e323544Christopher Wileyclass Assignment : public AstNode { 26023285262ca6f45149d23851995f57bf50e323544Christopher Wiley public: 26123285262ca6f45149d23851995f57bf50e323544Christopher Wiley Assignment(const std::string& left, const std::string& right); 26223285262ca6f45149d23851995f57bf50e323544Christopher Wiley Assignment(const std::string& left, AstNode* right); 26323285262ca6f45149d23851995f57bf50e323544Christopher Wiley ~Assignment() = default; 26423285262ca6f45149d23851995f57bf50e323544Christopher Wiley void Write(CodeWriter* to) const override; 26523285262ca6f45149d23851995f57bf50e323544Christopher Wiley 26623285262ca6f45149d23851995f57bf50e323544Christopher Wiley private: 26723285262ca6f45149d23851995f57bf50e323544Christopher Wiley const std::string lhs_; 26823285262ca6f45149d23851995f57bf50e323544Christopher Wiley std::unique_ptr<AstNode> rhs_; 26923285262ca6f45149d23851995f57bf50e323544Christopher Wiley 27023285262ca6f45149d23851995f57bf50e323544Christopher Wiley DISALLOW_COPY_AND_ASSIGN(Assignment); 27123285262ca6f45149d23851995f57bf50e323544Christopher Wiley}; // class Assignment 27223285262ca6f45149d23851995f57bf50e323544Christopher Wiley 27323285262ca6f45149d23851995f57bf50e323544Christopher Wileyclass MethodCall : public AstNode { 27423285262ca6f45149d23851995f57bf50e323544Christopher Wiley public: 27523285262ca6f45149d23851995f57bf50e323544Christopher Wiley MethodCall(const std::string& method_name, 27623285262ca6f45149d23851995f57bf50e323544Christopher Wiley const std::string& single_argument); 277ade4b45d8d9e922aca70d0995f2fb8a170de838bChristopher Wiley MethodCall(const std::string& method_name, ArgList&& arg_list); 27823285262ca6f45149d23851995f57bf50e323544Christopher Wiley ~MethodCall() = default; 27923285262ca6f45149d23851995f57bf50e323544Christopher Wiley void Write(CodeWriter* to) const override; 28023285262ca6f45149d23851995f57bf50e323544Christopher Wiley 28123285262ca6f45149d23851995f57bf50e323544Christopher Wiley private: 28223285262ca6f45149d23851995f57bf50e323544Christopher Wiley const std::string method_name_; 283ade4b45d8d9e922aca70d0995f2fb8a170de838bChristopher Wiley const ArgList arguments_; 28423285262ca6f45149d23851995f57bf50e323544Christopher Wiley 28523285262ca6f45149d23851995f57bf50e323544Christopher Wiley DISALLOW_COPY_AND_ASSIGN(MethodCall); 28623285262ca6f45149d23851995f57bf50e323544Christopher Wiley}; // class MethodCall 28723285262ca6f45149d23851995f57bf50e323544Christopher Wiley 2880eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wileyclass IfStatement : public AstNode { 2890eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley public: 2900eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley IfStatement(AstNode* expression, 2910eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley bool invert_expression = false); 2920eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley virtual ~IfStatement() = default; 2930eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley StatementBlock* OnTrue() { return &on_true_; } 2940eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley StatementBlock* OnFalse() { return &on_false_; } 2950eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley void Write(CodeWriter* to) const override; 2960eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley 2970eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley private: 2980eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley std::unique_ptr<AstNode> expression_; 2990eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley bool invert_expression_ = false; 3000eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley StatementBlock on_true_; 3010eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley StatementBlock on_false_; 3020eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley 3030eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley DISALLOW_COPY_AND_ASSIGN(IfStatement); 3040eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley}; // class IfStatement 3050eb903eff2376941ad7e947bd5d3a32e15d89f69Christopher Wiley 3063c5d28def0021e34e526debc71e6b4473c0b9a0cChristopher Wileyclass Statement : public AstNode { 307da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley public: 3083c5d28def0021e34e526debc71e6b4473c0b9a0cChristopher Wiley explicit Statement(std::unique_ptr<AstNode> expression); 3093c5d28def0021e34e526debc71e6b4473c0b9a0cChristopher Wiley explicit Statement(AstNode* expression); // Takes possession. 3103c5d28def0021e34e526debc71e6b4473c0b9a0cChristopher Wiley explicit Statement(const std::string& expression); 3113c5d28def0021e34e526debc71e6b4473c0b9a0cChristopher Wiley ~Statement() = default; 312da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley void Write(CodeWriter* to) const override; 313da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 314da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley private: 3153c5d28def0021e34e526debc71e6b4473c0b9a0cChristopher Wiley std::unique_ptr<AstNode> expression_; 316da6959923ceecc58d0fd5a5ba5da12ee614386a0Christopher Wiley 3173c5d28def0021e34e526debc71e6b4473c0b9a0cChristopher Wiley DISALLOW_COPY_AND_ASSIGN(Statement); 3183c5d28def0021e34e526debc71e6b4473c0b9a0cChristopher Wiley}; // class Statement 31988924d60d8758c87ad48e6d227839eb354cf369eCasey Dahlin 320d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wileyclass Comparison : public AstNode { 321d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley public: 322d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley Comparison(AstNode* lhs, const std::string& comparison, AstNode* rhs); 323d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley ~Comparison() = default; 324d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley void Write(CodeWriter* to) const override; 325d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley 326d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley private: 327d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley std::unique_ptr<AstNode> left_; 328d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley std::unique_ptr<AstNode> right_; 329d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley const std::string operator_; 330d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley 331d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley DISALLOW_COPY_AND_ASSIGN(Comparison); 332d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley}; // class Comparison 333d55db28b25e03de7edafc8c35db53a9f491bd38fChristopher Wiley 33423285262ca6f45149d23851995f57bf50e323544Christopher Wileyclass LiteralExpression : public AstNode { 33523285262ca6f45149d23851995f57bf50e323544Christopher Wiley public: 33623285262ca6f45149d23851995f57bf50e323544Christopher Wiley explicit LiteralExpression(const std::string& expression); 33723285262ca6f45149d23851995f57bf50e323544Christopher Wiley ~LiteralExpression() = default; 33823285262ca6f45149d23851995f57bf50e323544Christopher Wiley void Write(CodeWriter* to) const override; 33923285262ca6f45149d23851995f57bf50e323544Christopher Wiley 34023285262ca6f45149d23851995f57bf50e323544Christopher Wiley private: 34123285262ca6f45149d23851995f57bf50e323544Christopher Wiley const std::string expression_; 34223285262ca6f45149d23851995f57bf50e323544Christopher Wiley 34323285262ca6f45149d23851995f57bf50e323544Christopher Wiley DISALLOW_COPY_AND_ASSIGN(LiteralExpression); 34423285262ca6f45149d23851995f57bf50e323544Christopher Wiley}; // class LiteralExpression 34523285262ca6f45149d23851995f57bf50e323544Christopher Wiley 346f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wileyclass CppNamespace : public Declaration { 34734b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin public: 34834b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin CppNamespace(const std::string& name, 349f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley std::vector<std::unique_ptr<Declaration>> declarations); 3500c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley CppNamespace(const std::string& name, 3510c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley std::unique_ptr<Declaration> declaration); 3520c732dbae2e6b33cde9a266ea091e1459742f1f1Christopher Wiley CppNamespace(const std::string& name); 353b7d0f7f4bb08171e025659d17cd3bb57b35b739aCasey Dahlin virtual ~CppNamespace() = default; 35434b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin 35534b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin void Write(CodeWriter* to) const override; 35634b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin 35734b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin private: 358f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley std::vector<std::unique_ptr<Declaration>> declarations_; 35934b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin std::string name_; 36034b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin 36134b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin DISALLOW_COPY_AND_ASSIGN(CppNamespace); 36234b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin}; // class CppNamespace 363f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley 364f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wileyclass Document : public AstNode { 365f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley public: 366f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley Document(const std::vector<std::string>& include_list, 367f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley std::unique_ptr<CppNamespace> a_namespace); 36834b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin 36934b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin void Write(CodeWriter* to) const override; 370f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley 371f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley private: 372f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley std::vector<std::string> include_list_; 37334b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin std::unique_ptr<CppNamespace> namespace_; 374f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley 375f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley DISALLOW_COPY_AND_ASSIGN(Document); 376f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley}; // class Document 377f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley 378f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wileyclass CppHeader final : public Document { 37934b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin public: 38034b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin CppHeader(const std::string& include_guard, 38134b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin const std::vector<std::string>& include_list, 382b7d0f7f4bb08171e025659d17cd3bb57b35b739aCasey Dahlin std::unique_ptr<CppNamespace> a_namespace); 38334b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin void Write(CodeWriter* to) const override; 38434b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin 38534b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin private: 38634b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin const std::string include_guard_; 38734b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin 38834b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin DISALLOW_COPY_AND_ASSIGN(CppHeader); 38934b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin}; // class CppHeader 39034b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin 391f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wileyclass CppSource final : public Document { 39234b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin public: 39334b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin CppSource(const std::vector<std::string>& include_list, 394b7d0f7f4bb08171e025659d17cd3bb57b35b739aCasey Dahlin std::unique_ptr<CppNamespace> a_namespace); 39534b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin 39634b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin private: 39734b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin DISALLOW_COPY_AND_ASSIGN(CppSource); 39834b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin}; // class CppSource 39934b8610486ce41e1a611a2e1ff7f3aedcc49c5f5Casey Dahlin 400f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley} // namespace cpp 401f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley} // namespace aidl 402f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley} // namespace android 403f600a555ef6f86afea7d55fc735f0c7b5f7cf6a5Christopher Wiley 404f944e79bdaf92fffe9ea78314d3636e7e4de8c51Christopher Wiley#endif // AIDL_AST_CPP_H_ 405