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