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 NewArrayExpression : public Expression { 211 const std::string type; 212 std::shared_ptr<Expression> size; 213 214 NewArrayExpression(const std::string& type, std::shared_ptr<Expression> size); 215 virtual ~NewArrayExpression() = default; 216 void Write(CodeWriter* to) const override; 217 }; 218 219 struct Cast : public Expression { 220 const std::string type; 221 std::shared_ptr<Expression> expression = nullptr; 222 223 Cast() = default; 224 Cast(const std::string& type, std::shared_ptr<Expression> expression); 225 virtual ~Cast() = default; 226 void Write(CodeWriter* to) const override; 227 }; 228 229 struct VariableDeclaration : public Statement { 230 std::shared_ptr<Variable> lvalue = nullptr; 231 std::shared_ptr<Expression> rvalue = nullptr; 232 233 explicit VariableDeclaration(std::shared_ptr<Variable> lvalue); 234 VariableDeclaration(std::shared_ptr<Variable> lvalue, std::shared_ptr<Expression> rvalue); 235 virtual ~VariableDeclaration() = default; 236 void Write(CodeWriter* to) const override; 237 }; 238 239 struct IfStatement : public Statement { 240 std::shared_ptr<Expression> expression = nullptr; 241 std::shared_ptr<StatementBlock> statements = std::make_shared<StatementBlock>(); 242 std::shared_ptr<IfStatement> elseif = nullptr; 243 244 IfStatement() = default; 245 virtual ~IfStatement() = default; 246 void Write(CodeWriter* to) const override; 247 }; 248 249 struct ReturnStatement : public Statement { 250 std::shared_ptr<Expression> expression; 251 252 explicit ReturnStatement(std::shared_ptr<Expression> expression); 253 virtual ~ReturnStatement() = default; 254 void Write(CodeWriter* to) const override; 255 }; 256 257 struct TryStatement : public Statement { 258 std::shared_ptr<StatementBlock> statements = std::make_shared<StatementBlock>(); 259 260 TryStatement() = default; 261 virtual ~TryStatement() = default; 262 void Write(CodeWriter* to) const override; 263 }; 264 265 struct FinallyStatement : public Statement { 266 std::shared_ptr<StatementBlock> statements = std::make_shared<StatementBlock>(); 267 268 FinallyStatement() = default; 269 virtual ~FinallyStatement() = default; 270 void Write(CodeWriter* to) const override; 271 }; 272 273 struct Case : public AstNode { 274 std::vector<std::string> cases; 275 std::shared_ptr<StatementBlock> statements = std::make_shared<StatementBlock>(); 276 277 Case() = default; 278 explicit Case(const std::string& c); 279 virtual ~Case() = default; 280 void Write(CodeWriter* to) const override; 281 }; 282 283 struct SwitchStatement : public Statement { 284 std::shared_ptr<Expression> expression; 285 std::vector<std::shared_ptr<Case>> cases; 286 287 explicit SwitchStatement(std::shared_ptr<Expression> expression); 288 virtual ~SwitchStatement() = default; 289 void Write(CodeWriter* to) const override; 290 }; 291 292 struct Method : public ClassElement { 293 std::string comment; 294 std::vector<std::string> annotations; 295 int modifiers = 0; 296 std::optional<std::string> returnType = std::nullopt; // nullopt means constructor 297 std::string name; 298 std::vector<std::shared_ptr<Variable>> parameters; 299 std::vector<std::string> exceptions; 300 std::shared_ptr<StatementBlock> statements = nullptr; 301 302 Method() = default; 303 virtual ~Method() = default; 304 305 void Write(CodeWriter* to) const override; 306 }; 307 308 struct LiteralClassElement : public ClassElement { 309 std::string element; 310 LiteralClassElementLiteralClassElement311 LiteralClassElement(std::string e) : element(e) {} 312 virtual ~LiteralClassElement() = default; 313 314 void Write(CodeWriter* to) const override; 315 }; 316 317 struct Class : public ClassElement { 318 enum { CLASS, INTERFACE }; 319 320 std::string comment; 321 std::vector<std::string> annotations; 322 int modifiers = 0; 323 int what = CLASS; // CLASS or INTERFACE 324 std::string type; 325 std::optional<std::string> extends = std::nullopt; 326 std::vector<std::string> interfaces; 327 std::vector<std::shared_ptr<ClassElement>> elements; 328 329 Class() = default; 330 virtual ~Class() = default; 331 332 void Write(CodeWriter* to) const override; 333 }; 334 335 class Document : public AstNode { 336 public: 337 Document(const std::string& comment, 338 const std::string& package, 339 std::unique_ptr<Class> clazz); 340 virtual ~Document() = default; 341 void Write(CodeWriter* to) const override; 342 343 private: 344 std::string comment_; 345 std::string package_; 346 std::unique_ptr<Class> clazz_; 347 }; 348 349 extern std::shared_ptr<Expression> NULL_VALUE; 350 extern std::shared_ptr<Expression> THIS_VALUE; 351 extern std::shared_ptr<Expression> SUPER_VALUE; 352 extern std::shared_ptr<Expression> TRUE_VALUE; 353 extern std::shared_ptr<Expression> FALSE_VALUE; 354 } // namespace java 355 } // namespace aidl 356 } // namespace android 357