ast.h 38.6 KB
Newer Older
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1
// Copyright (c) 2014 Dropbox, Inc.
Kevin Modzelewski's avatar
Kevin Modzelewski committed
2
//
Kevin Modzelewski's avatar
Kevin Modzelewski committed
3 4 5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Kevin Modzelewski's avatar
Kevin Modzelewski committed
6
//
Kevin Modzelewski's avatar
Kevin Modzelewski committed
7
//    http://www.apache.org/licenses/LICENSE-2.0
Kevin Modzelewski's avatar
Kevin Modzelewski committed
8
//
Kevin Modzelewski's avatar
Kevin Modzelewski committed
9 10 11 12 13 14 15 16 17 18 19 20 21
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef PYSTON_CORE_AST_H
#define PYSTON_CORE_AST_H

#include <cassert>
#include <cstdlib>
#include <stdint.h>
#include <string>
22
#include <vector>
Kevin Modzelewski's avatar
Kevin Modzelewski committed
23

24 25
#include "llvm/ADT/StringRef.h"

26 27
#include "core/common.h"

Kevin Modzelewski's avatar
Kevin Modzelewski committed
28 29 30
namespace pyston {

namespace AST_TYPE {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
// These are in a pretty random order (started off alphabetical but then I had to add more).
// These can be changed freely as long as parse_ast.py is also updated
enum AST_TYPE {
    alias = 1,
    arguments = 2,
    Assert = 3,
    Assign = 4,
    Attribute = 5,
    AugAssign = 6,
    BinOp = 7,
    BoolOp = 8,
    Call = 9,
    ClassDef = 10,
    Compare = 11,
    comprehension = 12,
    Delete = 13,
    Dict = 14,
    Exec = 16,
    ExceptHandler = 17,
    ExtSlice = 18,
    Expr = 19,
    For = 20,
    FunctionDef = 21,
    GeneratorExp = 22,
    Global = 23,
    If = 24,
    IfExp = 25,
    Import = 26,
    ImportFrom = 27,
    Index = 28,
    keyword = 29,
    Lambda = 30,
    List = 31,
    ListComp = 32,
    Module = 33,
    Num = 34,
    Name = 35,
    Pass = 37,
    Pow = 38,
    Print = 39,
    Raise = 40,
    Repr = 41,
    Return = 42,
    Slice = 44,
    Str = 45,
    Subscript = 46,
    TryExcept = 47,
    TryFinally = 48,
    Tuple = 49,
    UnaryOp = 50,
    With = 51,
    While = 52,
    Yield = 53,
    Store = 54,
    Load = 55,
    Param = 56,
    Not = 57,
    In = 58,
    Is = 59,
    IsNot = 60,
    Or = 61,
    And = 62,
    Eq = 63,
    NotEq = 64,
    NotIn = 65,
    GtE = 66,
    Gt = 67,
    Mod = 68,
    Add = 69,
    Continue = 70,
    Lt = 71,
    LtE = 72,
    Break = 73,
    Sub = 74,
    Del = 75,
    Mult = 76,
    Div = 77,
    USub = 78,
    BitAnd = 79,
    BitOr = 80,
    BitXor = 81,
    RShift = 82,
    LShift = 83,
    Invert = 84,
    UAdd = 85,
    FloorDiv = 86,
    DictComp = 15,
    Set = 43,

    // Pseudo-nodes that are specific to this compiler:
    Branch = 200,
    Jump = 201,
    ClsAttribute = 202,
    AugBinOp = 203,
125 126
    Invoke = 204,
    LangPrimitive = 205,
127
    Unreachable = 206,
128

129 130
    // These aren't real AST types, but since we use AST types to represent binexp types
    // and divmod+truediv are essentially types of binops, we add them here (at least for now):
131
    DivMod = 250,
132
    TrueDiv = 251,
133
};
Kevin Modzelewski's avatar
Kevin Modzelewski committed
134 135 136 137 138 139 140 141
};

class ASTVisitor;
class ExprVisitor;
class StmtVisitor;
class AST_keyword;

class AST {
142 143
public:
    virtual ~AST() {}
144

145 146
    const AST_TYPE::AST_TYPE type;
    uint32_t lineno, col_offset;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
147

148
    virtual void accept(ASTVisitor* v) = 0;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
149

150
    AST(AST_TYPE::AST_TYPE type) : type(type) {}
Kevin Modzelewski's avatar
Kevin Modzelewski committed
151 152 153
};

class AST_expr : public AST {
154 155
public:
    virtual void* accept_expr(ExprVisitor* v) = 0;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
156

157
    AST_expr(AST_TYPE::AST_TYPE type) : AST(type) {}
Kevin Modzelewski's avatar
Kevin Modzelewski committed
158 159 160
};

class AST_stmt : public AST {
161 162
public:
    virtual void accept_stmt(StmtVisitor* v) = 0;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
163

164
    AST_stmt(AST_TYPE::AST_TYPE type) : AST(type) {}
Kevin Modzelewski's avatar
Kevin Modzelewski committed
165 166 167 168 169
};



class AST_alias : public AST {
170 171
public:
    const std::string name, asname;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
172

173
    virtual void accept(ASTVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
174

175
    AST_alias(const std::string& name, const std::string& asname) : AST(AST_TYPE::alias), name(name), asname(asname) {}
176

177
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::alias;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
178 179 180
};

class AST_arguments : public AST {
181 182 183
public:
    // no lineno, col_offset attributes
    std::vector<AST_expr*> args, defaults;
184 185 186 187

    // These are represented as strings, not names; not sure why.
    // If they don't exist, the string is empty.
    std::string kwarg, vararg;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
188

189
    virtual void accept(ASTVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
190

191
    AST_arguments() : AST(AST_TYPE::arguments) {}
192

193
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::arguments;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
194 195
};

196
class AST_Assert : public AST_stmt {
197 198
public:
    AST_expr* msg, *test;
199

200 201
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
202

203
    AST_Assert() : AST_stmt(AST_TYPE::Assert) {}
204

205
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Assert;
206 207
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
208
class AST_Assign : public AST_stmt {
209 210 211
public:
    std::vector<AST_expr*> targets;
    AST_expr* value;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
212

213 214
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
215

216
    AST_Assign() : AST_stmt(AST_TYPE::Assign) {}
217

218
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Assign;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
219 220
};

221
class AST_AugAssign : public AST_stmt {
222 223 224 225
public:
    AST_expr* value;
    AST_expr* target;
    AST_TYPE::AST_TYPE op_type;
226

227 228
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
229

230
    AST_AugAssign() : AST_stmt(AST_TYPE::AugAssign) {}
231

232
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::AugAssign;
233 234
};

235
class AST_AugBinOp : public AST_expr {
236 237 238
public:
    AST_TYPE::AST_TYPE op_type;
    AST_expr* left, *right;
239

240 241
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
242

243
    AST_AugBinOp() : AST_expr(AST_TYPE::AugBinOp) {}
244

245
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::AugBinOp;
246 247
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
248
class AST_Attribute : public AST_expr {
249 250 251 252
public:
    AST_expr* value;
    AST_TYPE::AST_TYPE ctx_type;
    std::string attr;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
253

254 255
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
256

257
    AST_Attribute() : AST_expr(AST_TYPE::Attribute) {}
258

259
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Attribute;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
260 261 262
};

class AST_BinOp : public AST_expr {
263 264 265
public:
    AST_TYPE::AST_TYPE op_type;
    AST_expr* left, *right;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
266

267 268
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
269

270
    AST_BinOp() : AST_expr(AST_TYPE::BinOp) {}
271

272
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::BinOp;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
273 274 275
};

class AST_BoolOp : public AST_expr {
276 277 278
public:
    AST_TYPE::AST_TYPE op_type;
    std::vector<AST_expr*> values;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
279

280 281
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
282

283
    AST_BoolOp() : AST_expr(AST_TYPE::BoolOp) {}
284

285
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::BoolOp;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
286 287 288
};

class AST_Break : public AST_stmt {
289 290 291
public:
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
292

293
    AST_Break() : AST_stmt(AST_TYPE::Break) {}
294

295
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Break;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
296 297 298
};

class AST_Call : public AST_expr {
299 300 301 302
public:
    AST_expr* starargs, *kwargs, *func;
    std::vector<AST_expr*> args;
    std::vector<AST_keyword*> keywords;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
303

304 305
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
306

307
    AST_Call() : AST_expr(AST_TYPE::Call) {}
308

309
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Call;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
310 311 312
};

class AST_Compare : public AST_expr {
313 314 315 316
public:
    std::vector<AST_TYPE::AST_TYPE> ops;
    std::vector<AST_expr*> comparators;
    AST_expr* left;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
317

318 319
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
320

321
    AST_Compare() : AST_expr(AST_TYPE::Compare) {}
322

323
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Compare;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
324 325
};

326
class AST_comprehension : public AST {
327 328 329 330
public:
    AST_expr* target;
    AST_expr* iter;
    std::vector<AST_expr*> ifs;
331

332
    virtual void accept(ASTVisitor* v);
333

334
    AST_comprehension() : AST(AST_TYPE::comprehension) {}
335

336
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::comprehension;
337 338
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
339
class AST_ClassDef : public AST_stmt {
340 341 342
public:
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
343

344 345 346
    std::vector<AST_expr*> bases, decorator_list;
    std::vector<AST_stmt*> body;
    std::string name;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
347

348
    AST_ClassDef() : AST_stmt(AST_TYPE::ClassDef) {}
349

350
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::ClassDef;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
351 352 353
};

class AST_Continue : public AST_stmt {
354 355 356
public:
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
357

358
    AST_Continue() : AST_stmt(AST_TYPE::Continue) {}
359

360
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Continue;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
361 362 363
};

class AST_Dict : public AST_expr {
364 365
public:
    std::vector<AST_expr*> keys, values;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
366

367 368
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
369

370
    AST_Dict() : AST_expr(AST_TYPE::Dict) {}
371

372
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Dict;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
373 374
};

375 376 377 378 379 380 381 382 383 384 385 386 387
class AST_DictComp : public AST_expr {
public:
    std::vector<AST_comprehension*> generators;
    AST_expr* key, *value;

    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);

    AST_DictComp() : AST_expr(AST_TYPE::DictComp) {}

    const static AST_TYPE::AST_TYPE TYPE = AST_TYPE::DictComp;
};

388 389 390 391 392 393 394 395 396 397 398
class AST_Delete : public AST_stmt {
public:
    std::vector<AST_expr*> targets;
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);

    AST_Delete() : AST_stmt(AST_TYPE::Delete) {};

    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Delete;
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
399
class AST_Expr : public AST_stmt {
400 401
public:
    AST_expr* value;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
402

403 404
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
405

406
    AST_Expr() : AST_stmt(AST_TYPE::Expr) {}
407

408
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Expr;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
409 410
};

411 412 413 414 415
class AST_ExceptHandler : public AST {
public:
    std::vector<AST_stmt*> body;
    AST_expr* type; // can be NULL for a bare "except:" clause
    AST_expr* name; // can be NULL if the exception doesn't get a name
416

417
    virtual void accept(ASTVisitor* v);
418

419
    AST_ExceptHandler() : AST(AST_TYPE::ExceptHandler) {}
420

421
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::ExceptHandler;
422 423
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
424
class AST_For : public AST_stmt {
425 426 427
public:
    std::vector<AST_stmt*> body, orelse;
    AST_expr* target, *iter;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
428

429 430
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
431

432
    AST_For() : AST_stmt(AST_TYPE::For) {}
433

434
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::For;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
435 436 437
};

class AST_FunctionDef : public AST_stmt {
438 439 440 441 442
public:
    std::vector<AST_stmt*> body;
    std::vector<AST_expr*> decorator_list;
    std::string name;
    AST_arguments* args;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
443

444 445
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
446

447
    AST_FunctionDef() : AST_stmt(AST_TYPE::FunctionDef) {}
448

449
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::FunctionDef;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
450 451
};

452 453 454 455 456 457 458 459 460 461 462 463 464
class AST_GeneratorExp : public AST_expr {
public:
    std::vector<AST_comprehension*> generators;
    AST_expr* elt;

    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);

    AST_GeneratorExp() : AST_expr(AST_TYPE::GeneratorExp) {}

    const static AST_TYPE::AST_TYPE TYPE = AST_TYPE::GeneratorExp;
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
465
class AST_Global : public AST_stmt {
466 467
public:
    std::vector<std::string> names;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
468

469 470
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
471

472
    AST_Global() : AST_stmt(AST_TYPE::Global) {}
473

474
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Global;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
475 476 477
};

class AST_If : public AST_stmt {
478 479 480
public:
    std::vector<AST_stmt*> body, orelse;
    AST_expr* test;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
481

482 483
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
484

485
    AST_If() : AST_stmt(AST_TYPE::If) {}
486

487
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::If;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
488 489
};

490
class AST_IfExp : public AST_expr {
491 492
public:
    AST_expr* body, *test, *orelse;
493

494 495
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
496

497
    AST_IfExp() : AST_expr(AST_TYPE::IfExp) {}
498

499
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::IfExp;
500 501
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
502
class AST_Import : public AST_stmt {
503 504
public:
    std::vector<AST_alias*> names;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
505

506 507
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
508

509
    AST_Import() : AST_stmt(AST_TYPE::Import) {}
510

511
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Import;
512 513 514
};

class AST_ImportFrom : public AST_stmt {
515 516 517 518
public:
    std::string module;
    std::vector<AST_alias*> names;
    int level;
519

520 521
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
522

523
    AST_ImportFrom() : AST_stmt(AST_TYPE::ImportFrom) {}
524

525
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::ImportFrom;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
526 527 528
};

class AST_Index : public AST_expr {
529 530
public:
    AST_expr* value;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
531

532 533
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
534

535
    AST_Index() : AST_expr(AST_TYPE::Index) {}
536

537
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Index;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
538 539 540
};

class AST_keyword : public AST {
541 542 543 544
public:
    // no lineno, col_offset attributes
    AST_expr* value;
    std::string arg;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
545

546
    virtual void accept(ASTVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
547

548
    AST_keyword() : AST(AST_TYPE::keyword) {}
549

550
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::keyword;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
551 552
};

553 554 555 556 557 558 559 560 561 562 563 564 565
class AST_Lambda : public AST_expr {
public:
    AST_arguments* args;
    AST_expr* body;

    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);

    AST_Lambda() : AST_expr(AST_TYPE::Lambda) {}

    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Lambda;
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
566
class AST_List : public AST_expr {
567 568 569
public:
    std::vector<AST_expr*> elts;
    AST_TYPE::AST_TYPE ctx_type;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
570

571 572
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
573

574
    AST_List() : AST_expr(AST_TYPE::List) {}
575

576
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::List;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
577 578
};

579
class AST_ListComp : public AST_expr {
580 581 582
public:
    std::vector<AST_comprehension*> generators;
    AST_expr* elt;
583

584 585
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
586

587
    AST_ListComp() : AST_expr(AST_TYPE::ListComp) {}
588

589
    const static AST_TYPE::AST_TYPE TYPE = AST_TYPE::ListComp;
590 591
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
592
class AST_Module : public AST {
593 594 595
public:
    // no lineno, col_offset attributes
    std::vector<AST_stmt*> body;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
596

597
    virtual void accept(ASTVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
598

599
    AST_Module() : AST(AST_TYPE::Module) {}
600

601
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Module;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
602 603 604
};

class AST_Name : public AST_expr {
605 606 607
public:
    AST_TYPE::AST_TYPE ctx_type;
    std::string id;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
608

609 610
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
611

612
    AST_Name() : AST_expr(AST_TYPE::Name) {}
613

614
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Name;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
615 616 617
};

class AST_Num : public AST_expr {
618 619 620 621 622
public:
    enum NumType {
        // These values must correspond to the values in parse_ast.py
        INT = 0x10,
        FLOAT = 0x20,
623
        LONG = 0x30,
624 625 626

        // for COMPLEX, n_float is the imaginary part, real part is 0
        COMPLEX = 0x40,
627 628 629 630 631 632
    } num_type;

    union {
        int64_t n_int;
        double n_float;
    };
633
    std::string n_long;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
634

635 636
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
637

638
    AST_Num() : AST_expr(AST_TYPE::Num) {}
639

640
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Num;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
641 642
};

Marius Wachtler's avatar
Marius Wachtler committed
643
class AST_Repr : public AST_expr {
644 645
public:
    AST_expr* value;
Marius Wachtler's avatar
Marius Wachtler committed
646

647 648
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Marius Wachtler's avatar
Marius Wachtler committed
649

650
    AST_Repr() : AST_expr(AST_TYPE::Repr) {}
Marius Wachtler's avatar
Marius Wachtler committed
651

652
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Repr;
Marius Wachtler's avatar
Marius Wachtler committed
653 654
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
655
class AST_Pass : public AST_stmt {
656 657 658
public:
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
659

660
    AST_Pass() : AST_stmt(AST_TYPE::Pass) {}
661

662
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Pass;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
663 664 665
};

class AST_Print : public AST_stmt {
666 667 668 669
public:
    AST_expr* dest;
    bool nl;
    std::vector<AST_expr*> values;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
670

671 672
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
673

674
    AST_Print() : AST_stmt(AST_TYPE::Print) {}
675

676
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Print;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
677 678
};

679
class AST_Raise : public AST_stmt {
680 681 682 683 684 685
public:
    // In the python ast module, these are called "type", "inst", and "tback", respectively.
    // Renaming to arg{0..2} since I find that confusing, since they are filled in
    // sequentially rather than semantically.
    // Ie "raise Exception()" will have type==Exception(), inst==None, tback==None
    AST_expr* arg0, *arg1, *arg2;
686

687 688
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
689

690
    AST_Raise() : AST_stmt(AST_TYPE::Raise), arg0(NULL), arg1(NULL), arg2(NULL) {}
691

692
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Raise;
693 694
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
695
class AST_Return : public AST_stmt {
696 697
public:
    AST_expr* value;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
698

699 700
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
701

702
    AST_Return() : AST_stmt(AST_TYPE::Return) {}
703

704
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Return;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
705 706
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
707 708 709 710 711 712 713 714 715 716 717 718
class AST_Set : public AST_expr {
public:
    std::vector<AST_expr*> elts;

    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);

    AST_Set() : AST_expr(AST_TYPE::Set) {}

    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Set;
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
719
class AST_Slice : public AST_expr {
720 721
public:
    AST_expr* lower, *upper, *step;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
722

723 724
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
725

726
    AST_Slice() : AST_expr(AST_TYPE::Slice) {}
727

728
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Slice;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
729 730 731
};

class AST_Str : public AST_expr {
732
public:
733 734 735 736 737
    enum StrType {
        STR = 0x10,
        UNICODE = 0x20,
    } str_type;

738
    std::string s;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
739

740 741
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
742

743
    AST_Str() : AST_expr(AST_TYPE::Str) {}
744 745
    AST_Str(const std::string& s) : AST_expr(AST_TYPE::Str), str_type(STR), s(s) {}
    AST_Str(const std::string&& s) : AST_expr(AST_TYPE::Str), str_type(STR), s(std::move(s)) {}
746

747
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Str;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
748 749 750
};

class AST_Subscript : public AST_expr {
751 752 753
public:
    AST_expr* value, *slice;
    AST_TYPE::AST_TYPE ctx_type;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
754

755 756
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
757

758
    AST_Subscript() : AST_expr(AST_TYPE::Subscript) {}
759

760
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Subscript;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
761 762
};

763
class AST_TryExcept : public AST_stmt {
764 765 766
public:
    std::vector<AST_stmt*> body, orelse;
    std::vector<AST_ExceptHandler*> handlers;
767

768 769
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
770

771
    AST_TryExcept() : AST_stmt(AST_TYPE::TryExcept) {}
772

773
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::TryExcept;
774 775 776
};

class AST_TryFinally : public AST_stmt {
777 778
public:
    std::vector<AST_stmt*> body, finalbody;
779

780 781
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
782

783
    AST_TryFinally() : AST_stmt(AST_TYPE::TryFinally) {}
784

785
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::TryFinally;
786 787
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
788
class AST_Tuple : public AST_expr {
789 790 791
public:
    std::vector<AST_expr*> elts;
    AST_TYPE::AST_TYPE ctx_type;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
792

793 794
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
795

796
    AST_Tuple() : AST_expr(AST_TYPE::Tuple) {}
797

798
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Tuple;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
799 800 801
};

class AST_UnaryOp : public AST_expr {
802 803 804
public:
    AST_expr* operand;
    AST_TYPE::AST_TYPE op_type;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
805

806 807
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
808

809
    AST_UnaryOp() : AST_expr(AST_TYPE::UnaryOp) {}
810

811
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::UnaryOp;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
812 813 814
};

class AST_While : public AST_stmt {
815 816 817
public:
    AST_expr* test;
    std::vector<AST_stmt*> body, orelse;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
818

819 820
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
821

822
    AST_While() : AST_stmt(AST_TYPE::While) {}
823

824
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::While;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
825 826 827
};

class AST_With : public AST_stmt {
828 829 830
public:
    AST_expr* optional_vars, *context_expr;
    std::vector<AST_stmt*> body;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
831

832 833
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
834

835
    AST_With() : AST_stmt(AST_TYPE::With) {}
836

837
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::With;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
838 839
};

840 841 842 843 844 845 846 847 848 849 850 851
class AST_Yield : public AST_expr {
public:
    AST_expr* value;

    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);

    AST_Yield() : AST_expr(AST_TYPE::Yield) {}

    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Yield;
};

Kevin Modzelewski's avatar
Kevin Modzelewski committed
852 853 854 855 856 857 858

// AST pseudo-nodes that will get added during CFG-construction.  These don't exist in the input AST, but adding them in
// lets us avoid creating a completely new IR for this phase

class CFGBlock;

class AST_Branch : public AST_stmt {
859 860 861
public:
    AST_expr* test;
    CFGBlock* iftrue, *iffalse;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
862

863 864
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
865

866
    AST_Branch() : AST_stmt(AST_TYPE::Branch) {}
867

868
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Branch;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
869 870 871
};

class AST_Jump : public AST_stmt {
872 873
public:
    CFGBlock* target;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
874

875 876
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
877

878 879 880 881
    AST_Jump() : AST_stmt(AST_TYPE::Jump) {
        lineno = -1;
        col_offset = -1;
    }
882

883
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Jump;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
884 885 886
};

class AST_ClsAttribute : public AST_expr {
887 888 889
public:
    AST_expr* value;
    std::string attr;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
890

891 892
    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
893

894
    AST_ClsAttribute() : AST_expr(AST_TYPE::ClsAttribute) {}
895

896
    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::ClsAttribute;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
897 898
};

899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
class AST_Invoke : public AST_stmt {
public:
    AST_stmt* stmt;

    CFGBlock* normal_dest, *exc_dest;

    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);

    AST_Invoke(AST_stmt* stmt) : AST_stmt(AST_TYPE::Invoke), stmt(stmt) {}

    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Invoke;
};

// "LangPrimitive" represents operations that "primitive" to the language,
// but aren't directly *exactly* representable as normal Python.
// ClsAttribute would fall into this category, as would isinstance (which
// is not the same as the "isinstance" name since that could get redefined).
917
// These are basically bytecodes, framed as pseudo-AST-nodes.
918 919 920 921 922
class AST_LangPrimitive : public AST_expr {
public:
    enum Opcodes {
        ISINSTANCE,
        LANDINGPAD,
923
        LOCALS,
924
        GET_ITER,
925 926 927 928 929 930 931 932 933 934 935
    } opcode;
    std::vector<AST_expr*> args;

    virtual void accept(ASTVisitor* v);
    virtual void* accept_expr(ExprVisitor* v);

    AST_LangPrimitive(Opcodes opcode) : AST_expr(AST_TYPE::LangPrimitive), opcode(opcode) {}

    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::LangPrimitive;
};

936 937 938 939 940 941 942 943 944
class AST_Unreachable : public AST_stmt {
public:
    virtual void accept(ASTVisitor* v);
    virtual void accept_stmt(StmtVisitor* v);

    AST_Unreachable() : AST_stmt(AST_TYPE::Unreachable) {}

    static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Unreachable;
};
945

946
template <typename T> T* ast_cast(AST* node) {
947 948 949 950 951 952
    assert(node->type == T::TYPE);
    return static_cast<T*>(node);
}



Kevin Modzelewski's avatar
Kevin Modzelewski committed
953
class ASTVisitor {
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
protected:
public:
    virtual ~ASTVisitor() {}

    virtual bool visit_alias(AST_alias* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_arguments(AST_arguments* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_assert(AST_Assert* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_assign(AST_Assign* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_augassign(AST_AugAssign* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_augbinop(AST_AugBinOp* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_attribute(AST_Attribute* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_binop(AST_BinOp* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_boolop(AST_BoolOp* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_break(AST_Break* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_call(AST_Call* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_clsattribute(AST_ClsAttribute* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_compare(AST_Compare* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_comprehension(AST_comprehension* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_classdef(AST_ClassDef* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_continue(AST_Continue* node) { RELEASE_ASSERT(0, ""); }
974
    virtual bool visit_delete(AST_Delete* node) { RELEASE_ASSERT(0, ""); }
975
    virtual bool visit_dict(AST_Dict* node) { RELEASE_ASSERT(0, ""); }
976
    virtual bool visit_dictcomp(AST_DictComp* node) { RELEASE_ASSERT(0, ""); }
977 978 979 980
    virtual bool visit_excepthandler(AST_ExceptHandler* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_expr(AST_Expr* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_for(AST_For* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_functiondef(AST_FunctionDef* node) { RELEASE_ASSERT(0, ""); }
981
    virtual bool visit_generatorexp(AST_GeneratorExp* node) { RELEASE_ASSERT(0, ""); }
982 983 984 985 986 987
    virtual bool visit_global(AST_Global* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_if(AST_If* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_ifexp(AST_IfExp* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_import(AST_Import* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_importfrom(AST_ImportFrom* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_index(AST_Index* node) { RELEASE_ASSERT(0, ""); }
988
    virtual bool visit_invoke(AST_Invoke* node) { RELEASE_ASSERT(0, ""); }
989
    virtual bool visit_keyword(AST_keyword* node) { RELEASE_ASSERT(0, ""); }
990
    virtual bool visit_lambda(AST_Lambda* node) { RELEASE_ASSERT(0, ""); }
991
    virtual bool visit_langprimitive(AST_LangPrimitive* node) { RELEASE_ASSERT(0, ""); }
992 993 994 995 996 997 998 999 1000 1001
    virtual bool visit_list(AST_List* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_listcomp(AST_ListComp* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_module(AST_Module* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_name(AST_Name* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_num(AST_Num* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_pass(AST_Pass* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_print(AST_Print* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_raise(AST_Raise* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_repr(AST_Repr* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_return(AST_Return* node) { RELEASE_ASSERT(0, ""); }
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1002
    virtual bool visit_set(AST_Set* node) { RELEASE_ASSERT(0, ""); }
1003 1004 1005 1006 1007 1008 1009
    virtual bool visit_slice(AST_Slice* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_str(AST_Str* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_subscript(AST_Subscript* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_tryexcept(AST_TryExcept* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_tryfinally(AST_TryFinally* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_tuple(AST_Tuple* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_unaryop(AST_UnaryOp* node) { RELEASE_ASSERT(0, ""); }
1010
    virtual bool visit_unreachable(AST_Unreachable* node) { RELEASE_ASSERT(0, ""); }
1011 1012
    virtual bool visit_while(AST_While* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_with(AST_With* node) { RELEASE_ASSERT(0, ""); }
1013
    virtual bool visit_yield(AST_Yield* node) { RELEASE_ASSERT(0, ""); }
1014 1015 1016

    virtual bool visit_branch(AST_Branch* node) { RELEASE_ASSERT(0, ""); }
    virtual bool visit_jump(AST_Jump* node) { RELEASE_ASSERT(0, ""); }
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1017 1018 1019
};

class NoopASTVisitor : public ASTVisitor {
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
protected:
public:
    virtual ~NoopASTVisitor() {}

    virtual bool visit_alias(AST_alias* node) { return false; }
    virtual bool visit_arguments(AST_arguments* node) { return false; }
    virtual bool visit_assert(AST_Assert* node) { return false; }
    virtual bool visit_assign(AST_Assign* node) { return false; }
    virtual bool visit_augassign(AST_AugAssign* node) { return false; }
    virtual bool visit_augbinop(AST_AugBinOp* node) { return false; }
    virtual bool visit_attribute(AST_Attribute* node) { return false; }
    virtual bool visit_binop(AST_BinOp* node) { return false; }
    virtual bool visit_boolop(AST_BoolOp* node) { return false; }
    virtual bool visit_break(AST_Break* node) { return false; }
    virtual bool visit_call(AST_Call* node) { return false; }
    virtual bool visit_clsattribute(AST_ClsAttribute* node) { return false; }
    virtual bool visit_compare(AST_Compare* node) { return false; }
    virtual bool visit_comprehension(AST_comprehension* node) { return false; }
    virtual bool visit_classdef(AST_ClassDef* node) { return false; }
    virtual bool visit_continue(AST_Continue* node) { return false; }
1040
    virtual bool visit_delete(AST_Delete* node) { return false; }
1041
    virtual bool visit_dict(AST_Dict* node) { return false; }
1042
    virtual bool visit_dictcomp(AST_DictComp* node) { return false; }
1043 1044 1045 1046
    virtual bool visit_excepthandler(AST_ExceptHandler* node) { return false; }
    virtual bool visit_expr(AST_Expr* node) { return false; }
    virtual bool visit_for(AST_For* node) { return false; }
    virtual bool visit_functiondef(AST_FunctionDef* node) { return false; }
1047
    virtual bool visit_generatorexp(AST_GeneratorExp* node) { return false; }
1048 1049 1050 1051 1052 1053
    virtual bool visit_global(AST_Global* node) { return false; }
    virtual bool visit_if(AST_If* node) { return false; }
    virtual bool visit_ifexp(AST_IfExp* node) { return false; }
    virtual bool visit_import(AST_Import* node) { return false; }
    virtual bool visit_importfrom(AST_ImportFrom* node) { return false; }
    virtual bool visit_index(AST_Index* node) { return false; }
1054
    virtual bool visit_invoke(AST_Invoke* node) { return false; }
1055
    virtual bool visit_keyword(AST_keyword* node) { return false; }
1056
    virtual bool visit_lambda(AST_Lambda* node) { return false; }
1057
    virtual bool visit_langprimitive(AST_LangPrimitive* node) { return false; }
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
    virtual bool visit_list(AST_List* node) { return false; }
    virtual bool visit_listcomp(AST_ListComp* node) { return false; }
    virtual bool visit_module(AST_Module* node) { return false; }
    virtual bool visit_name(AST_Name* node) { return false; }
    virtual bool visit_num(AST_Num* node) { return false; }
    virtual bool visit_pass(AST_Pass* node) { return false; }
    virtual bool visit_print(AST_Print* node) { return false; }
    virtual bool visit_raise(AST_Raise* node) { return false; }
    virtual bool visit_repr(AST_Repr* node) { return false; }
    virtual bool visit_return(AST_Return* node) { return false; }
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1068
    virtual bool visit_set(AST_Set* node) { return false; }
1069 1070 1071 1072 1073 1074 1075
    virtual bool visit_slice(AST_Slice* node) { return false; }
    virtual bool visit_str(AST_Str* node) { return false; }
    virtual bool visit_subscript(AST_Subscript* node) { return false; }
    virtual bool visit_tryexcept(AST_TryExcept* node) { return false; }
    virtual bool visit_tryfinally(AST_TryFinally* node) { return false; }
    virtual bool visit_tuple(AST_Tuple* node) { return false; }
    virtual bool visit_unaryop(AST_UnaryOp* node) { return false; }
1076
    virtual bool visit_unreachable(AST_Unreachable* node) { return false; }
1077 1078
    virtual bool visit_while(AST_While* node) { return false; }
    virtual bool visit_with(AST_With* node) { return false; }
1079
    virtual bool visit_yield(AST_Yield* node) { return false; }
1080 1081 1082

    virtual bool visit_branch(AST_Branch* node) { return false; }
    virtual bool visit_jump(AST_Jump* node) { return false; }
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1083 1084 1085
};

class ExprVisitor {
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
protected:
public:
    virtual ~ExprVisitor() {}

    virtual void* visit_augbinop(AST_AugBinOp* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_attribute(AST_Attribute* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_binop(AST_BinOp* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_boolop(AST_BoolOp* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_call(AST_Call* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_clsattribute(AST_ClsAttribute* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_compare(AST_Compare* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_dict(AST_Dict* node) { RELEASE_ASSERT(0, ""); }
1098
    virtual void* visit_dictcomp(AST_DictComp* node) { RELEASE_ASSERT(0, ""); }
1099
    virtual void* visit_generatorexp(AST_GeneratorExp* node) { RELEASE_ASSERT(0, ""); }
1100 1101
    virtual void* visit_ifexp(AST_IfExp* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_index(AST_Index* node) { RELEASE_ASSERT(0, ""); }
1102
    virtual void* visit_lambda(AST_Lambda* node) { RELEASE_ASSERT(0, ""); }
1103
    virtual void* visit_langprimitive(AST_LangPrimitive* node) { RELEASE_ASSERT(0, ""); }
1104 1105 1106 1107 1108
    virtual void* visit_list(AST_List* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_listcomp(AST_ListComp* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_name(AST_Name* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_num(AST_Num* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_repr(AST_Repr* node) { RELEASE_ASSERT(0, ""); }
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1109
    virtual void* visit_set(AST_Set* node) { RELEASE_ASSERT(0, ""); }
1110 1111 1112 1113 1114
    virtual void* visit_slice(AST_Slice* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_str(AST_Str* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_subscript(AST_Subscript* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_tuple(AST_Tuple* node) { RELEASE_ASSERT(0, ""); }
    virtual void* visit_unaryop(AST_UnaryOp* node) { RELEASE_ASSERT(0, ""); }
1115
    virtual void* visit_yield(AST_Yield* node) { RELEASE_ASSERT(0, ""); }
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1116 1117 1118
};

class StmtVisitor {
1119 1120 1121 1122 1123 1124 1125 1126 1127
protected:
public:
    virtual ~StmtVisitor() {}

    virtual void visit_assert(AST_Assert* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_assign(AST_Assign* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_augassign(AST_AugAssign* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_break(AST_Break* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_classdef(AST_ClassDef* node) { RELEASE_ASSERT(0, ""); }
1128
    virtual void visit_delete(AST_Delete* node) { RELEASE_ASSERT(0, ""); }
1129 1130 1131 1132 1133 1134 1135 1136
    virtual void visit_continue(AST_Continue* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_expr(AST_Expr* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_for(AST_For* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_functiondef(AST_FunctionDef* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_global(AST_Global* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_if(AST_If* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_import(AST_Import* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_importfrom(AST_ImportFrom* node) { RELEASE_ASSERT(0, ""); }
1137
    virtual void visit_invoke(AST_Invoke* node) { RELEASE_ASSERT(0, ""); }
1138 1139 1140 1141 1142 1143
    virtual void visit_pass(AST_Pass* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_print(AST_Print* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_raise(AST_Raise* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_return(AST_Return* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_tryexcept(AST_TryExcept* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_tryfinally(AST_TryFinally* node) { RELEASE_ASSERT(0, ""); }
1144
    virtual void visit_unreachable(AST_Unreachable* node) { RELEASE_ASSERT(0, ""); }
1145 1146 1147 1148 1149
    virtual void visit_while(AST_While* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_with(AST_With* node) { RELEASE_ASSERT(0, ""); }

    virtual void visit_branch(AST_Branch* node) { RELEASE_ASSERT(0, ""); }
    virtual void visit_jump(AST_Jump* node) { RELEASE_ASSERT(0, ""); }
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1150 1151
};

1152
void print_ast(AST* ast);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1153
class PrintVisitor : public ASTVisitor {
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
private:
    int indent;
    void printIndent();

public:
    PrintVisitor(int indent = 0) : indent(indent) {}
    virtual ~PrintVisitor() {}

    virtual bool visit_alias(AST_alias* node);
    virtual bool visit_arguments(AST_arguments* node);
    virtual bool visit_assert(AST_Assert* node);
    virtual bool visit_assign(AST_Assign* node);
    virtual bool visit_augassign(AST_AugAssign* node);
    virtual bool visit_augbinop(AST_AugBinOp* node);
    virtual bool visit_attribute(AST_Attribute* node);
    virtual bool visit_binop(AST_BinOp* node);
    virtual bool visit_boolop(AST_BoolOp* node);
    virtual bool visit_break(AST_Break* node);
    virtual bool visit_call(AST_Call* node);
    virtual bool visit_compare(AST_Compare* node);
    virtual bool visit_comprehension(AST_comprehension* node);
    virtual bool visit_classdef(AST_ClassDef* node);
    virtual bool visit_clsattribute(AST_ClsAttribute* node);
    virtual bool visit_continue(AST_Continue* node);
1178
    virtual bool visit_delete(AST_Delete* node);
1179
    virtual bool visit_dict(AST_Dict* node);
1180
    virtual bool visit_dictcomp(AST_DictComp* node);
1181 1182 1183 1184
    virtual bool visit_excepthandler(AST_ExceptHandler* node);
    virtual bool visit_expr(AST_Expr* node);
    virtual bool visit_for(AST_For* node);
    virtual bool visit_functiondef(AST_FunctionDef* node);
1185
    virtual bool visit_generatorexp(AST_GeneratorExp* node);
1186 1187 1188 1189 1190 1191
    virtual bool visit_global(AST_Global* node);
    virtual bool visit_if(AST_If* node);
    virtual bool visit_ifexp(AST_IfExp* node);
    virtual bool visit_import(AST_Import* node);
    virtual bool visit_importfrom(AST_ImportFrom* node);
    virtual bool visit_index(AST_Index* node);
1192
    virtual bool visit_invoke(AST_Invoke* node);
1193
    virtual bool visit_keyword(AST_keyword* node);
1194
    virtual bool visit_lambda(AST_Lambda* node);
1195
    virtual bool visit_langprimitive(AST_LangPrimitive* node);
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
    virtual bool visit_list(AST_List* node);
    virtual bool visit_listcomp(AST_ListComp* node);
    virtual bool visit_module(AST_Module* node);
    virtual bool visit_name(AST_Name* node);
    virtual bool visit_num(AST_Num* node);
    virtual bool visit_pass(AST_Pass* node);
    virtual bool visit_print(AST_Print* node);
    virtual bool visit_raise(AST_Raise* node);
    virtual bool visit_repr(AST_Repr* node);
    virtual bool visit_return(AST_Return* node);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1206
    virtual bool visit_set(AST_Set* node);
1207 1208 1209 1210 1211 1212 1213
    virtual bool visit_slice(AST_Slice* node);
    virtual bool visit_str(AST_Str* node);
    virtual bool visit_subscript(AST_Subscript* node);
    virtual bool visit_tuple(AST_Tuple* node);
    virtual bool visit_tryexcept(AST_TryExcept* node);
    virtual bool visit_tryfinally(AST_TryFinally* node);
    virtual bool visit_unaryop(AST_UnaryOp* node);
1214
    virtual bool visit_unreachable(AST_Unreachable* node);
1215 1216
    virtual bool visit_while(AST_While* node);
    virtual bool visit_with(AST_With* node);
1217
    virtual bool visit_yield(AST_Yield* node);
1218 1219 1220

    virtual bool visit_branch(AST_Branch* node);
    virtual bool visit_jump(AST_Jump* node);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1221 1222 1223 1224 1225
};

// Given an AST node, return a vector of the node plus all its descendents.
// This is useful for analyses that care more about the constituent nodes than the
// exact tree structure; ex, finding all "global" directives.
1226 1227
void flatten(const std::vector<AST_stmt*>& roots, std::vector<AST*>& output, bool expand_scopes);
void flatten(AST_expr* root, std::vector<AST*>& output, bool expand_scopes);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1228
// Similar to the flatten() function, but filters for a specific type of ast nodes:
1229
template <class T, class R> void findNodes(const R& roots, std::vector<T*>& output, bool expand_scopes) {
1230 1231 1232
    std::vector<AST*> flattened;
    flatten(roots, flattened, expand_scopes);
    for (AST* n : flattened) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1233
        if (n->type == T::TYPE)
1234
            output.push_back(reinterpret_cast<T*>(n));
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1235 1236
    }
}
1237

1238
llvm::StringRef getOpSymbol(int op_type);
1239 1240 1241 1242
const std::string& getOpName(int op_type);
std::string getReverseOpName(int op_type);
std::string getInplaceOpName(int op_type);
std::string getInplaceOpSymbol(int op_type);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1243 1244 1245
};

#endif