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