ASTHelpers.h
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * C++ Insights, copyright (C) by Andreas Fertig
4 * Distributed under an MIT license. See LICENSE for details
5 *
6 ****************************************************************************/
7
8#ifndef INSIGHTS_AST_HELPERS_H
9#define INSIGHTS_AST_HELPERS_H
10//-----------------------------------------------------------------------------
11
12#include <array>
13#include <span>
14#include <string_view>
15#include <vector>
16
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/ExprCXX.h"
19#include "llvm/ADT/SmallVector.h"
20
21#include "InsightsStrongTypes.h"
22//-----------------------------------------------------------------------------
23
25void ReplaceNode(Stmt* parent, Stmt* oldNode, Stmt* newNode);
26
27using params_vector = std::vector<std::pair<std::string_view, QualType>>;
28using params_store = std::vector<std::pair<std::string, QualType>>;
29
31
32QualType GetRecordDeclType(const CXXMethodDecl* md);
33QualType GetRecordDeclType(const RecordDecl* rd);
34
35DeclRefExpr* mkVarDeclRefExpr(std::string_view name, QualType type);
36
38STRONG_BOOL(AsReference);
39
40CallExpr* CallConstructor(QualType ctorType,
41 QualType lhsType,
42 const FieldDecl* fieldDecl,
43 ArrayRef<Expr*> callParams,
44 DoCast doCast = DoCast::No,
45 AsReference asReference = AsReference::No);
46
47CallExpr* CallConstructor(QualType ctorType,
48 const VarDecl* fieldDecl,
49 ArrayRef<Expr*> callParams,
50 DoCast doCast = DoCast::No,
51 AsReference asReference = AsReference::No);
52
53CXXBoolLiteralExpr* Bool(bool b);
54CallExpr* CallDestructor(const VarDecl* fieldDecl);
55CXXNewExpr* New(ArrayRef<Expr*> placementArgs, const Expr* expr, QualType t);
56BinaryOperator* Mul(Expr* lhs, Expr* rhs);
57BinaryOperator* And(VarDecl* lhs, Expr* rhs);
58QualType Typedef(std::string_view name, QualType underlayingType);
59
60SmallVector<Expr*, 5> ArgsToExprVector(const Expr* expr);
61
62///! A helper type to have a container for ArrayRef
64{
65 SmallVector<Stmt*, 64> mStmts{};
66
67 StmtsContainer() = default;
68 StmtsContainer(std::initializer_list<const Stmt*> stmts)
69 {
70 for(const auto& stmt : stmts) {
71 Add(stmt);
72 }
73 }
74
75 void clear() { mStmts.clear(); }
76
77 void Add(const Stmt* stmt)
78 {
79 if(stmt) {
80 mStmts.push_back(const_cast<Stmt*>(stmt));
81 }
82 }
83
84 void AddBodyStmts(Stmt* body);
85
86 operator ArrayRef<Stmt*>() { return mStmts; }
87};
88//-----------------------------------------------------------------------------
89
90template<typename... Dcls>
91DeclStmt* mkDeclStmt(Dcls... dcls)
92{
93 std::array<Decl*, sizeof...(dcls)> decls{dcls...};
94
95 DeclStmt* _mkDeclStmt(std::span<Decl*> decls);
96 return _mkDeclStmt(decls);
97}
98
99Stmt* Comment(std::string_view comment);
100VarDecl* Variable(std::string_view name, QualType type, DeclContext* dc = nullptr);
101CXXRecordDecl* Struct(std::string_view name);
102ReturnStmt* Return(Expr* stmt = nullptr);
103ReturnStmt* Return(const ValueDecl* stmt);
104
105CompoundStmt* mkCompoundStmt(ArrayRef<Stmt*> bodyStmts, SourceLocation beginLoc = {}, SourceLocation endLoc = {});
106DeclRefExpr* mkDeclRefExpr(const ValueDecl* vd);
107NullStmt* mkNullStmt();
108FieldDecl* mkFieldDecl(DeclContext* dc, std::string_view name, QualType type);
109
110ParenExpr* Paren(Expr*);
111QualType ContantArrayTy(QualType t, int size);
112InitListExpr* InitList(ArrayRef<Expr*> initExprs, QualType t);
113ArraySubscriptExpr* ArraySubscript(const Expr* lhs, uint64_t index, QualType type);
114MemberExpr* AccessMember(const Expr* expr, const ValueDecl* vd, bool isArrow = true);
115CXXMemberCallExpr* CallMemberFun(Expr* memExpr, QualType retType);
116ImplicitCastExpr* CastLToRValue(const VarDecl* vd);
117FunctionDecl* Function(std::string_view name, QualType returnType, const params_vector& parameters);
118ParmVarDecl* Parameter(const FunctionDecl* fd, std::string_view name, QualType type);
119BinaryOperator* Assign(DeclRefExpr* declRef, ValueDecl* field, Expr* assignExpr);
120BinaryOperator* Assign(MemberExpr* me, ValueDecl* field, Expr* assignExpr);
121BinaryOperator* Assign(DeclRefExpr* declRef, Expr* assignExpr);
122BinaryOperator* Assign(const VarDecl* var, Expr* assignExpr);
123BinaryOperator* Assign(UnaryOperator* var, Expr* assignExpr);
124BinaryOperator* Assign(Expr* var, Expr* assignExpr);
125BinaryOperator* Equal(Expr* var, Expr* assignExpr);
126BinaryOperator* Plus(Expr* var, Expr* assignExpr);
127CXXReinterpretCastExpr* ReinterpretCast(QualType toType, const Expr* toExpr, bool makePointer = false);
128CXXStaticCastExpr* StaticCast(QualType toType, const Expr* toExpr, bool makePointer = false);
129CXXStaticCastExpr* CastToVoidFunPtr(std::string_view name);
130CXXStaticCastExpr* Cast(const Expr* toExpr, QualType toType);
131IntegerLiteral* Int32(uint64_t value);
132IfStmt* If(const Expr* condition, ArrayRef<Stmt*> bodyStmts);
133SwitchStmt* Switch(Expr* stmt);
134CaseStmt* Case(int value, Stmt* stmt);
135BreakStmt* Break();
136LabelStmt* Label(std::string_view name);
137GotoStmt* Goto(std::string_view labelName);
138UnaryOperator* Not(const Expr* stmt);
139UnaryOperator* Ref(const Expr* e);
140UnaryOperator* Ref(const ValueDecl* d);
141UnaryOperator* Dref(const Expr* stmt);
142UnaryOperator* AddrOf(const Expr* stmt);
143CallExpr* Call(const FunctionDecl* fd, ArrayRef<Expr*> params);
144CallExpr* Call(MemberExpr* fd, ArrayRef<Expr*> params);
145CallExpr* Call(std::string_view name, ArrayRef<Expr*> args);
146CXXTryStmt* Try(const Stmt* tryBody, CXXCatchStmt* catchAllBody);
147CXXCatchStmt* Catch(Stmt* body);
148CXXCatchStmt* Catch(ArrayRef<Stmt*> body);
149CXXThrowExpr* Throw(const Expr* expr = nullptr);
150UnaryExprOrTypeTraitExpr* Sizeof(QualType toType);
151QualType Ptr(QualType srcType);
152CanQualType VoidTy();
153
154} // namespace clang::insights::asthelpers
155
156#endif /* INSIGHTS_AST_HELPERS_H */
#define STRONG_BOOL(typeName)
A more than simple typsafe bool.
BinaryOperator * Equal(Expr *var, Expr *assignExpr)
UnaryOperator * AddrOf(const Expr *stmt)
DeclStmt * mkDeclStmt(Dcls... dcls)
Definition ASTHelpers.h:91
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 &params)
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
Definition ASTHelpers.h:27
DeclStmt * _mkDeclStmt(std::span< Decl * > decls)
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)
static CallExpr * CallConstructor(QualType ctorType, DeclRefExpr *lhsDeclRef, Expr *lhsMemberExpr, ArrayRef< Expr * > callParams, DoCast doCast, AsReference asReference)
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)
LabelStmt * Label(std::string_view name)
std::vector< std::pair< std::string, QualType > > params_store
Definition ASTHelpers.h:28
UnaryOperator * Not(const Expr *stmt)
CXXBoolLiteralExpr * Bool(bool b)
FunctionDecl * Function(std::string_view name, QualType returnType, const params_vector &parameters)
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
Definition ASTHelpers.h:64
StmtsContainer(std::initializer_list< const Stmt * > stmts)
Definition ASTHelpers.h:68