Skip to content

Commit 4459e00

Browse files
committed
Added Cfront mode, an educational C++ to C transformation.
1 parent 069207d commit 4459e00

File tree

132 files changed

+7149
-1016
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+7149
-1016
lines changed

Diff for: ASTHelpers.cpp

+696
Large diffs are not rendered by default.

Diff for: ASTHelpers.h

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
24+
namespace clang::insights::asthelpers {
25+
void ReplaceNode(Stmt* parent, Stmt* oldNode, Stmt* newNode);
26+
27+
using params_vector = std::vector<std::pair<std::string_view, QualType>>;
28+
using params_store = std::vector<std::pair<std::string, QualType>>;
29+
30+
params_vector to_params_view(params_store& params);
31+
32+
QualType GetRecordDeclType(const CXXMethodDecl* md);
33+
QualType GetRecordDeclType(const RecordDecl* rd);
34+
35+
DeclRefExpr* mkVarDeclRefExpr(std::string_view name, QualType type);
36+
37+
STRONG_BOOL(DoCast);
38+
STRONG_BOOL(AsReference);
39+
40+
CallExpr* 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+
47+
CallExpr* CallConstructor(QualType ctorType,
48+
const VarDecl* fieldDecl,
49+
ArrayRef<Expr*> callParams,
50+
DoCast doCast = DoCast::No,
51+
AsReference asReference = AsReference::No);
52+
53+
CXXBoolLiteralExpr* Bool(bool b);
54+
CallExpr* CallDestructor(const VarDecl* fieldDecl);
55+
CXXNewExpr* New(ArrayRef<Expr*> placementArgs, const Expr* expr, QualType t);
56+
BinaryOperator* Mul(Expr* lhs, Expr* rhs);
57+
QualType Typedef(std::string_view name, QualType underlayingType);
58+
59+
SmallVector<Expr*, 5> ArgsToExprVector(const Expr* expr);
60+
61+
///! A helper type to have a container for ArrayRef
62+
struct StmtsContainer
63+
{
64+
SmallVector<Stmt*, 64> mStmts{};
65+
66+
StmtsContainer() = default;
67+
StmtsContainer(std::initializer_list<const Stmt*> stmts)
68+
{
69+
for(const auto& stmt : stmts) {
70+
Add(stmt);
71+
}
72+
}
73+
74+
void clear() { mStmts.clear(); }
75+
76+
void Add(const Stmt* stmt)
77+
{
78+
if(stmt) {
79+
mStmts.push_back(const_cast<Stmt*>(stmt));
80+
}
81+
}
82+
83+
void AddBodyStmts(Stmt* body);
84+
85+
operator ArrayRef<Stmt*>() { return mStmts; }
86+
};
87+
//-----------------------------------------------------------------------------
88+
89+
template<typename... Dcls>
90+
DeclStmt* mkDeclStmt(Dcls... dcls)
91+
{
92+
std::array<Decl*, sizeof...(dcls)> decls{dcls...};
93+
94+
DeclStmt* _mkDeclStmt(std::span<Decl*> decls);
95+
return _mkDeclStmt(decls);
96+
}
97+
98+
Stmt* Comment(std::string_view comment);
99+
VarDecl* Variable(std::string_view name, QualType type, DeclContext* dc = nullptr);
100+
CXXRecordDecl* Struct(std::string_view name);
101+
ReturnStmt* Return(Expr* stmt = nullptr);
102+
ReturnStmt* Return(const ValueDecl* stmt);
103+
104+
CompoundStmt* mkCompoundStmt(ArrayRef<Stmt*> bodyStmts, SourceLocation beginLoc = {}, SourceLocation endLoc = {});
105+
DeclRefExpr* mkDeclRefExpr(const ValueDecl* vd);
106+
NullStmt* mkNullStmt();
107+
FieldDecl* mkFieldDecl(DeclContext* dc, std::string_view name, QualType type);
108+
109+
ArraySubscriptExpr* ArraySubscript(const Expr* lhs, uint64_t index, QualType type);
110+
MemberExpr* AccessMember(const Expr* expr, const ValueDecl* vd, bool isArrow = true);
111+
CXXMemberCallExpr* CallMemberFun(Expr* memExpr, QualType retType);
112+
ImplicitCastExpr* CastLToRValue(const VarDecl* vd);
113+
FunctionDecl* Function(std::string_view name, QualType returnType, const params_vector& parameters);
114+
ParmVarDecl* Parameter(const FunctionDecl* fd, std::string_view name, QualType type);
115+
BinaryOperator* Assign(DeclRefExpr* declRef, ValueDecl* field, Expr* assignExpr);
116+
BinaryOperator* Assign(MemberExpr* me, ValueDecl* field, Expr* assignExpr);
117+
BinaryOperator* Assign(DeclRefExpr* declRef, Expr* assignExpr);
118+
BinaryOperator* Assign(const VarDecl* var, Expr* assignExpr);
119+
BinaryOperator* Assign(UnaryOperator* var, Expr* assignExpr);
120+
BinaryOperator* Assign(Expr* var, Expr* assignExpr);
121+
BinaryOperator* Equal(Expr* var, Expr* assignExpr);
122+
CXXStaticCastExpr* StaticCast(QualType toType, const Expr* toExpr, bool makePointer = false);
123+
CXXStaticCastExpr* CastToVoidFunPtr(std::string_view name);
124+
CXXStaticCastExpr* Cast(const Expr* toExpr, QualType toType);
125+
IntegerLiteral* Int32(uint64_t value);
126+
IfStmt* If(const Expr* condition, ArrayRef<Stmt*> bodyStmts);
127+
SwitchStmt* Switch(Expr* stmt);
128+
CaseStmt* Case(int value, Stmt* stmt);
129+
BreakStmt* Break();
130+
LabelStmt* Label(std::string_view name);
131+
GotoStmt* Goto(std::string_view labelName);
132+
UnaryOperator* Not(const Expr* stmt);
133+
UnaryOperator* Not(const VarDecl* stmt);
134+
UnaryOperator* Ref(const Expr* e);
135+
UnaryOperator* Ref(const ValueDecl* d);
136+
UnaryOperator* Dref(const Expr* stmt);
137+
UnaryOperator* AddrOf(const Expr* stmt);
138+
CallExpr* Call(const FunctionDecl* fd, ArrayRef<Expr*> params);
139+
CallExpr* Call(std::string_view name, ArrayRef<Expr*> args);
140+
CXXTryStmt* Try(const Stmt* tryBody, CXXCatchStmt* catchAllBody);
141+
CXXCatchStmt* Catch(Stmt* body);
142+
CXXCatchStmt* Catch(ArrayRef<Stmt*> body);
143+
CXXThrowExpr* Throw(const Expr* expr = nullptr);
144+
UnaryExprOrTypeTraitExpr* Sizeof(QualType toType);
145+
QualType Ptr(QualType srcType);
146+
CanQualType VoidTy();
147+
148+
} // namespace clang::insights::asthelpers
149+
150+
#endif /* INSIGHTS_AST_HELPERS_H */

Diff for: CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,17 @@ endif()
496496

497497
# name the executable and all source files
498498
add_clang_tool(insights
499+
ASTHelpers.cpp
499500
CodeGenerator.cpp
501+
CfrontCodeGenerator.cpp
500502
CoroutinesCodeGenerator.cpp
501503
DPrint.cpp
502504
FunctionDeclHandler.cpp
503505
GlobalVariableHandler.cpp
504506
Insights.cpp
505507
InsightsBase.cpp
506508
InsightsHelpers.cpp
509+
LifetimeTracker.cpp
507510
OutputFormatHelper.cpp
508511
RecordDeclHandler.cpp
509512
TemplateHandler.cpp

0 commit comments

Comments
 (0)