1 /* 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 17 #pragma once 18 19 #include <stdarg.h> 20 #include <stdio.h> 21 #include <memory> 22 #include <optional> 23 #include <string> 24 #include <variant> 25 #include <vector> 26 27 enum { 28 PACKAGE_PRIVATE = 0x00000000, 29 PUBLIC = 0x00000001, 30 PRIVATE = 0x00000002, 31 PROTECTED = 0x00000003, 32 SCOPE_MASK = 0x00000003, 33 34 STATIC = 0x00000010, 35 FINAL = 0x00000020, 36 ABSTRACT = 0x00000040, 37 38 OVERRIDE = 0x00000100, 39 40 ALL_MODIFIERS = 0xffffffff 41 }; 42 43 namespace android { 44 namespace aidl { 45 class CodeWriter; 46 } // namespace aidl 47 } // namespace android 48 49 namespace android { 50 namespace aidl { 51 namespace java { 52 53 // Write the modifiers that are set in both mod and mask 54 void WriteModifiers(CodeWriter* to, int mod, int mask); 55 56 struct AstNode { 57 AstNode() = default; 58 virtual ~AstNode() = default; 59 virtual void Write(CodeWriter* to) const = 0; 60 std::string ToString(); 61 }; 62 63 struct ClassElement : public AstNode { 64 ClassElement() = default; 65 virtual ~ClassElement() = default; 66 }; 67 68 struct Expression : public AstNode { 69 virtual ~Expression() = default; 70 }; 71 72 struct LiteralExpression : public Expression { 73 std::string value; 74 75 explicit LiteralExpression(const std::string& value); 76 virtual ~LiteralExpression() = default; 77 void Write(CodeWriter* to) const override; 78 }; 79 80 struct StringLiteralExpression : public Expression { 81 std::string value; 82 83 explicit StringLiteralExpression(const std::string& value); 84 virtual ~StringLiteralExpression() = default; 85 void Write(CodeWriter* to) const override; 86 }; 87 88 struct Variable : public Expression { 89 std::vector<std::string> annotations; 90 const std::string type; 91 std::string name; 92 93 Variable() = default; 94 Variable(const std::string& type, const std::string& name); 95 virtual ~Variable() = default; 96 97 void WriteDeclaration(CodeWriter* to) const; 98 void Write(CodeWriter* to) const; 99 }; 100 101 struct FieldVariable : public Expression { 102 std::variant<std::shared_ptr<Expression>, std::string> receiver; 103 std::string name; 104 105 FieldVariable(std::shared_ptr<Expression> object, const std::string& name); 106 FieldVariable(const std::string& clazz, const std::string& name); 107 virtual ~FieldVariable() = default; 108 109 void Write(CodeWriter* to) const; 110 }; 111 112 struct Field : public ClassElement { 113 std::string comment; 114 std::vector<std::string> annotations; 115 int modifiers = 0; 116 std::shared_ptr<Variable> variable = nullptr; 117 std::string value; 118 119 Field() = default; 120 Field(int modifiers, std::shared_ptr<Variable> variable); 121 virtual ~Field() = default; 122 123 void Write(CodeWriter* to) const override; 124 }; 125 126 struct Statement : public AstNode { 127 virtual ~Statement() = default; 128 }; 129 130 struct LiteralStatement : public Statement { 131 public: 132 LiteralStatement(const std::string& value); 133 virtual ~LiteralStatement() = default; 134 void Write(CodeWriter* to) const override; 135 136 private: 137 const std::string value_; 138 }; 139 140 struct StatementBlock : public Statement { 141 std::vector<std::shared_ptr<Statement>> statements; 142 143 StatementBlock() = default; 144 virtual ~StatementBlock() = default; 145 void Write(CodeWriter* to) const override; 146 147 void Add(std::shared_ptr<Statement> statement); 148 void Add(std::shared_ptr<Expression> expression); 149 }; 150 151 struct ExpressionStatement : public Statement { 152 std::shared_ptr<Expression> expression; 153 154 explicit ExpressionStatement(std::shared_ptr<Expression> expression); 155 virtual ~ExpressionStatement() = default; 156 void Write(CodeWriter* to) const override; 157 }; 158 159 struct Assignment : public Expression { 160 std::shared_ptr<Variable> lvalue; 161 std::shared_ptr<Expression> rvalue; 162 std::optional<std::string> cast = std::nullopt; 163 164 Assignment(std::shared_ptr<Variable> lvalue, std::shared_ptr<Expression> rvalue); 165 Assignment(std::shared_ptr<Variable> lvalue, std::shared_ptr<Expression> rvalue, 166 std::string cast); 167 virtual ~Assignment() = default; 168 void Write(CodeWriter* to) const override; 169 }; 170 171 struct MethodCall : public Expression { 172 std::variant<std::monostate, std::shared_ptr<Expression>, std::string> receiver; 173 std::string name; 174 std::vector<std::shared_ptr<Expression>> arguments; 175 std::vector<std::string> exceptions; 176 177 explicit MethodCall(const std::string& name); 178 MethodCall(const std::string& name, const std::vector<std::shared_ptr<Expression>>& args); 179 MethodCall(std::shared_ptr<Expression> obj, const std::string& name); 180 MethodCall(const std::string& clazz, const std::string& name); 181 MethodCall(std::shared_ptr<Expression> obj, const std::string& name, 182 const std::vector<std::shared_ptr<Expression>>& args); 183 MethodCall(const std::string&, const std::string& name, 184 const std::vector<std::shared_ptr<Expression>>& args); 185 virtual ~MethodCall() = default; 186 void Write(CodeWriter* to) const override; 187 }; 188 189 struct Comparison : public Expression { 190 std::shared_ptr<Expression> lvalue; 191 std::string op; 192 std::shared_ptr<Expression> rvalue; 193 194 Comparison(std::shared_ptr<Expression> lvalue, const std::string& op, 195 std::shared_ptr<Expression> rvalue); 196 virtual ~Comparison() = default; 197 void Write(CodeWriter* to) const override; 198 }; 199 200 struct NewExpression : public Expression { 201 const std::string instantiableName; 202 std::vector<std::shared_ptr<Expression>> arguments; 203 204 explicit NewExpression(const std::string& name); 205 NewExpression(const std::string& name, const std::vector<std::shared_ptr<Expression>>& args); 206 virtual ~NewExpression() = default; 207 void Write(CodeWriter* to) const override; 208 }; 209 210 struct Cast : public Expression { 211 const std::string type; 212 std::shared_ptr<Expression> expression = nullptr; 213 214 Cast() = default; 215 Cast(const std::string& type, std::shared_ptr<Expression> expression); 216 virtual ~Cast() = default; 217 void Write(CodeWriter* to) const override; 218 }; 219 220 struct VariableDeclaration : public Statement { 221 std::shared_ptr<Variable> lvalue = nullptr; 222 std::shared_ptr<Expression> rvalue = nullptr; 223 224 explicit VariableDeclaration(std::shared_ptr<Variable> lvalue); 225 VariableDeclaration(std::shared_ptr<Variable> lvalue, std::shared_ptr<Expression> rvalue); 226 virtual ~VariableDeclaration() = default; 227 void Write(CodeWriter* to) const override; 228 }; 229 230 struct IfStatement : public Statement { 231 std::shared_ptr<Expression> expression = nullptr; 232 std::shared_ptr<StatementBlock> statements = std::make_shared<StatementBlock>(); 233 std::shared_ptr<IfStatement> elseif = nullptr; 234 235 IfStatement() = default; 236 virtual ~IfStatement() = default; 237 void Write(CodeWriter* to) const override; 238 }; 239 240 struct ReturnStatement : public Statement { 241 std::shared_ptr<Expression> expression; 242 243 explicit ReturnStatement(std::shared_ptr<Expression> expression); 244 virtual ~ReturnStatement() = default; 245 void Write(CodeWriter* to) const override; 246 }; 247 248 struct BreakStatement : public Statement { 249 BreakStatement() = default; 250 virtual ~BreakStatement() = default; 251 void Write(CodeWriter* to) const override; 252 }; 253 254 struct TryStatement : public Statement { 255 std::shared_ptr<StatementBlock> statements = std::make_shared<StatementBlock>(); 256 257 TryStatement() = default; 258 virtual ~TryStatement() = default; 259 void Write(CodeWriter* to) const override; 260 }; 261 262 struct FinallyStatement : public Statement { 263 std::shared_ptr<StatementBlock> statements = std::make_shared<StatementBlock>(); 264 265 FinallyStatement() = default; 266 virtual ~FinallyStatement() = default; 267 void Write(CodeWriter* to) const override; 268 }; 269 270 struct Case : public AstNode { 271 std::vector<std::string> cases; 272 std::shared_ptr<StatementBlock> statements = std::make_shared<StatementBlock>(); 273 274 Case() = default; 275 explicit Case(const std::string& c); 276 virtual ~Case() = default; 277 void Write(CodeWriter* to) const override; 278 }; 279 280 struct SwitchStatement : public Statement { 281 std::shared_ptr<Expression> expression; 282 std::vector<std::shared_ptr<Case>> cases; 283 284 explicit SwitchStatement(std::shared_ptr<Expression> expression); 285 virtual ~SwitchStatement() = default; 286 void Write(CodeWriter* to) const override; 287 }; 288 289 struct Method : public ClassElement { 290 std::string comment; 291 std::vector<std::string> annotations; 292 int modifiers = 0; 293 std::optional<std::string> returnType = std::nullopt; // nullopt means constructor 294 std::string name; 295 std::vector<std::shared_ptr<Variable>> parameters; 296 std::vector<std::string> exceptions; 297 std::shared_ptr<StatementBlock> statements = nullptr; 298 299 Method() = default; 300 virtual ~Method() = default; 301 302 void Write(CodeWriter* to) const override; 303 }; 304 305 struct LiteralClassElement : public ClassElement { 306 std::string element; 307 LiteralClassElementLiteralClassElement308 LiteralClassElement(std::string e) : element(e) {} 309 virtual ~LiteralClassElement() = default; 310 311 void Write(CodeWriter* to) const override; 312 }; 313 314 struct Class : public ClassElement { 315 enum { CLASS, INTERFACE }; 316 317 std::string comment; 318 std::vector<std::string> annotations; 319 int modifiers = 0; 320 int what = CLASS; // CLASS or INTERFACE 321 std::string type; 322 std::optional<std::string> extends = std::nullopt; 323 std::vector<std::string> interfaces; 324 std::vector<std::shared_ptr<ClassElement>> elements; 325 326 Class() = default; 327 virtual ~Class() = default; 328 329 void Write(CodeWriter* to) const override; 330 }; 331 332 extern std::shared_ptr<Expression> NULL_VALUE; 333 extern std::shared_ptr<Expression> THIS_VALUE; 334 extern std::shared_ptr<Expression> SUPER_VALUE; 335 extern std::shared_ptr<Expression> TRUE_VALUE; 336 extern std::shared_ptr<Expression> FALSE_VALUE; 337 } // namespace java 338 } // namespace aidl 339 } // namespace android 340