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