aboutsummaryrefslogtreecommitdiffstats
path: root/slang_rs_ast_replace.h
blob: f93171deb88c9bfe85b8c7c577d697f775b0b8bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
/*
 * Copyright 2011, The Android Open Source Project
 *
 * 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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_AST_REPLACE_H_  // NOLINT
#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_AST_REPLACE_H_

#include "clang/AST/StmtVisitor.h"

#include "slang_assert.h"
#include "clang/AST/ASTContext.h"

namespace clang {
  class Diagnostic;
  class Expr;
  class Stmt;
}

namespace slang {

class RSASTReplace : public clang::StmtVisitor<RSASTReplace> {
 private:
  clang::ASTContext &C;
  clang::Stmt *mOuterStmt;
  clang::Stmt *mOldStmt;
  clang::Stmt *mNewStmt;
  clang::Expr *mOldExpr;
  clang::Expr *mNewExpr;

  inline bool matchesExpr(const clang::Expr *E) const {
    bool retVal = mOldExpr && (mOldExpr == E);
    if (retVal) {
      slangAssert(mNewExpr &&
          "Cannot replace an expression if we don't have a new expression");
    }
    return retVal;
  }

  inline bool matchesStmt(const clang::Stmt *S) const {
    slangAssert(mOldStmt);
    return mOldStmt == S;
  }

  void ReplaceInCompoundStmt(clang::CompoundStmt *CS);

 public:
  explicit RSASTReplace(clang::ASTContext &Con)
      : C(Con),
        mOuterStmt(nullptr),
        mOldStmt(nullptr),
        mNewStmt(nullptr),
        mOldExpr(nullptr),
        mNewExpr(nullptr) {
  }

  void VisitStmt(clang::Stmt *S);
  void VisitCompoundStmt(clang::CompoundStmt *CS);
  void VisitCaseStmt(clang::CaseStmt *CS);
  void VisitDefaultStmt(clang::DefaultStmt *DS);
  void VisitDoStmt(clang::DoStmt *DS);
  void VisitForStmt(clang::ForStmt *FS);
  void VisitIfStmt(clang::IfStmt *IS);
  void VisitSwitchCase(clang::SwitchCase *SC);
  void VisitSwitchStmt(clang::SwitchStmt *SS);
  void VisitWhileStmt(clang::WhileStmt *WS);

  // Replace all instances of OldStmt in OuterStmt with NewStmt.
  void ReplaceStmt(
      clang::Stmt *OuterStmt,
      clang::Stmt *OldStmt,
      clang::Stmt *NewStmt);
};

}  // namespace slang

#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_AST_REPLACE_H_  NOLINT