8 #ifndef INSIGHTS_AST_HELPERS_H
9 #define INSIGHTS_AST_HELPERS_H
14 #include <string_view>
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/ExprCXX.h"
19 #include "llvm/ADT/SmallVector.h"
27 using params_vector = std::vector<std::pair<std::string_view, QualType>>;
28 using params_store = std::vector<std::pair<std::string, QualType>>;
42 const FieldDecl* fieldDecl,
43 ArrayRef<Expr*> callParams,
44 DoCast doCast = DoCast::No,
45 AsReference asReference = AsReference::No);
48 const VarDecl* fieldDecl,
49 ArrayRef<Expr*> callParams,
50 DoCast doCast = DoCast::No,
51 AsReference asReference = AsReference::No);
53 CXXBoolLiteralExpr*
Bool(
bool b);
55 CXXNewExpr*
New(ArrayRef<Expr*> placementArgs,
const Expr* expr, QualType t);
56 BinaryOperator*
Mul(Expr* lhs, Expr* rhs);
57 BinaryOperator*
And(VarDecl* lhs, Expr* rhs);
58 QualType
Typedef(std::string_view name, QualType underlayingType);
70 for(
const auto& stmt : stmts) {
86 operator ArrayRef<Stmt*>() {
return mStmts; }
90 template<
typename... Dcls>
93 std::array<Decl*,
sizeof...(dcls)> decls{dcls...};
100 VarDecl*
Variable(std::string_view name, QualType type, DeclContext* dc =
nullptr);
101 CXXRecordDecl*
Struct(std::string_view name);
102 ReturnStmt*
Return(Expr* stmt =
nullptr);
103 ReturnStmt*
Return(
const ValueDecl* stmt);
105 CompoundStmt*
mkCompoundStmt(ArrayRef<Stmt*> bodyStmts, SourceLocation beginLoc = {}, SourceLocation endLoc = {});
108 FieldDecl*
mkFieldDecl(DeclContext* dc, std::string_view name, QualType type);
110 ParenExpr*
Paren(Expr*);
112 InitListExpr*
InitList(ArrayRef<Expr*> initExprs, QualType t);
113 ArraySubscriptExpr*
ArraySubscript(
const Expr* lhs, uint64_t index, QualType type);
114 MemberExpr*
AccessMember(
const Expr* expr,
const ValueDecl* vd,
bool isArrow =
true);
115 CXXMemberCallExpr*
CallMemberFun(Expr* memExpr, QualType retType);
118 ParmVarDecl*
Parameter(
const FunctionDecl* fd, std::string_view name, QualType type);
119 BinaryOperator*
Assign(DeclRefExpr* declRef, ValueDecl* field, Expr* assignExpr);
120 BinaryOperator*
Assign(MemberExpr* me, ValueDecl* field, Expr* assignExpr);
121 BinaryOperator*
Assign(DeclRefExpr* declRef, Expr* assignExpr);
122 BinaryOperator*
Assign(
const VarDecl* var, Expr* assignExpr);
123 BinaryOperator*
Assign(UnaryOperator* var, Expr* assignExpr);
124 BinaryOperator*
Assign(Expr* var, Expr* assignExpr);
125 BinaryOperator*
Equal(Expr* var, Expr* assignExpr);
126 BinaryOperator*
Plus(Expr* var, Expr* assignExpr);
127 CXXReinterpretCastExpr*
ReinterpretCast(QualType toType,
const Expr* toExpr,
bool makePointer =
false);
128 CXXStaticCastExpr*
StaticCast(QualType toType,
const Expr* toExpr,
bool makePointer =
false);
130 CXXStaticCastExpr*
Cast(
const Expr* toExpr, QualType toType);
131 IntegerLiteral*
Int32(uint64_t value);
132 IfStmt*
If(
const Expr* condition, ArrayRef<Stmt*> bodyStmts);
133 SwitchStmt*
Switch(Expr* stmt);
134 CaseStmt*
Case(
int value,
Stmt* stmt);
136 LabelStmt*
Label(std::string_view name);
137 GotoStmt*
Goto(std::string_view labelName);
138 UnaryOperator*
Not(
const Expr* stmt);
139 UnaryOperator*
Ref(
const Expr* e);
140 UnaryOperator*
Ref(
const ValueDecl* d);
141 UnaryOperator*
Dref(
const Expr* stmt);
142 UnaryOperator*
AddrOf(
const Expr* stmt);
143 CallExpr*
Call(
const FunctionDecl* fd, ArrayRef<Expr*> params);
144 CallExpr*
Call(MemberExpr* fd, ArrayRef<Expr*> params);
145 CallExpr*
Call(std::string_view name, ArrayRef<Expr*> args);
146 CXXTryStmt*
Try(
const Stmt* tryBody, CXXCatchStmt* catchAllBody);
148 CXXCatchStmt*
Catch(ArrayRef<Stmt*> body);
149 CXXThrowExpr*
Throw(
const Expr* expr =
nullptr);
150 UnaryExprOrTypeTraitExpr*
Sizeof(QualType toType);
151 QualType
Ptr(QualType srcType);
BinaryOperator * Equal(Expr *var, Expr *assignExpr)
UnaryOperator * AddrOf(const Expr *stmt)
BinaryOperator * Plus(Expr *var, Expr *assignExpr)
CXXReinterpretCastExpr * ReinterpretCast(QualType toType, const Expr *toExpr, bool makePointer)
CXXRecordDecl * Struct(std::string_view name)
FieldDecl * mkFieldDecl(DeclContext *dc, std::string_view name, QualType type)
params_vector to_params_view(params_store ¶ms)
DeclStmt * mkDeclStmt(Dcls... dcls)
DeclRefExpr * mkDeclRefExpr(const ValueDecl *vd)
QualType Typedef(std::string_view name, QualType underlayingType)
CXXNewExpr * New(ArrayRef< Expr * > placementArgs, const Expr *expr, QualType t)
std::vector< std::pair< std::string_view, QualType > > params_vector
CallExpr * Call(const FunctionDecl *fd, ArrayRef< Expr * > params)
UnaryOperator * Ref(const Expr *e)
VarDecl * Variable(std::string_view name, QualType type, DeclContext *dc)
MemberExpr * AccessMember(const Expr *expr, const ValueDecl *vd, bool isArrow)
DeclRefExpr * mkVarDeclRefExpr(std::string_view name, QualType type)
SmallVector< Expr *, 5 > ArgsToExprVector(const Expr *expr)
ArraySubscriptExpr * ArraySubscript(const Expr *lhs, uint64_t index, QualType type)
ReturnStmt * Return(Expr *stmt)
CXXStaticCastExpr * Cast(const Expr *toExpr, QualType toType)
CXXStaticCastExpr * CastToVoidFunPtr(std::string_view name)
CaseStmt * Case(int value, Stmt *stmt)
Stmt * Comment(std::string_view comment)
ParenExpr * Paren(Expr *expr)
IfStmt * If(const Expr *condition, ArrayRef< Stmt * > bodyStmts)
BinaryOperator * And(VarDecl *lhs, Expr *rhs)
BinaryOperator * Mul(Expr *lhs, Expr *rhs)
GotoStmt * Goto(std::string_view labelName)
InitListExpr * InitList(ArrayRef< Expr * > initExprs, QualType t)
CXXCatchStmt * Catch(ArrayRef< Stmt * > body)
CXXThrowExpr * Throw(const Expr *expr)
void ReplaceNode(Stmt *parent, Stmt *oldNode, Stmt *newNode)
UnaryExprOrTypeTraitExpr * Sizeof(QualType toType)
QualType GetRecordDeclType(const CXXMethodDecl *md)
static CallExpr * CallConstructor(QualType ctorType, DeclRefExpr *lhsDeclRef, Expr *lhsMemberExpr, ArrayRef< Expr * > callParams, DoCast doCast, AsReference asReference)
LabelStmt * Label(std::string_view name)
std::vector< std::pair< std::string, QualType > > params_store
DeclStmt * _mkDeclStmt(std::span< Decl * > decls)
UnaryOperator * Not(const Expr *stmt)
CXXBoolLiteralExpr * Bool(bool b)
FunctionDecl * Function(std::string_view name, QualType returnType, const params_vector ¶meters)
CompoundStmt * mkCompoundStmt(ArrayRef< Stmt * > bodyStmts, SourceLocation beginLoc, SourceLocation endLoc)
SwitchStmt * Switch(Expr *stmt)
IntegerLiteral * Int32(uint64_t value)
QualType ContantArrayTy(QualType t, int size)
ImplicitCastExpr * CastLToRValue(const VarDecl *vd)
ParmVarDecl * Parameter(const FunctionDecl *fd, std::string_view name, QualType type)
UnaryOperator * Dref(const Expr *stmt)
QualType Ptr(QualType srcType)
CXXStaticCastExpr * StaticCast(QualType toType, const Expr *toExpr, bool makePointer)
CXXMemberCallExpr * CallMemberFun(Expr *memExpr, QualType retType)
BinaryOperator * Assign(const VarDecl *var, Expr *assignExpr)
CXXTryStmt * Try(const Stmt *tryBody, CXXCatchStmt *catchAllBody)
CallExpr * CallDestructor(const VarDecl *varDecl)
! A helper type to have a container for ArrayRef
StmtsContainer(std::initializer_list< const Stmt * > stmts)
void Add(const Stmt *stmt)
SmallVector< Stmt *, 64 > mStmts
void AddBodyStmts(Stmt *body)