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