Christopher Wiley | 038485e | 2015-09-12 11:14:14 -0700 | [diff] [blame] | 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 | #ifndef AIDL_AST_JAVA_H_ |
| 18 | #define AIDL_AST_JAVA_H_ |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | #include <set> |
| 23 | #include <stdarg.h> |
| 24 | #include <stdio.h> |
| 25 | |
Christopher Wiley | 9f4c7ae | 2015-08-24 14:07:32 -0700 | [diff] [blame] | 26 | using std::set; |
| 27 | using std::string; |
| 28 | using std::vector; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 29 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 30 | enum { |
| 31 | PACKAGE_PRIVATE = 0x00000000, |
| 32 | PUBLIC = 0x00000001, |
| 33 | PRIVATE = 0x00000002, |
| 34 | PROTECTED = 0x00000003, |
| 35 | SCOPE_MASK = 0x00000003, |
| 36 | |
| 37 | STATIC = 0x00000010, |
| 38 | FINAL = 0x00000020, |
| 39 | ABSTRACT = 0x00000040, |
| 40 | |
| 41 | OVERRIDE = 0x00000100, |
| 42 | |
| 43 | ALL_MODIFIERS = 0xffffffff |
| 44 | }; |
| 45 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 46 | namespace android { |
| 47 | namespace aidl { |
| 48 | |
| 49 | class CodeWriter; |
| 50 | class Type; |
| 51 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 52 | // Write the modifiers that are set in both mod and mask |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 53 | void WriteModifiers(CodeWriter* to, int mod, int mask); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 54 | |
| 55 | struct ClassElement |
| 56 | { |
| 57 | ClassElement(); |
| 58 | virtual ~ClassElement(); |
| 59 | |
| 60 | virtual void GatherTypes(set<Type*>* types) const = 0; |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 61 | virtual void Write(CodeWriter* to) const = 0; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct Expression |
| 65 | { |
| 66 | virtual ~Expression(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 67 | virtual void Write(CodeWriter* to) const = 0; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | struct LiteralExpression : public Expression |
| 71 | { |
| 72 | string value; |
| 73 | |
| 74 | LiteralExpression(const string& value); |
| 75 | virtual ~LiteralExpression(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 76 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | // TODO: also escape the contents. not needed for now |
| 80 | struct StringLiteralExpression : public Expression |
| 81 | { |
| 82 | string value; |
| 83 | |
| 84 | StringLiteralExpression(const string& value); |
| 85 | virtual ~StringLiteralExpression(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 86 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | struct Variable : public Expression |
| 90 | { |
| 91 | Type* type; |
| 92 | string name; |
| 93 | int dimension; |
| 94 | |
| 95 | Variable(); |
| 96 | Variable(Type* type, const string& name); |
| 97 | Variable(Type* type, const string& name, int dimension); |
| 98 | virtual ~Variable(); |
| 99 | |
| 100 | virtual void GatherTypes(set<Type*>* types) const; |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 101 | void WriteDeclaration(CodeWriter* to) const; |
| 102 | void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | struct FieldVariable : public Expression |
| 106 | { |
| 107 | Expression* object; |
| 108 | Type* clazz; |
| 109 | string name; |
| 110 | |
| 111 | FieldVariable(Expression* object, const string& name); |
| 112 | FieldVariable(Type* clazz, const string& name); |
| 113 | virtual ~FieldVariable(); |
| 114 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 115 | void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | struct Field : public ClassElement |
| 119 | { |
| 120 | string comment; |
| 121 | int modifiers; |
| 122 | Variable *variable; |
| 123 | string value; |
| 124 | |
| 125 | Field(); |
| 126 | Field(int modifiers, Variable* variable); |
| 127 | virtual ~Field(); |
| 128 | |
| 129 | virtual void GatherTypes(set<Type*>* types) const; |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 130 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | struct Statement |
| 134 | { |
| 135 | virtual ~Statement(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 136 | virtual void Write(CodeWriter* to) const = 0; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | struct StatementBlock : public Statement |
| 140 | { |
| 141 | vector<Statement*> statements; |
| 142 | |
| 143 | StatementBlock(); |
| 144 | virtual ~StatementBlock(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 145 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 146 | |
| 147 | void Add(Statement* statement); |
| 148 | void Add(Expression* expression); |
| 149 | }; |
| 150 | |
| 151 | struct ExpressionStatement : public Statement |
| 152 | { |
| 153 | Expression* expression; |
| 154 | |
| 155 | ExpressionStatement(Expression* expression); |
| 156 | virtual ~ExpressionStatement(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 157 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | struct Assignment : public Expression |
| 161 | { |
| 162 | Variable* lvalue; |
| 163 | Expression* rvalue; |
| 164 | Type* cast; |
| 165 | |
| 166 | Assignment(Variable* lvalue, Expression* rvalue); |
| 167 | Assignment(Variable* lvalue, Expression* rvalue, Type* cast); |
| 168 | virtual ~Assignment(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 169 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | struct MethodCall : public Expression |
| 173 | { |
| 174 | Expression* obj; |
| 175 | Type* clazz; |
| 176 | string name; |
| 177 | vector<Expression*> arguments; |
| 178 | vector<string> exceptions; |
| 179 | |
| 180 | MethodCall(const string& name); |
| 181 | MethodCall(const string& name, int argc, ...); |
| 182 | MethodCall(Expression* obj, const string& name); |
| 183 | MethodCall(Type* clazz, const string& name); |
| 184 | MethodCall(Expression* obj, const string& name, int argc, ...); |
| 185 | MethodCall(Type* clazz, const string& name, int argc, ...); |
| 186 | virtual ~MethodCall(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 187 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 188 | |
| 189 | private: |
| 190 | void init(int n, va_list args); |
| 191 | }; |
| 192 | |
| 193 | struct Comparison : public Expression |
| 194 | { |
| 195 | Expression* lvalue; |
| 196 | string op; |
| 197 | Expression* rvalue; |
| 198 | |
| 199 | Comparison(Expression* lvalue, const string& op, Expression* rvalue); |
| 200 | virtual ~Comparison(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 201 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | struct NewExpression : public Expression |
| 205 | { |
| 206 | Type* type; |
| 207 | vector<Expression*> arguments; |
| 208 | |
| 209 | NewExpression(Type* type); |
| 210 | NewExpression(Type* type, int argc, ...); |
| 211 | virtual ~NewExpression(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 212 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 213 | |
| 214 | private: |
| 215 | void init(int n, va_list args); |
| 216 | }; |
| 217 | |
| 218 | struct NewArrayExpression : public Expression |
| 219 | { |
| 220 | Type* type; |
| 221 | Expression* size; |
| 222 | |
| 223 | NewArrayExpression(Type* type, Expression* size); |
| 224 | virtual ~NewArrayExpression(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 225 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | struct Ternary : public Expression |
| 229 | { |
| 230 | Expression* condition; |
| 231 | Expression* ifpart; |
| 232 | Expression* elsepart; |
| 233 | |
| 234 | Ternary(); |
| 235 | Ternary(Expression* condition, Expression* ifpart, Expression* elsepart); |
| 236 | virtual ~Ternary(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 237 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | struct Cast : public Expression |
| 241 | { |
| 242 | Type* type; |
| 243 | Expression* expression; |
| 244 | |
| 245 | Cast(); |
| 246 | Cast(Type* type, Expression* expression); |
| 247 | virtual ~Cast(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 248 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | struct VariableDeclaration : public Statement |
| 252 | { |
| 253 | Variable* lvalue; |
| 254 | Type* cast; |
| 255 | Expression* rvalue; |
| 256 | |
| 257 | VariableDeclaration(Variable* lvalue); |
| 258 | VariableDeclaration(Variable* lvalue, Expression* rvalue, Type* cast = NULL); |
| 259 | virtual ~VariableDeclaration(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 260 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | struct IfStatement : public Statement |
| 264 | { |
| 265 | Expression* expression; |
| 266 | StatementBlock* statements; |
| 267 | IfStatement* elseif; |
| 268 | |
| 269 | IfStatement(); |
| 270 | virtual ~IfStatement(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 271 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | struct ReturnStatement : public Statement |
| 275 | { |
| 276 | Expression* expression; |
| 277 | |
| 278 | ReturnStatement(Expression* expression); |
| 279 | virtual ~ReturnStatement(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 280 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | struct TryStatement : public Statement |
| 284 | { |
| 285 | StatementBlock* statements; |
| 286 | |
| 287 | TryStatement(); |
| 288 | virtual ~TryStatement(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 289 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | struct CatchStatement : public Statement |
| 293 | { |
| 294 | StatementBlock* statements; |
| 295 | Variable* exception; |
| 296 | |
| 297 | CatchStatement(Variable* exception); |
| 298 | virtual ~CatchStatement(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 299 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | struct FinallyStatement : public Statement |
| 303 | { |
| 304 | StatementBlock* statements; |
| 305 | |
| 306 | FinallyStatement(); |
| 307 | virtual ~FinallyStatement(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 308 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | struct Case |
| 312 | { |
| 313 | vector<string> cases; |
| 314 | StatementBlock* statements; |
| 315 | |
| 316 | Case(); |
| 317 | Case(const string& c); |
| 318 | virtual ~Case(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 319 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | struct SwitchStatement : public Statement |
| 323 | { |
| 324 | Expression* expression; |
| 325 | vector<Case*> cases; |
| 326 | |
| 327 | SwitchStatement(Expression* expression); |
| 328 | virtual ~SwitchStatement(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 329 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | struct Break : public Statement |
| 333 | { |
| 334 | Break(); |
| 335 | virtual ~Break(); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 336 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | struct Method : public ClassElement |
| 340 | { |
| 341 | string comment; |
| 342 | int modifiers; |
| 343 | Type* returnType; |
| 344 | size_t returnTypeDimension; |
| 345 | string name; |
| 346 | vector<Variable*> parameters; |
| 347 | vector<Type*> exceptions; |
| 348 | StatementBlock* statements; |
| 349 | |
| 350 | Method(); |
| 351 | virtual ~Method(); |
| 352 | |
| 353 | virtual void GatherTypes(set<Type*>* types) const; |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 354 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 355 | }; |
| 356 | |
| 357 | struct Class : public ClassElement |
| 358 | { |
| 359 | enum { |
| 360 | CLASS, |
| 361 | INTERFACE |
| 362 | }; |
| 363 | |
| 364 | string comment; |
| 365 | int modifiers; |
| 366 | int what; // CLASS or INTERFACE |
| 367 | Type* type; |
| 368 | Type* extends; |
| 369 | vector<Type*> interfaces; |
| 370 | vector<ClassElement*> elements; |
| 371 | |
| 372 | Class(); |
| 373 | virtual ~Class(); |
| 374 | |
| 375 | virtual void GatherTypes(set<Type*>* types) const; |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 376 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | struct Document |
| 380 | { |
| 381 | string comment; |
| 382 | string package; |
| 383 | string originalSrc; |
| 384 | set<Type*> imports; |
| 385 | vector<Class*> classes; |
| 386 | |
| 387 | Document(); |
| 388 | virtual ~Document(); |
| 389 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 390 | virtual void Write(CodeWriter* to) const; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 391 | }; |
| 392 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 393 | } // namespace aidl |
| 394 | } // namespace android |
| 395 | |
Christopher Wiley | 038485e | 2015-09-12 11:14:14 -0700 | [diff] [blame] | 396 | #endif // AIDL_AST_JAVA_H_ |