1bc324330SEugene Zelenko //===- ThreadSafetyCommon.cpp ---------------------------------------------===//
2b2213910SDeLesley Hutchins //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b2213910SDeLesley Hutchins //
7b2213910SDeLesley Hutchins //===----------------------------------------------------------------------===//
8b2213910SDeLesley Hutchins //
9b2213910SDeLesley Hutchins // Implementation of the interfaces declared in ThreadSafetyCommon.h
10b2213910SDeLesley Hutchins //
11b2213910SDeLesley Hutchins //===----------------------------------------------------------------------===//
12b2213910SDeLesley Hutchins 
1328347a72SAaron Ballman #include "clang/Analysis/Analyses/ThreadSafetyCommon.h"
14b2213910SDeLesley Hutchins #include "clang/AST/Attr.h"
15bc324330SEugene Zelenko #include "clang/AST/Decl.h"
16b2213910SDeLesley Hutchins #include "clang/AST/DeclCXX.h"
17bc324330SEugene Zelenko #include "clang/AST/DeclGroup.h"
18a7bcab75SBenjamin Kramer #include "clang/AST/DeclObjC.h"
19bc324330SEugene Zelenko #include "clang/AST/Expr.h"
20b2213910SDeLesley Hutchins #include "clang/AST/ExprCXX.h"
21bc324330SEugene Zelenko #include "clang/AST/OperationKinds.h"
22bc324330SEugene Zelenko #include "clang/AST/Stmt.h"
23bc324330SEugene Zelenko #include "clang/AST/Type.h"
24f7813c56SDeLesley Hutchins #include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
25b2213910SDeLesley Hutchins #include "clang/Analysis/CFG.h"
26bc324330SEugene Zelenko #include "clang/Basic/LLVM.h"
27b2213910SDeLesley Hutchins #include "clang/Basic/OperatorKinds.h"
28bc324330SEugene Zelenko #include "clang/Basic/Specifiers.h"
2961cdaf66SSimon Pilgrim #include "llvm/ADT/StringExtras.h"
30b2213910SDeLesley Hutchins #include "llvm/ADT/StringRef.h"
31bc324330SEugene Zelenko #include "llvm/Support/Casting.h"
32aab9aff0SDeLesley Hutchins #include <algorithm>
33bc324330SEugene Zelenko #include <cassert>
34bc324330SEugene Zelenko #include <string>
35bc324330SEugene Zelenko #include <utility>
36dcfba334SHans Wennborg 
3766a97ee9SBenjamin Kramer using namespace clang;
3866a97ee9SBenjamin Kramer using namespace threadSafety;
39b2213910SDeLesley Hutchins 
40f4b5e7c6SDeLesley Hutchins // From ThreadSafetyUtil.h
getSourceLiteralString(const Expr * CE)41bc324330SEugene Zelenko std::string threadSafety::getSourceLiteralString(const Expr *CE) {
42f4b5e7c6SDeLesley Hutchins   switch (CE->getStmtClass()) {
43f4b5e7c6SDeLesley Hutchins     case Stmt::IntegerLiteralClass:
4461cdaf66SSimon Pilgrim       return toString(cast<IntegerLiteral>(CE)->getValue(), 10, true);
45f4b5e7c6SDeLesley Hutchins     case Stmt::StringLiteralClass: {
46f4b5e7c6SDeLesley Hutchins       std::string ret("\"");
47f4b5e7c6SDeLesley Hutchins       ret += cast<StringLiteral>(CE)->getString();
48f4b5e7c6SDeLesley Hutchins       ret += "\"";
49f4b5e7c6SDeLesley Hutchins       return ret;
50f4b5e7c6SDeLesley Hutchins     }
51f4b5e7c6SDeLesley Hutchins     case Stmt::CharacterLiteralClass:
52f4b5e7c6SDeLesley Hutchins     case Stmt::CXXNullPtrLiteralExprClass:
53f4b5e7c6SDeLesley Hutchins     case Stmt::GNUNullExprClass:
54f4b5e7c6SDeLesley Hutchins     case Stmt::CXXBoolLiteralExprClass:
55f4b5e7c6SDeLesley Hutchins     case Stmt::FloatingLiteralClass:
56f4b5e7c6SDeLesley Hutchins     case Stmt::ImaginaryLiteralClass:
57f4b5e7c6SDeLesley Hutchins     case Stmt::ObjCStringLiteralClass:
58f4b5e7c6SDeLesley Hutchins     default:
59f4b5e7c6SDeLesley Hutchins       return "#lit";
60f4b5e7c6SDeLesley Hutchins   }
61f4b5e7c6SDeLesley Hutchins }
62f4b5e7c6SDeLesley Hutchins 
63f8b412adSDeLesley Hutchins // Return true if E is a variable that points to an incomplete Phi node.
isIncompletePhi(const til::SExpr * E)6466a97ee9SBenjamin Kramer static bool isIncompletePhi(const til::SExpr *E) {
6566a97ee9SBenjamin Kramer   if (const auto *Ph = dyn_cast<til::Phi>(E))
6666a97ee9SBenjamin Kramer     return Ph->status() == til::Phi::PH_Incomplete;
67f8b412adSDeLesley Hutchins   return false;
68f8b412adSDeLesley Hutchins }
69f8b412adSDeLesley Hutchins 
70bc324330SEugene Zelenko using CallingContext = SExprBuilder::CallingContext;
71b2213910SDeLesley Hutchins 
lookupStmt(const Stmt * S)72b2213910SDeLesley Hutchins til::SExpr *SExprBuilder::lookupStmt(const Stmt *S) {
73aab9aff0SDeLesley Hutchins   auto It = SMap.find(S);
74aab9aff0SDeLesley Hutchins   if (It != SMap.end())
75b2213910SDeLesley Hutchins     return It->second;
763f993c13SAaron Ballman   return nullptr;
77b2213910SDeLesley Hutchins }
78b2213910SDeLesley Hutchins 
buildCFG(CFGWalker & Walker)79aab9aff0SDeLesley Hutchins til::SCFG *SExprBuilder::buildCFG(CFGWalker &Walker) {
80aab9aff0SDeLesley Hutchins   Walker.walk(*this);
81aab9aff0SDeLesley Hutchins   return Scfg;
82b2213910SDeLesley Hutchins }
83b2213910SDeLesley Hutchins 
isCalleeArrow(const Expr * E)8466a97ee9SBenjamin Kramer static bool isCalleeArrow(const Expr *E) {
85bc324330SEugene Zelenko   const auto *ME = dyn_cast<MemberExpr>(E->IgnoreParenCasts());
86ea1f8338SDeLesley Hutchins   return ME ? ME->isArrow() : false;
87ea1f8338SDeLesley Hutchins }
88ea1f8338SDeLesley Hutchins 
ClassifyDiagnostic(const CapabilityAttr * A)89*f8afb8fdSAaron Puchert static StringRef ClassifyDiagnostic(const CapabilityAttr *A) {
90*f8afb8fdSAaron Puchert   return A->getName();
91*f8afb8fdSAaron Puchert }
92*f8afb8fdSAaron Puchert 
ClassifyDiagnostic(QualType VDT)93*f8afb8fdSAaron Puchert static StringRef ClassifyDiagnostic(QualType VDT) {
94*f8afb8fdSAaron Puchert   // We need to look at the declaration of the type of the value to determine
95*f8afb8fdSAaron Puchert   // which it is. The type should either be a record or a typedef, or a pointer
96*f8afb8fdSAaron Puchert   // or reference thereof.
97*f8afb8fdSAaron Puchert   if (const auto *RT = VDT->getAs<RecordType>()) {
98*f8afb8fdSAaron Puchert     if (const auto *RD = RT->getDecl())
99*f8afb8fdSAaron Puchert       if (const auto *CA = RD->getAttr<CapabilityAttr>())
100*f8afb8fdSAaron Puchert         return ClassifyDiagnostic(CA);
101*f8afb8fdSAaron Puchert   } else if (const auto *TT = VDT->getAs<TypedefType>()) {
102*f8afb8fdSAaron Puchert     if (const auto *TD = TT->getDecl())
103*f8afb8fdSAaron Puchert       if (const auto *CA = TD->getAttr<CapabilityAttr>())
104*f8afb8fdSAaron Puchert         return ClassifyDiagnostic(CA);
105*f8afb8fdSAaron Puchert   } else if (VDT->isPointerType() || VDT->isReferenceType())
106*f8afb8fdSAaron Puchert     return ClassifyDiagnostic(VDT->getPointeeType());
107*f8afb8fdSAaron Puchert 
108*f8afb8fdSAaron Puchert   return "mutex";
109*f8afb8fdSAaron Puchert }
110*f8afb8fdSAaron Puchert 
1119fc8faf9SAdrian Prantl /// Translate a clang expression in an attribute to a til::SExpr.
112ea1f8338SDeLesley Hutchins /// Constructs the context from D, DeclExp, and SelfDecl.
113ea1f8338SDeLesley Hutchins ///
114ea1f8338SDeLesley Hutchins /// \param AttrExp The expression to translate.
115ea1f8338SDeLesley Hutchins /// \param D       The declaration to which the attribute is attached.
116ea1f8338SDeLesley Hutchins /// \param DeclExp An expression involving the Decl to which the attribute
117ea1f8338SDeLesley Hutchins ///                is attached.  E.g. the call to a function.
translateAttrExpr(const Expr * AttrExp,const NamedDecl * D,const Expr * DeclExp,VarDecl * SelfDecl)1184266522aSDeLesley Hutchins CapabilityExpr SExprBuilder::translateAttrExpr(const Expr *AttrExp,
119ea1f8338SDeLesley Hutchins                                                const NamedDecl *D,
120ea1f8338SDeLesley Hutchins                                                const Expr *DeclExp,
121ea1f8338SDeLesley Hutchins                                                VarDecl *SelfDecl) {
122ea1f8338SDeLesley Hutchins   // If we are processing a raw attribute expression, with no substitutions.
123ea1f8338SDeLesley Hutchins   if (!DeclExp)
124ea1f8338SDeLesley Hutchins     return translateAttrExpr(AttrExp, nullptr);
125ea1f8338SDeLesley Hutchins 
126ea1f8338SDeLesley Hutchins   CallingContext Ctx(nullptr, D);
127ea1f8338SDeLesley Hutchins 
128ea1f8338SDeLesley Hutchins   // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute
129ea1f8338SDeLesley Hutchins   // for formal parameters when we call buildMutexID later.
130bc324330SEugene Zelenko   if (const auto *ME = dyn_cast<MemberExpr>(DeclExp)) {
131ea1f8338SDeLesley Hutchins     Ctx.SelfArg   = ME->getBase();
132ea1f8338SDeLesley Hutchins     Ctx.SelfArrow = ME->isArrow();
133bc324330SEugene Zelenko   } else if (const auto *CE = dyn_cast<CXXMemberCallExpr>(DeclExp)) {
134ea1f8338SDeLesley Hutchins     Ctx.SelfArg   = CE->getImplicitObjectArgument();
135ea1f8338SDeLesley Hutchins     Ctx.SelfArrow = isCalleeArrow(CE->getCallee());
136ea1f8338SDeLesley Hutchins     Ctx.NumArgs   = CE->getNumArgs();
137ea1f8338SDeLesley Hutchins     Ctx.FunArgs   = CE->getArgs();
138bc324330SEugene Zelenko   } else if (const auto *CE = dyn_cast<CallExpr>(DeclExp)) {
139ea1f8338SDeLesley Hutchins     Ctx.NumArgs = CE->getNumArgs();
140ea1f8338SDeLesley Hutchins     Ctx.FunArgs = CE->getArgs();
141bc324330SEugene Zelenko   } else if (const auto *CE = dyn_cast<CXXConstructExpr>(DeclExp)) {
142ea1f8338SDeLesley Hutchins     Ctx.SelfArg = nullptr;  // Will be set below
143ea1f8338SDeLesley Hutchins     Ctx.NumArgs = CE->getNumArgs();
144ea1f8338SDeLesley Hutchins     Ctx.FunArgs = CE->getArgs();
145ea1f8338SDeLesley Hutchins   } else if (D && isa<CXXDestructorDecl>(D)) {
146ea1f8338SDeLesley Hutchins     // There's no such thing as a "destructor call" in the AST.
147ea1f8338SDeLesley Hutchins     Ctx.SelfArg = DeclExp;
148ea1f8338SDeLesley Hutchins   }
149ea1f8338SDeLesley Hutchins 
150ea1f8338SDeLesley Hutchins   // Hack to handle constructors, where self cannot be recovered from
151ea1f8338SDeLesley Hutchins   // the expression.
152ea1f8338SDeLesley Hutchins   if (SelfDecl && !Ctx.SelfArg) {
1535fc4db75SBruno Ricci     DeclRefExpr SelfDRE(SelfDecl->getASTContext(), SelfDecl, false,
1545fc4db75SBruno Ricci                         SelfDecl->getType(), VK_LValue,
155ea1f8338SDeLesley Hutchins                         SelfDecl->getLocation());
156ea1f8338SDeLesley Hutchins     Ctx.SelfArg = &SelfDRE;
157ea1f8338SDeLesley Hutchins 
158ea1f8338SDeLesley Hutchins     // If the attribute has no arguments, then assume the argument is "this".
159ea1f8338SDeLesley Hutchins     if (!AttrExp)
160ea1f8338SDeLesley Hutchins       return translateAttrExpr(Ctx.SelfArg, nullptr);
161ea1f8338SDeLesley Hutchins     else  // For most attributes.
162ea1f8338SDeLesley Hutchins       return translateAttrExpr(AttrExp, &Ctx);
163ea1f8338SDeLesley Hutchins   }
164ea1f8338SDeLesley Hutchins 
165ea1f8338SDeLesley Hutchins   // If the attribute has no arguments, then assume the argument is "this".
166ea1f8338SDeLesley Hutchins   if (!AttrExp)
167ea1f8338SDeLesley Hutchins     return translateAttrExpr(Ctx.SelfArg, nullptr);
168ea1f8338SDeLesley Hutchins   else  // For most attributes.
169ea1f8338SDeLesley Hutchins     return translateAttrExpr(AttrExp, &Ctx);
170ea1f8338SDeLesley Hutchins }
171ea1f8338SDeLesley Hutchins 
1729fc8faf9SAdrian Prantl /// Translate a clang expression in an attribute to a til::SExpr.
173ea1f8338SDeLesley Hutchins // This assumes a CallingContext has already been created.
translateAttrExpr(const Expr * AttrExp,CallingContext * Ctx)1744266522aSDeLesley Hutchins CapabilityExpr SExprBuilder::translateAttrExpr(const Expr *AttrExp,
175ea1f8338SDeLesley Hutchins                                                CallingContext *Ctx) {
1764266522aSDeLesley Hutchins   if (!AttrExp)
177*f8afb8fdSAaron Puchert     return CapabilityExpr();
1784266522aSDeLesley Hutchins 
179bc324330SEugene Zelenko   if (const auto* SLit = dyn_cast<StringLiteral>(AttrExp)) {
180ea1f8338SDeLesley Hutchins     if (SLit->getString() == StringRef("*"))
181ea1f8338SDeLesley Hutchins       // The "*" expr is a universal lock, which essentially turns off
182ea1f8338SDeLesley Hutchins       // checks until it is removed from the lockset.
183*f8afb8fdSAaron Puchert       return CapabilityExpr(new (Arena) til::Wildcard(), StringRef("wildcard"),
184*f8afb8fdSAaron Puchert                             false);
185ea1f8338SDeLesley Hutchins     else
186ea1f8338SDeLesley Hutchins       // Ignore other string literals for now.
187*f8afb8fdSAaron Puchert       return CapabilityExpr();
1884266522aSDeLesley Hutchins   }
1894266522aSDeLesley Hutchins 
1904266522aSDeLesley Hutchins   bool Neg = false;
191bc324330SEugene Zelenko   if (const auto *OE = dyn_cast<CXXOperatorCallExpr>(AttrExp)) {
1924266522aSDeLesley Hutchins     if (OE->getOperator() == OO_Exclaim) {
1934266522aSDeLesley Hutchins       Neg = true;
1944266522aSDeLesley Hutchins       AttrExp = OE->getArg(0);
1954266522aSDeLesley Hutchins     }
1964266522aSDeLesley Hutchins   }
197bc324330SEugene Zelenko   else if (const auto *UO = dyn_cast<UnaryOperator>(AttrExp)) {
1984266522aSDeLesley Hutchins     if (UO->getOpcode() == UO_LNot) {
1994266522aSDeLesley Hutchins       Neg = true;
2004266522aSDeLesley Hutchins       AttrExp = UO->getSubExpr();
2014266522aSDeLesley Hutchins     }
202ea1f8338SDeLesley Hutchins   }
203ea1f8338SDeLesley Hutchins 
204ea1f8338SDeLesley Hutchins   til::SExpr *E = translate(AttrExp, Ctx);
205ea1f8338SDeLesley Hutchins 
2064266522aSDeLesley Hutchins   // Trap mutex expressions like nullptr, or 0.
2074266522aSDeLesley Hutchins   // Any literal value is nonsense.
2084266522aSDeLesley Hutchins   if (!E || isa<til::Literal>(E))
209*f8afb8fdSAaron Puchert     return CapabilityExpr();
210*f8afb8fdSAaron Puchert 
211*f8afb8fdSAaron Puchert   StringRef Kind = ClassifyDiagnostic(AttrExp->getType());
2124266522aSDeLesley Hutchins 
213ea1f8338SDeLesley Hutchins   // Hack to deal with smart pointers -- strip off top-level pointer casts.
214b296c64eSAaron Puchert   if (const auto *CE = dyn_cast<til::Cast>(E)) {
215ea1f8338SDeLesley Hutchins     if (CE->castOpcode() == til::CAST_objToPtr)
216*f8afb8fdSAaron Puchert       return CapabilityExpr(CE->expr(), Kind, Neg);
217ea1f8338SDeLesley Hutchins   }
218*f8afb8fdSAaron Puchert   return CapabilityExpr(E, Kind, Neg);
219ea1f8338SDeLesley Hutchins }
220ea1f8338SDeLesley Hutchins 
221b2213910SDeLesley Hutchins // Translate a clang statement or expression to a TIL expression.
222b2213910SDeLesley Hutchins // Also performs substitution of variables; Ctx provides the context.
223b2213910SDeLesley Hutchins // Dispatches on the type of S.
translate(const Stmt * S,CallingContext * Ctx)224b2213910SDeLesley Hutchins til::SExpr *SExprBuilder::translate(const Stmt *S, CallingContext *Ctx) {
225aab9aff0SDeLesley Hutchins   if (!S)
226aab9aff0SDeLesley Hutchins     return nullptr;
227aab9aff0SDeLesley Hutchins 
228b2213910SDeLesley Hutchins   // Check if S has already been translated and cached.
229b2213910SDeLesley Hutchins   // This handles the lookup of SSA names for DeclRefExprs here.
230b2213910SDeLesley Hutchins   if (til::SExpr *E = lookupStmt(S))
231b2213910SDeLesley Hutchins     return E;
232b2213910SDeLesley Hutchins 
233b2213910SDeLesley Hutchins   switch (S->getStmtClass()) {
234b2213910SDeLesley Hutchins   case Stmt::DeclRefExprClass:
235b2213910SDeLesley Hutchins     return translateDeclRefExpr(cast<DeclRefExpr>(S), Ctx);
236b2213910SDeLesley Hutchins   case Stmt::CXXThisExprClass:
237b2213910SDeLesley Hutchins     return translateCXXThisExpr(cast<CXXThisExpr>(S), Ctx);
238b2213910SDeLesley Hutchins   case Stmt::MemberExprClass:
239b2213910SDeLesley Hutchins     return translateMemberExpr(cast<MemberExpr>(S), Ctx);
240b081f44eSAaron Puchert   case Stmt::ObjCIvarRefExprClass:
241b081f44eSAaron Puchert     return translateObjCIVarRefExpr(cast<ObjCIvarRefExpr>(S), Ctx);
242b2213910SDeLesley Hutchins   case Stmt::CallExprClass:
243b2213910SDeLesley Hutchins     return translateCallExpr(cast<CallExpr>(S), Ctx);
244b2213910SDeLesley Hutchins   case Stmt::CXXMemberCallExprClass:
245b2213910SDeLesley Hutchins     return translateCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), Ctx);
246b2213910SDeLesley Hutchins   case Stmt::CXXOperatorCallExprClass:
247b2213910SDeLesley Hutchins     return translateCXXOperatorCallExpr(cast<CXXOperatorCallExpr>(S), Ctx);
248b2213910SDeLesley Hutchins   case Stmt::UnaryOperatorClass:
249b2213910SDeLesley Hutchins     return translateUnaryOperator(cast<UnaryOperator>(S), Ctx);
250b2213910SDeLesley Hutchins   case Stmt::BinaryOperatorClass:
251f8b412adSDeLesley Hutchins   case Stmt::CompoundAssignOperatorClass:
252b2213910SDeLesley Hutchins     return translateBinaryOperator(cast<BinaryOperator>(S), Ctx);
253b2213910SDeLesley Hutchins 
254b2213910SDeLesley Hutchins   case Stmt::ArraySubscriptExprClass:
255b2213910SDeLesley Hutchins     return translateArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Ctx);
256b2213910SDeLesley Hutchins   case Stmt::ConditionalOperatorClass:
257ea1f8338SDeLesley Hutchins     return translateAbstractConditionalOperator(
258ea1f8338SDeLesley Hutchins              cast<ConditionalOperator>(S), Ctx);
259b2213910SDeLesley Hutchins   case Stmt::BinaryConditionalOperatorClass:
260ea1f8338SDeLesley Hutchins     return translateAbstractConditionalOperator(
261b2213910SDeLesley Hutchins              cast<BinaryConditionalOperator>(S), Ctx);
262b2213910SDeLesley Hutchins 
263b2213910SDeLesley Hutchins   // We treat these as no-ops
2647c44da27SBill Wendling   case Stmt::ConstantExprClass:
2657c44da27SBill Wendling     return translate(cast<ConstantExpr>(S)->getSubExpr(), Ctx);
266b2213910SDeLesley Hutchins   case Stmt::ParenExprClass:
267b2213910SDeLesley Hutchins     return translate(cast<ParenExpr>(S)->getSubExpr(), Ctx);
268b2213910SDeLesley Hutchins   case Stmt::ExprWithCleanupsClass:
269b2213910SDeLesley Hutchins     return translate(cast<ExprWithCleanups>(S)->getSubExpr(), Ctx);
270b2213910SDeLesley Hutchins   case Stmt::CXXBindTemporaryExprClass:
271b2213910SDeLesley Hutchins     return translate(cast<CXXBindTemporaryExpr>(S)->getSubExpr(), Ctx);
2724baaa5abSRichard Smith   case Stmt::MaterializeTemporaryExprClass:
273b0561b33STyker     return translate(cast<MaterializeTemporaryExpr>(S)->getSubExpr(), Ctx);
274b2213910SDeLesley Hutchins 
275b2213910SDeLesley Hutchins   // Collect all literals
276b2213910SDeLesley Hutchins   case Stmt::CharacterLiteralClass:
277b2213910SDeLesley Hutchins   case Stmt::CXXNullPtrLiteralExprClass:
278b2213910SDeLesley Hutchins   case Stmt::GNUNullExprClass:
279b2213910SDeLesley Hutchins   case Stmt::CXXBoolLiteralExprClass:
280b2213910SDeLesley Hutchins   case Stmt::FloatingLiteralClass:
281b2213910SDeLesley Hutchins   case Stmt::ImaginaryLiteralClass:
282b2213910SDeLesley Hutchins   case Stmt::IntegerLiteralClass:
283b2213910SDeLesley Hutchins   case Stmt::StringLiteralClass:
284b2213910SDeLesley Hutchins   case Stmt::ObjCStringLiteralClass:
285b2213910SDeLesley Hutchins     return new (Arena) til::Literal(cast<Expr>(S));
286aab9aff0SDeLesley Hutchins 
287aab9aff0SDeLesley Hutchins   case Stmt::DeclStmtClass:
288aab9aff0SDeLesley Hutchins     return translateDeclStmt(cast<DeclStmt>(S), Ctx);
289b2213910SDeLesley Hutchins   default:
290b2213910SDeLesley Hutchins     break;
291b2213910SDeLesley Hutchins   }
292bc324330SEugene Zelenko   if (const auto *CE = dyn_cast<CastExpr>(S))
293b2213910SDeLesley Hutchins     return translateCastExpr(CE, Ctx);
294b2213910SDeLesley Hutchins 
295b2213910SDeLesley Hutchins   return new (Arena) til::Undefined(S);
296b2213910SDeLesley Hutchins }
297b2213910SDeLesley Hutchins 
translateDeclRefExpr(const DeclRefExpr * DRE,CallingContext * Ctx)298b2213910SDeLesley Hutchins til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE,
299b2213910SDeLesley Hutchins                                                CallingContext *Ctx) {
300bc324330SEugene Zelenko   const auto *VD = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl());
301b2213910SDeLesley Hutchins 
302b2213910SDeLesley Hutchins   // Function parameters require substitution and/or renaming.
303b296c64eSAaron Puchert   if (const auto *PV = dyn_cast<ParmVarDecl>(VD)) {
304b2213910SDeLesley Hutchins     unsigned I = PV->getFunctionScopeIndex();
305cefafc49SJF Bastien     const DeclContext *D = PV->getDeclContext();
306cefafc49SJF Bastien     if (Ctx && Ctx->FunArgs) {
307cefafc49SJF Bastien       const Decl *Canonical = Ctx->AttrDecl->getCanonicalDecl();
308cefafc49SJF Bastien       if (isa<FunctionDecl>(D)
309cefafc49SJF Bastien               ? (cast<FunctionDecl>(D)->getCanonicalDecl() == Canonical)
310cefafc49SJF Bastien               : (cast<ObjCMethodDecl>(D)->getCanonicalDecl() == Canonical)) {
311b2213910SDeLesley Hutchins         // Substitute call arguments for references to function parameters
312b2213910SDeLesley Hutchins         assert(I < Ctx->NumArgs);
313b2213910SDeLesley Hutchins         return translate(Ctx->FunArgs[I], Ctx->Prev);
314b2213910SDeLesley Hutchins       }
315cefafc49SJF Bastien     }
316b2213910SDeLesley Hutchins     // Map the param back to the param of the original function declaration
317b2213910SDeLesley Hutchins     // for consistent comparisons.
318cefafc49SJF Bastien     VD = isa<FunctionDecl>(D)
319cefafc49SJF Bastien              ? cast<FunctionDecl>(D)->getCanonicalDecl()->getParamDecl(I)
320cefafc49SJF Bastien              : cast<ObjCMethodDecl>(D)->getCanonicalDecl()->getParamDecl(I);
321b2213910SDeLesley Hutchins   }
322b2213910SDeLesley Hutchins 
323dc0541f1SDeLesley Hutchins   // For non-local variables, treat it as a reference to a named object.
324b2213910SDeLesley Hutchins   return new (Arena) til::LiteralPtr(VD);
325b2213910SDeLesley Hutchins }
326b2213910SDeLesley Hutchins 
translateCXXThisExpr(const CXXThisExpr * TE,CallingContext * Ctx)327b2213910SDeLesley Hutchins til::SExpr *SExprBuilder::translateCXXThisExpr(const CXXThisExpr *TE,
328b2213910SDeLesley Hutchins                                                CallingContext *Ctx) {
329b2213910SDeLesley Hutchins   // Substitute for 'this'
330b2213910SDeLesley Hutchins   if (Ctx && Ctx->SelfArg)
331b2213910SDeLesley Hutchins     return translate(Ctx->SelfArg, Ctx->Prev);
332b2213910SDeLesley Hutchins   assert(SelfVar && "We have no variable for 'this'!");
333b2213910SDeLesley Hutchins   return SelfVar;
334b2213910SDeLesley Hutchins }
335b2213910SDeLesley Hutchins 
getValueDeclFromSExpr(const til::SExpr * E)33666a97ee9SBenjamin Kramer static const ValueDecl *getValueDeclFromSExpr(const til::SExpr *E) {
337bc324330SEugene Zelenko   if (const auto *V = dyn_cast<til::Variable>(E))
338ea1f8338SDeLesley Hutchins     return V->clangDecl();
339bc324330SEugene Zelenko   if (const auto *Ph = dyn_cast<til::Phi>(E))
3404e38f100SDeLesley Hutchins     return Ph->clangDecl();
341bc324330SEugene Zelenko   if (const auto *P = dyn_cast<til::Project>(E))
342ea1f8338SDeLesley Hutchins     return P->clangDecl();
343bc324330SEugene Zelenko   if (const auto *L = dyn_cast<til::LiteralPtr>(E))
344ea1f8338SDeLesley Hutchins     return L->clangDecl();
345dcfba334SHans Wennborg   return nullptr;
346ea1f8338SDeLesley Hutchins }
347ea1f8338SDeLesley Hutchins 
hasAnyPointerType(const til::SExpr * E)348b081f44eSAaron Puchert static bool hasAnyPointerType(const til::SExpr *E) {
349ea1f8338SDeLesley Hutchins   auto *VD = getValueDeclFromSExpr(E);
350b081f44eSAaron Puchert   if (VD && VD->getType()->isAnyPointerType())
351ea1f8338SDeLesley Hutchins     return true;
352bc324330SEugene Zelenko   if (const auto *C = dyn_cast<til::Cast>(E))
353ea1f8338SDeLesley Hutchins     return C->castOpcode() == til::CAST_objToPtr;
354ea1f8338SDeLesley Hutchins 
355ea1f8338SDeLesley Hutchins   return false;
356ea1f8338SDeLesley Hutchins }
357ea1f8338SDeLesley Hutchins 
358ea1f8338SDeLesley Hutchins // Grab the very first declaration of virtual method D
getFirstVirtualDecl(const CXXMethodDecl * D)35966a97ee9SBenjamin Kramer static const CXXMethodDecl *getFirstVirtualDecl(const CXXMethodDecl *D) {
360ea1f8338SDeLesley Hutchins   while (true) {
361ea1f8338SDeLesley Hutchins     D = D->getCanonicalDecl();
362acfa339eSBenjamin Kramer     auto OverriddenMethods = D->overridden_methods();
363acfa339eSBenjamin Kramer     if (OverriddenMethods.begin() == OverriddenMethods.end())
364ea1f8338SDeLesley Hutchins       return D;  // Method does not override anything
365acfa339eSBenjamin Kramer     // FIXME: this does not work with multiple inheritance.
366acfa339eSBenjamin Kramer     D = *OverriddenMethods.begin();
367ea1f8338SDeLesley Hutchins   }
368ea1f8338SDeLesley Hutchins   return nullptr;
369ea1f8338SDeLesley Hutchins }
370ea1f8338SDeLesley Hutchins 
translateMemberExpr(const MemberExpr * ME,CallingContext * Ctx)371b2213910SDeLesley Hutchins til::SExpr *SExprBuilder::translateMemberExpr(const MemberExpr *ME,
372b2213910SDeLesley Hutchins                                               CallingContext *Ctx) {
373ea1f8338SDeLesley Hutchins   til::SExpr *BE = translate(ME->getBase(), Ctx);
374ea1f8338SDeLesley Hutchins   til::SExpr *E  = new (Arena) til::SApply(BE);
375ea1f8338SDeLesley Hutchins 
376bc324330SEugene Zelenko   const auto *D = cast<ValueDecl>(ME->getMemberDecl()->getCanonicalDecl());
377bc324330SEugene Zelenko   if (const auto *VD = dyn_cast<CXXMethodDecl>(D))
378ea1f8338SDeLesley Hutchins     D = getFirstVirtualDecl(VD);
379ea1f8338SDeLesley Hutchins 
380ea1f8338SDeLesley Hutchins   til::Project *P = new (Arena) til::Project(E, D);
381b081f44eSAaron Puchert   if (hasAnyPointerType(BE))
382b081f44eSAaron Puchert     P->setArrow(true);
383b081f44eSAaron Puchert   return P;
384b081f44eSAaron Puchert }
385b081f44eSAaron Puchert 
translateObjCIVarRefExpr(const ObjCIvarRefExpr * IVRE,CallingContext * Ctx)386b081f44eSAaron Puchert til::SExpr *SExprBuilder::translateObjCIVarRefExpr(const ObjCIvarRefExpr *IVRE,
387b081f44eSAaron Puchert                                                    CallingContext *Ctx) {
388b081f44eSAaron Puchert   til::SExpr *BE = translate(IVRE->getBase(), Ctx);
389b081f44eSAaron Puchert   til::SExpr *E = new (Arena) til::SApply(BE);
390b081f44eSAaron Puchert 
391b081f44eSAaron Puchert   const auto *D = cast<ObjCIvarDecl>(IVRE->getDecl()->getCanonicalDecl());
392b081f44eSAaron Puchert 
393b081f44eSAaron Puchert   til::Project *P = new (Arena) til::Project(E, D);
394b081f44eSAaron Puchert   if (hasAnyPointerType(BE))
395ea1f8338SDeLesley Hutchins     P->setArrow(true);
396ea1f8338SDeLesley Hutchins   return P;
397b2213910SDeLesley Hutchins }
398b2213910SDeLesley Hutchins 
translateCallExpr(const CallExpr * CE,CallingContext * Ctx,const Expr * SelfE)399b2213910SDeLesley Hutchins til::SExpr *SExprBuilder::translateCallExpr(const CallExpr *CE,
400ea1f8338SDeLesley Hutchins                                             CallingContext *Ctx,
401ea1f8338SDeLesley Hutchins                                             const Expr *SelfE) {
402ea1f8338SDeLesley Hutchins   if (CapabilityExprMode) {
403ea1f8338SDeLesley Hutchins     // Handle LOCK_RETURNED
404f6ccde78SAaron Puchert     if (const FunctionDecl *FD = CE->getDirectCallee()) {
405f6ccde78SAaron Puchert       FD = FD->getMostRecentDecl();
406ea1f8338SDeLesley Hutchins       if (LockReturnedAttr *At = FD->getAttr<LockReturnedAttr>()) {
407ea1f8338SDeLesley Hutchins         CallingContext LRCallCtx(Ctx);
408ea1f8338SDeLesley Hutchins         LRCallCtx.AttrDecl = CE->getDirectCallee();
409ea1f8338SDeLesley Hutchins         LRCallCtx.SelfArg = SelfE;
410ea1f8338SDeLesley Hutchins         LRCallCtx.NumArgs = CE->getNumArgs();
411ea1f8338SDeLesley Hutchins         LRCallCtx.FunArgs = CE->getArgs();
4124266522aSDeLesley Hutchins         return const_cast<til::SExpr *>(
4134266522aSDeLesley Hutchins             translateAttrExpr(At->getArg(), &LRCallCtx).sexpr());
414ea1f8338SDeLesley Hutchins       }
415ea1f8338SDeLesley Hutchins     }
416f6ccde78SAaron Puchert   }
417ea1f8338SDeLesley Hutchins 
418b2213910SDeLesley Hutchins   til::SExpr *E = translate(CE->getCallee(), Ctx);
4193f993c13SAaron Ballman   for (const auto *Arg : CE->arguments()) {
4203f993c13SAaron Ballman     til::SExpr *A = translate(Arg, Ctx);
421b2213910SDeLesley Hutchins     E = new (Arena) til::Apply(E, A);
422b2213910SDeLesley Hutchins   }
423b2213910SDeLesley Hutchins   return new (Arena) til::Call(E, CE);
424b2213910SDeLesley Hutchins }
425b2213910SDeLesley Hutchins 
translateCXXMemberCallExpr(const CXXMemberCallExpr * ME,CallingContext * Ctx)426b2213910SDeLesley Hutchins til::SExpr *SExprBuilder::translateCXXMemberCallExpr(
427b2213910SDeLesley Hutchins     const CXXMemberCallExpr *ME, CallingContext *Ctx) {
428ea1f8338SDeLesley Hutchins   if (CapabilityExprMode) {
429ea1f8338SDeLesley Hutchins     // Ignore calls to get() on smart pointers.
430ea1f8338SDeLesley Hutchins     if (ME->getMethodDecl()->getNameAsString() == "get" &&
431ea1f8338SDeLesley Hutchins         ME->getNumArgs() == 0) {
432ea1f8338SDeLesley Hutchins       auto *E = translate(ME->getImplicitObjectArgument(), Ctx);
433ea1f8338SDeLesley Hutchins       return new (Arena) til::Cast(til::CAST_objToPtr, E);
434ea1f8338SDeLesley Hutchins       // return E;
435ea1f8338SDeLesley Hutchins     }
436ea1f8338SDeLesley Hutchins   }
437ea1f8338SDeLesley Hutchins   return translateCallExpr(cast<CallExpr>(ME), Ctx,
438ea1f8338SDeLesley Hutchins                            ME->getImplicitObjectArgument());
439b2213910SDeLesley Hutchins }
440b2213910SDeLesley Hutchins 
translateCXXOperatorCallExpr(const CXXOperatorCallExpr * OCE,CallingContext * Ctx)441b2213910SDeLesley Hutchins til::SExpr *SExprBuilder::translateCXXOperatorCallExpr(
442b2213910SDeLesley Hutchins     const CXXOperatorCallExpr *OCE, CallingContext *Ctx) {
443ea1f8338SDeLesley Hutchins   if (CapabilityExprMode) {
444ea1f8338SDeLesley Hutchins     // Ignore operator * and operator -> on smart pointers.
445ea1f8338SDeLesley Hutchins     OverloadedOperatorKind k = OCE->getOperator();
446ea1f8338SDeLesley Hutchins     if (k == OO_Star || k == OO_Arrow) {
447ea1f8338SDeLesley Hutchins       auto *E = translate(OCE->getArg(0), Ctx);
448ea1f8338SDeLesley Hutchins       return new (Arena) til::Cast(til::CAST_objToPtr, E);
449ea1f8338SDeLesley Hutchins       // return E;
450ea1f8338SDeLesley Hutchins     }
451ea1f8338SDeLesley Hutchins   }
452b2213910SDeLesley Hutchins   return translateCallExpr(cast<CallExpr>(OCE), Ctx);
453b2213910SDeLesley Hutchins }
454b2213910SDeLesley Hutchins 
translateUnaryOperator(const UnaryOperator * UO,CallingContext * Ctx)455b2213910SDeLesley Hutchins til::SExpr *SExprBuilder::translateUnaryOperator(const UnaryOperator *UO,
456b2213910SDeLesley Hutchins                                                  CallingContext *Ctx) {
457b2213910SDeLesley Hutchins   switch (UO->getOpcode()) {
458b2213910SDeLesley Hutchins   case UO_PostInc:
459b2213910SDeLesley Hutchins   case UO_PostDec:
460b2213910SDeLesley Hutchins   case UO_PreInc:
461b2213910SDeLesley Hutchins   case UO_PreDec:
462b2213910SDeLesley Hutchins     return new (Arena) til::Undefined(UO);
463b2213910SDeLesley Hutchins 
464bc324330SEugene Zelenko   case UO_AddrOf:
465ea1f8338SDeLesley Hutchins     if (CapabilityExprMode) {
466ea1f8338SDeLesley Hutchins       // interpret &Graph::mu_ as an existential.
467bc324330SEugene Zelenko       if (const auto *DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr())) {
468ea1f8338SDeLesley Hutchins         if (DRE->getDecl()->isCXXInstanceMember()) {
469ea1f8338SDeLesley Hutchins           // This is a pointer-to-member expression, e.g. &MyClass::mu_.
470ea1f8338SDeLesley Hutchins           // We interpret this syntax specially, as a wildcard.
471ea1f8338SDeLesley Hutchins           auto *W = new (Arena) til::Wildcard();
472ea1f8338SDeLesley Hutchins           return new (Arena) til::Project(W, DRE->getDecl());
473ea1f8338SDeLesley Hutchins         }
474ea1f8338SDeLesley Hutchins       }
475ea1f8338SDeLesley Hutchins     }
476ea1f8338SDeLesley Hutchins     // otherwise, & is a no-op
477ea1f8338SDeLesley Hutchins     return translate(UO->getSubExpr(), Ctx);
478ea1f8338SDeLesley Hutchins 
479b2213910SDeLesley Hutchins   // We treat these as no-ops
480b2213910SDeLesley Hutchins   case UO_Deref:
481b2213910SDeLesley Hutchins   case UO_Plus:
482b2213910SDeLesley Hutchins     return translate(UO->getSubExpr(), Ctx);
483b2213910SDeLesley Hutchins 
484b2213910SDeLesley Hutchins   case UO_Minus:
485f4b5e7c6SDeLesley Hutchins     return new (Arena)
486f4b5e7c6SDeLesley Hutchins       til::UnaryOp(til::UOP_Minus, translate(UO->getSubExpr(), Ctx));
487b2213910SDeLesley Hutchins   case UO_Not:
488f4b5e7c6SDeLesley Hutchins     return new (Arena)
489f4b5e7c6SDeLesley Hutchins       til::UnaryOp(til::UOP_BitNot, translate(UO->getSubExpr(), Ctx));
490b2213910SDeLesley Hutchins   case UO_LNot:
491f4b5e7c6SDeLesley Hutchins     return new (Arena)
492f4b5e7c6SDeLesley Hutchins       til::UnaryOp(til::UOP_LogicNot, translate(UO->getSubExpr(), Ctx));
493f4b5e7c6SDeLesley Hutchins 
494f4b5e7c6SDeLesley Hutchins   // Currently unsupported
495b2213910SDeLesley Hutchins   case UO_Real:
496b2213910SDeLesley Hutchins   case UO_Imag:
4973f993c13SAaron Ballman   case UO_Extension:
4989f690bd8SRichard Smith   case UO_Coawait:
499f4b5e7c6SDeLesley Hutchins     return new (Arena) til::Undefined(UO);
500b2213910SDeLesley Hutchins   }
501b2213910SDeLesley Hutchins   return new (Arena) til::Undefined(UO);
502b2213910SDeLesley Hutchins }
503b2213910SDeLesley Hutchins 
translateBinOp(til::TIL_BinaryOpcode Op,const BinaryOperator * BO,CallingContext * Ctx,bool Reverse)504f4b5e7c6SDeLesley Hutchins til::SExpr *SExprBuilder::translateBinOp(til::TIL_BinaryOpcode Op,
505f4b5e7c6SDeLesley Hutchins                                          const BinaryOperator *BO,
506f4b5e7c6SDeLesley Hutchins                                          CallingContext *Ctx, bool Reverse) {
507f4b5e7c6SDeLesley Hutchins    til::SExpr *E0 = translate(BO->getLHS(), Ctx);
508f4b5e7c6SDeLesley Hutchins    til::SExpr *E1 = translate(BO->getRHS(), Ctx);
509f4b5e7c6SDeLesley Hutchins    if (Reverse)
510f4b5e7c6SDeLesley Hutchins      return new (Arena) til::BinaryOp(Op, E1, E0);
511f4b5e7c6SDeLesley Hutchins    else
512f4b5e7c6SDeLesley Hutchins      return new (Arena) til::BinaryOp(Op, E0, E1);
513f4b5e7c6SDeLesley Hutchins }
514f4b5e7c6SDeLesley Hutchins 
translateBinAssign(til::TIL_BinaryOpcode Op,const BinaryOperator * BO,CallingContext * Ctx,bool Assign)515f8b412adSDeLesley Hutchins til::SExpr *SExprBuilder::translateBinAssign(til::TIL_BinaryOpcode Op,
516f8b412adSDeLesley Hutchins                                              const BinaryOperator *BO,
517f4b5e7c6SDeLesley Hutchins                                              CallingContext *Ctx,
518f4b5e7c6SDeLesley Hutchins                                              bool Assign) {
519f8b412adSDeLesley Hutchins   const Expr *LHS = BO->getLHS();
520f8b412adSDeLesley Hutchins   const Expr *RHS = BO->getRHS();
521f8b412adSDeLesley Hutchins   til::SExpr *E0 = translate(LHS, Ctx);
522f8b412adSDeLesley Hutchins   til::SExpr *E1 = translate(RHS, Ctx);
523f8b412adSDeLesley Hutchins 
524f8b412adSDeLesley Hutchins   const ValueDecl *VD = nullptr;
525f8b412adSDeLesley Hutchins   til::SExpr *CV = nullptr;
526bc324330SEugene Zelenko   if (const auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
527f8b412adSDeLesley Hutchins     VD = DRE->getDecl();
528f8b412adSDeLesley Hutchins     CV = lookupVarDecl(VD);
529f8b412adSDeLesley Hutchins   }
530f8b412adSDeLesley Hutchins 
531f4b5e7c6SDeLesley Hutchins   if (!Assign) {
532f8b412adSDeLesley Hutchins     til::SExpr *Arg = CV ? CV : new (Arena) til::Load(E0);
533f8b412adSDeLesley Hutchins     E1 = new (Arena) til::BinaryOp(Op, Arg, E1);
534f8b412adSDeLesley Hutchins     E1 = addStatement(E1, nullptr, VD);
535f8b412adSDeLesley Hutchins   }
536f8b412adSDeLesley Hutchins   if (VD && CV)
537f8b412adSDeLesley Hutchins     return updateVarDecl(VD, E1);
538f8b412adSDeLesley Hutchins   return new (Arena) til::Store(E0, E1);
539f8b412adSDeLesley Hutchins }
540f8b412adSDeLesley Hutchins 
translateBinaryOperator(const BinaryOperator * BO,CallingContext * Ctx)541b2213910SDeLesley Hutchins til::SExpr *SExprBuilder::translateBinaryOperator(const BinaryOperator *BO,
542b2213910SDeLesley Hutchins                                                   CallingContext *Ctx) {
543b2213910SDeLesley Hutchins   switch (BO->getOpcode()) {
544b2213910SDeLesley Hutchins   case BO_PtrMemD:
545b2213910SDeLesley Hutchins   case BO_PtrMemI:
546b2213910SDeLesley Hutchins     return new (Arena) til::Undefined(BO);
547b2213910SDeLesley Hutchins 
548f4b5e7c6SDeLesley Hutchins   case BO_Mul:  return translateBinOp(til::BOP_Mul, BO, Ctx);
549f4b5e7c6SDeLesley Hutchins   case BO_Div:  return translateBinOp(til::BOP_Div, BO, Ctx);
550f4b5e7c6SDeLesley Hutchins   case BO_Rem:  return translateBinOp(til::BOP_Rem, BO, Ctx);
551f4b5e7c6SDeLesley Hutchins   case BO_Add:  return translateBinOp(til::BOP_Add, BO, Ctx);
552f4b5e7c6SDeLesley Hutchins   case BO_Sub:  return translateBinOp(til::BOP_Sub, BO, Ctx);
553f4b5e7c6SDeLesley Hutchins   case BO_Shl:  return translateBinOp(til::BOP_Shl, BO, Ctx);
554f4b5e7c6SDeLesley Hutchins   case BO_Shr:  return translateBinOp(til::BOP_Shr, BO, Ctx);
555f4b5e7c6SDeLesley Hutchins   case BO_LT:   return translateBinOp(til::BOP_Lt,  BO, Ctx);
556f4b5e7c6SDeLesley Hutchins   case BO_GT:   return translateBinOp(til::BOP_Lt,  BO, Ctx, true);
557f4b5e7c6SDeLesley Hutchins   case BO_LE:   return translateBinOp(til::BOP_Leq, BO, Ctx);
558f4b5e7c6SDeLesley Hutchins   case BO_GE:   return translateBinOp(til::BOP_Leq, BO, Ctx, true);
559f4b5e7c6SDeLesley Hutchins   case BO_EQ:   return translateBinOp(til::BOP_Eq,  BO, Ctx);
560f4b5e7c6SDeLesley Hutchins   case BO_NE:   return translateBinOp(til::BOP_Neq, BO, Ctx);
561c70f1d63SRichard Smith   case BO_Cmp:  return translateBinOp(til::BOP_Cmp, BO, Ctx);
562f4b5e7c6SDeLesley Hutchins   case BO_And:  return translateBinOp(til::BOP_BitAnd,   BO, Ctx);
563f4b5e7c6SDeLesley Hutchins   case BO_Xor:  return translateBinOp(til::BOP_BitXor,   BO, Ctx);
564f4b5e7c6SDeLesley Hutchins   case BO_Or:   return translateBinOp(til::BOP_BitOr,    BO, Ctx);
565f4b5e7c6SDeLesley Hutchins   case BO_LAnd: return translateBinOp(til::BOP_LogicAnd, BO, Ctx);
566f4b5e7c6SDeLesley Hutchins   case BO_LOr:  return translateBinOp(til::BOP_LogicOr,  BO, Ctx);
5673f993c13SAaron Ballman 
568f4b5e7c6SDeLesley Hutchins   case BO_Assign:    return translateBinAssign(til::BOP_Eq,  BO, Ctx, true);
569f4b5e7c6SDeLesley Hutchins   case BO_MulAssign: return translateBinAssign(til::BOP_Mul, BO, Ctx);
570f4b5e7c6SDeLesley Hutchins   case BO_DivAssign: return translateBinAssign(til::BOP_Div, BO, Ctx);
571f4b5e7c6SDeLesley Hutchins   case BO_RemAssign: return translateBinAssign(til::BOP_Rem, BO, Ctx);
572f4b5e7c6SDeLesley Hutchins   case BO_AddAssign: return translateBinAssign(til::BOP_Add, BO, Ctx);
573f4b5e7c6SDeLesley Hutchins   case BO_SubAssign: return translateBinAssign(til::BOP_Sub, BO, Ctx);
574f4b5e7c6SDeLesley Hutchins   case BO_ShlAssign: return translateBinAssign(til::BOP_Shl, BO, Ctx);
575f4b5e7c6SDeLesley Hutchins   case BO_ShrAssign: return translateBinAssign(til::BOP_Shr, BO, Ctx);
576f4b5e7c6SDeLesley Hutchins   case BO_AndAssign: return translateBinAssign(til::BOP_BitAnd, BO, Ctx);
577f4b5e7c6SDeLesley Hutchins   case BO_XorAssign: return translateBinAssign(til::BOP_BitXor, BO, Ctx);
578f4b5e7c6SDeLesley Hutchins   case BO_OrAssign:  return translateBinAssign(til::BOP_BitOr,  BO, Ctx);
579b2213910SDeLesley Hutchins 
580b2213910SDeLesley Hutchins   case BO_Comma:
581f8b412adSDeLesley Hutchins     // The clang CFG should have already processed both sides.
582b2213910SDeLesley Hutchins     return translate(BO->getRHS(), Ctx);
583b2213910SDeLesley Hutchins   }
58478340014SDeLesley Hutchins   return new (Arena) til::Undefined(BO);
585f8b412adSDeLesley Hutchins }
586b2213910SDeLesley Hutchins 
translateCastExpr(const CastExpr * CE,CallingContext * Ctx)587b2213910SDeLesley Hutchins til::SExpr *SExprBuilder::translateCastExpr(const CastExpr *CE,
588b2213910SDeLesley Hutchins                                             CallingContext *Ctx) {
589bc324330SEugene Zelenko   CastKind K = CE->getCastKind();
590b2213910SDeLesley Hutchins   switch (K) {
591aab9aff0SDeLesley Hutchins   case CK_LValueToRValue: {
592bc324330SEugene Zelenko     if (const auto *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
593aab9aff0SDeLesley Hutchins       til::SExpr *E0 = lookupVarDecl(DRE->getDecl());
594aab9aff0SDeLesley Hutchins       if (E0)
595aab9aff0SDeLesley Hutchins         return E0;
596aab9aff0SDeLesley Hutchins     }
597aab9aff0SDeLesley Hutchins     til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
598ea1f8338SDeLesley Hutchins     return E0;
599ea1f8338SDeLesley Hutchins     // FIXME!! -- get Load working properly
600ea1f8338SDeLesley Hutchins     // return new (Arena) til::Load(E0);
601aab9aff0SDeLesley Hutchins   }
602b2213910SDeLesley Hutchins   case CK_NoOp:
603b2213910SDeLesley Hutchins   case CK_DerivedToBase:
604b2213910SDeLesley Hutchins   case CK_UncheckedDerivedToBase:
605b2213910SDeLesley Hutchins   case CK_ArrayToPointerDecay:
606aab9aff0SDeLesley Hutchins   case CK_FunctionToPointerDecay: {
607aab9aff0SDeLesley Hutchins     til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
608b2213910SDeLesley Hutchins     return E0;
609aab9aff0SDeLesley Hutchins   }
610aab9aff0SDeLesley Hutchins   default: {
611f4b5e7c6SDeLesley Hutchins     // FIXME: handle different kinds of casts.
612aab9aff0SDeLesley Hutchins     til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
613ea1f8338SDeLesley Hutchins     if (CapabilityExprMode)
614ea1f8338SDeLesley Hutchins       return E0;
615f4b5e7c6SDeLesley Hutchins     return new (Arena) til::Cast(til::CAST_none, E0);
616b2213910SDeLesley Hutchins   }
617b2213910SDeLesley Hutchins   }
618aab9aff0SDeLesley Hutchins }
619aab9aff0SDeLesley Hutchins 
6203f993c13SAaron Ballman til::SExpr *
translateArraySubscriptExpr(const ArraySubscriptExpr * E,CallingContext * Ctx)6213f993c13SAaron Ballman SExprBuilder::translateArraySubscriptExpr(const ArraySubscriptExpr *E,
6223f993c13SAaron Ballman                                           CallingContext *Ctx) {
623f1a31165SDeLesley Hutchins   til::SExpr *E0 = translate(E->getBase(), Ctx);
624f1a31165SDeLesley Hutchins   til::SExpr *E1 = translate(E->getIdx(), Ctx);
62544be81b5SDeLesley Hutchins   return new (Arena) til::ArrayIndex(E0, E1);
626b2213910SDeLesley Hutchins }
627b2213910SDeLesley Hutchins 
6283f993c13SAaron Ballman til::SExpr *
translateAbstractConditionalOperator(const AbstractConditionalOperator * CO,CallingContext * Ctx)629ea1f8338SDeLesley Hutchins SExprBuilder::translateAbstractConditionalOperator(
630ea1f8338SDeLesley Hutchins     const AbstractConditionalOperator *CO, CallingContext *Ctx) {
631ea1f8338SDeLesley Hutchins   auto *C = translate(CO->getCond(), Ctx);
632ea1f8338SDeLesley Hutchins   auto *T = translate(CO->getTrueExpr(), Ctx);
633ea1f8338SDeLesley Hutchins   auto *E = translate(CO->getFalseExpr(), Ctx);
634ea1f8338SDeLesley Hutchins   return new (Arena) til::IfThenElse(C, T, E);
635b2213910SDeLesley Hutchins }
636b2213910SDeLesley Hutchins 
637aab9aff0SDeLesley Hutchins til::SExpr *
translateDeclStmt(const DeclStmt * S,CallingContext * Ctx)638aab9aff0SDeLesley Hutchins SExprBuilder::translateDeclStmt(const DeclStmt *S, CallingContext *Ctx) {
639aab9aff0SDeLesley Hutchins   DeclGroupRef DGrp = S->getDeclGroup();
640bc324330SEugene Zelenko   for (auto I : DGrp) {
641bc324330SEugene Zelenko     if (auto *VD = dyn_cast_or_null<VarDecl>(I)) {
642aab9aff0SDeLesley Hutchins       Expr *E = VD->getInit();
643aab9aff0SDeLesley Hutchins       til::SExpr* SE = translate(E, Ctx);
6447e615c2fSDeLesley Hutchins 
645aab9aff0SDeLesley Hutchins       // Add local variables with trivial type to the variable map
646aab9aff0SDeLesley Hutchins       QualType T = VD->getType();
647bc324330SEugene Zelenko       if (T.isTrivialType(VD->getASTContext()))
648aab9aff0SDeLesley Hutchins         return addVarDecl(VD, SE);
649aab9aff0SDeLesley Hutchins       else {
650aab9aff0SDeLesley Hutchins         // TODO: add alloca
651aab9aff0SDeLesley Hutchins       }
652aab9aff0SDeLesley Hutchins     }
653aab9aff0SDeLesley Hutchins   }
654aab9aff0SDeLesley Hutchins   return nullptr;
655aab9aff0SDeLesley Hutchins }
6567e615c2fSDeLesley Hutchins 
657aab9aff0SDeLesley Hutchins // If (E) is non-trivial, then add it to the current basic block, and
658aab9aff0SDeLesley Hutchins // update the statement map so that S refers to E.  Returns a new variable
659aab9aff0SDeLesley Hutchins // that refers to E.
660aab9aff0SDeLesley Hutchins // If E is trivial returns E.
addStatement(til::SExpr * E,const Stmt * S,const ValueDecl * VD)661aab9aff0SDeLesley Hutchins til::SExpr *SExprBuilder::addStatement(til::SExpr* E, const Stmt *S,
662aab9aff0SDeLesley Hutchins                                        const ValueDecl *VD) {
6634e38f100SDeLesley Hutchins   if (!E || !CurrentBB || E->block() || til::ThreadSafetyTIL::isTrivial(E))
664aab9aff0SDeLesley Hutchins     return E;
6654e38f100SDeLesley Hutchins   if (VD)
6664e38f100SDeLesley Hutchins     E = new (Arena) til::Variable(E, VD);
6674e38f100SDeLesley Hutchins   CurrentInstructions.push_back(E);
66811bb3087SDeLesley Hutchins   if (S)
6694e38f100SDeLesley Hutchins     insertStmt(S, E);
6704e38f100SDeLesley Hutchins   return E;
671b2213910SDeLesley Hutchins }
672b2213910SDeLesley Hutchins 
673aab9aff0SDeLesley Hutchins // Returns the current value of VD, if known, and nullptr otherwise.
lookupVarDecl(const ValueDecl * VD)674aab9aff0SDeLesley Hutchins til::SExpr *SExprBuilder::lookupVarDecl(const ValueDecl *VD) {
675ae497dedSDeLesley Hutchins   auto It = LVarIdxMap.find(VD);
676ae497dedSDeLesley Hutchins   if (It != LVarIdxMap.end()) {
677ae497dedSDeLesley Hutchins     assert(CurrentLVarMap[It->second].first == VD);
678ae497dedSDeLesley Hutchins     return CurrentLVarMap[It->second].second;
679ae497dedSDeLesley Hutchins   }
680aab9aff0SDeLesley Hutchins   return nullptr;
681aab9aff0SDeLesley Hutchins }
682aab9aff0SDeLesley Hutchins 
683aab9aff0SDeLesley Hutchins // if E is a til::Variable, update its clangDecl.
maybeUpdateVD(til::SExpr * E,const ValueDecl * VD)68466a97ee9SBenjamin Kramer static void maybeUpdateVD(til::SExpr *E, const ValueDecl *VD) {
685aab9aff0SDeLesley Hutchins   if (!E)
686aab9aff0SDeLesley Hutchins     return;
687bc324330SEugene Zelenko   if (auto *V = dyn_cast<til::Variable>(E)) {
688aab9aff0SDeLesley Hutchins     if (!V->clangDecl())
689aab9aff0SDeLesley Hutchins       V->setClangDecl(VD);
690aab9aff0SDeLesley Hutchins   }
691aab9aff0SDeLesley Hutchins }
692aab9aff0SDeLesley Hutchins 
693aab9aff0SDeLesley Hutchins // Adds a new variable declaration.
addVarDecl(const ValueDecl * VD,til::SExpr * E)694aab9aff0SDeLesley Hutchins til::SExpr *SExprBuilder::addVarDecl(const ValueDecl *VD, til::SExpr *E) {
695aab9aff0SDeLesley Hutchins   maybeUpdateVD(E, VD);
696ae497dedSDeLesley Hutchins   LVarIdxMap.insert(std::make_pair(VD, CurrentLVarMap.size()));
697ae497dedSDeLesley Hutchins   CurrentLVarMap.makeWritable();
698ae497dedSDeLesley Hutchins   CurrentLVarMap.push_back(std::make_pair(VD, E));
699aab9aff0SDeLesley Hutchins   return E;
700aab9aff0SDeLesley Hutchins }
701aab9aff0SDeLesley Hutchins 
702aab9aff0SDeLesley Hutchins // Updates a current variable declaration.  (E.g. by assignment)
updateVarDecl(const ValueDecl * VD,til::SExpr * E)703aab9aff0SDeLesley Hutchins til::SExpr *SExprBuilder::updateVarDecl(const ValueDecl *VD, til::SExpr *E) {
704aab9aff0SDeLesley Hutchins   maybeUpdateVD(E, VD);
705ae497dedSDeLesley Hutchins   auto It = LVarIdxMap.find(VD);
706ae497dedSDeLesley Hutchins   if (It == LVarIdxMap.end()) {
707aab9aff0SDeLesley Hutchins     til::SExpr *Ptr = new (Arena) til::LiteralPtr(VD);
708aab9aff0SDeLesley Hutchins     til::SExpr *St  = new (Arena) til::Store(Ptr, E);
709aab9aff0SDeLesley Hutchins     return St;
710aab9aff0SDeLesley Hutchins   }
711ae497dedSDeLesley Hutchins   CurrentLVarMap.makeWritable();
712ae497dedSDeLesley Hutchins   CurrentLVarMap.elem(It->second).second = E;
713aab9aff0SDeLesley Hutchins   return E;
714aab9aff0SDeLesley Hutchins }
715aab9aff0SDeLesley Hutchins 
716ae497dedSDeLesley Hutchins // Make a Phi node in the current block for the i^th variable in CurrentVarMap.
717ae497dedSDeLesley Hutchins // If E != null, sets Phi[CurrentBlockInfo->ArgIndex] = E.
718ae497dedSDeLesley Hutchins // If E == null, this is a backedge and will be set later.
makePhiNodeVar(unsigned i,unsigned NPreds,til::SExpr * E)719ae497dedSDeLesley Hutchins void SExprBuilder::makePhiNodeVar(unsigned i, unsigned NPreds, til::SExpr *E) {
720ae497dedSDeLesley Hutchins   unsigned ArgIndex = CurrentBlockInfo->ProcessedPredecessors;
721ae497dedSDeLesley Hutchins   assert(ArgIndex > 0 && ArgIndex < NPreds);
722ae497dedSDeLesley Hutchins 
7234e38f100SDeLesley Hutchins   til::SExpr *CurrE = CurrentLVarMap[i].second;
7244e38f100SDeLesley Hutchins   if (CurrE->block() == CurrentBB) {
725ae497dedSDeLesley Hutchins     // We already have a Phi node in the current block,
726ae497dedSDeLesley Hutchins     // so just add the new variable to the Phi node.
727bc324330SEugene Zelenko     auto *Ph = dyn_cast<til::Phi>(CurrE);
728aab9aff0SDeLesley Hutchins     assert(Ph && "Expecting Phi node.");
729ae497dedSDeLesley Hutchins     if (E)
730ae497dedSDeLesley Hutchins       Ph->values()[ArgIndex] = E;
731ae497dedSDeLesley Hutchins     return;
732aab9aff0SDeLesley Hutchins   }
733aab9aff0SDeLesley Hutchins 
734ae497dedSDeLesley Hutchins   // Make a new phi node: phi(..., E)
735ae497dedSDeLesley Hutchins   // All phi args up to the current index are set to the current value.
736ae497dedSDeLesley Hutchins   til::Phi *Ph = new (Arena) til::Phi(Arena, NPreds);
737aab9aff0SDeLesley Hutchins   Ph->values().setValues(NPreds, nullptr);
738ae497dedSDeLesley Hutchins   for (unsigned PIdx = 0; PIdx < ArgIndex; ++PIdx)
739f8b412adSDeLesley Hutchins     Ph->values()[PIdx] = CurrE;
740ae497dedSDeLesley Hutchins   if (E)
741ae497dedSDeLesley Hutchins     Ph->values()[ArgIndex] = E;
7424e38f100SDeLesley Hutchins   Ph->setClangDecl(CurrentLVarMap[i].first);
743f8b412adSDeLesley Hutchins   // If E is from a back-edge, or either E or CurrE are incomplete, then
744f8b412adSDeLesley Hutchins   // mark this node as incomplete; we may need to remove it later.
745bc324330SEugene Zelenko   if (!E || isIncompletePhi(E) || isIncompletePhi(CurrE))
746a9db0019SDeLesley Hutchins     Ph->setStatus(til::Phi::PH_Incomplete);
747aab9aff0SDeLesley Hutchins 
748ae497dedSDeLesley Hutchins   // Add Phi node to current block, and update CurrentLVarMap[i]
7494e38f100SDeLesley Hutchins   CurrentArguments.push_back(Ph);
750a9db0019SDeLesley Hutchins   if (Ph->status() == til::Phi::PH_Incomplete)
7514e38f100SDeLesley Hutchins     IncompleteArgs.push_back(Ph);
752ae497dedSDeLesley Hutchins 
753ae497dedSDeLesley Hutchins   CurrentLVarMap.makeWritable();
7544e38f100SDeLesley Hutchins   CurrentLVarMap.elem(i).second = Ph;
755aab9aff0SDeLesley Hutchins }
756ae497dedSDeLesley Hutchins 
757ae497dedSDeLesley Hutchins // Merge values from Map into the current variable map.
758ae497dedSDeLesley Hutchins // This will construct Phi nodes in the current basic block as necessary.
mergeEntryMap(LVarDefinitionMap Map)759ae497dedSDeLesley Hutchins void SExprBuilder::mergeEntryMap(LVarDefinitionMap Map) {
760ae497dedSDeLesley Hutchins   assert(CurrentBlockInfo && "Not processing a block!");
761ae497dedSDeLesley Hutchins 
762ae497dedSDeLesley Hutchins   if (!CurrentLVarMap.valid()) {
763ae497dedSDeLesley Hutchins     // Steal Map, using copy-on-write.
764ae497dedSDeLesley Hutchins     CurrentLVarMap = std::move(Map);
765ae497dedSDeLesley Hutchins     return;
766aab9aff0SDeLesley Hutchins   }
767ae497dedSDeLesley Hutchins   if (CurrentLVarMap.sameAs(Map))
768ae497dedSDeLesley Hutchins     return;  // Easy merge: maps from different predecessors are unchanged.
769ae497dedSDeLesley Hutchins 
770ae497dedSDeLesley Hutchins   unsigned NPreds = CurrentBB->numPredecessors();
771ae497dedSDeLesley Hutchins   unsigned ESz = CurrentLVarMap.size();
772ae497dedSDeLesley Hutchins   unsigned MSz = Map.size();
773ae497dedSDeLesley Hutchins   unsigned Sz  = std::min(ESz, MSz);
774ae497dedSDeLesley Hutchins 
775ae497dedSDeLesley Hutchins   for (unsigned i = 0; i < Sz; ++i) {
776ae497dedSDeLesley Hutchins     if (CurrentLVarMap[i].first != Map[i].first) {
777ae497dedSDeLesley Hutchins       // We've reached the end of variables in common.
778ae497dedSDeLesley Hutchins       CurrentLVarMap.makeWritable();
779ae497dedSDeLesley Hutchins       CurrentLVarMap.downsize(i);
780ae497dedSDeLesley Hutchins       break;
781ae497dedSDeLesley Hutchins     }
782ae497dedSDeLesley Hutchins     if (CurrentLVarMap[i].second != Map[i].second)
783ae497dedSDeLesley Hutchins       makePhiNodeVar(i, NPreds, Map[i].second);
784aab9aff0SDeLesley Hutchins   }
785aab9aff0SDeLesley Hutchins   if (ESz > MSz) {
786ae497dedSDeLesley Hutchins     CurrentLVarMap.makeWritable();
787ae497dedSDeLesley Hutchins     CurrentLVarMap.downsize(Map.size());
788ae497dedSDeLesley Hutchins   }
789ae497dedSDeLesley Hutchins }
790ae497dedSDeLesley Hutchins 
791ae497dedSDeLesley Hutchins // Merge a back edge into the current variable map.
792ae497dedSDeLesley Hutchins // This will create phi nodes for all variables in the variable map.
mergeEntryMapBackEdge()793ae497dedSDeLesley Hutchins void SExprBuilder::mergeEntryMapBackEdge() {
794ae497dedSDeLesley Hutchins   // We don't have definitions for variables on the backedge, because we
795ae497dedSDeLesley Hutchins   // haven't gotten that far in the CFG.  Thus, when encountering a back edge,
796ae497dedSDeLesley Hutchins   // we conservatively create Phi nodes for all variables.  Unnecessary Phi
797ae497dedSDeLesley Hutchins   // nodes will be marked as incomplete, and stripped out at the end.
798ae497dedSDeLesley Hutchins   //
799ae497dedSDeLesley Hutchins   // An Phi node is unnecessary if it only refers to itself and one other
800ae497dedSDeLesley Hutchins   // variable, e.g. x = Phi(y, y, x)  can be reduced to x = y.
801ae497dedSDeLesley Hutchins 
802ae497dedSDeLesley Hutchins   assert(CurrentBlockInfo && "Not processing a block!");
803ae497dedSDeLesley Hutchins 
804ae497dedSDeLesley Hutchins   if (CurrentBlockInfo->HasBackEdges)
805ae497dedSDeLesley Hutchins     return;
806ae497dedSDeLesley Hutchins   CurrentBlockInfo->HasBackEdges = true;
807ae497dedSDeLesley Hutchins 
808ae497dedSDeLesley Hutchins   CurrentLVarMap.makeWritable();
809ae497dedSDeLesley Hutchins   unsigned Sz = CurrentLVarMap.size();
810ae497dedSDeLesley Hutchins   unsigned NPreds = CurrentBB->numPredecessors();
811ae497dedSDeLesley Hutchins 
812bc324330SEugene Zelenko   for (unsigned i = 0; i < Sz; ++i)
813ae497dedSDeLesley Hutchins     makePhiNodeVar(i, NPreds, nullptr);
814ae497dedSDeLesley Hutchins }
815ae497dedSDeLesley Hutchins 
816ae497dedSDeLesley Hutchins // Update the phi nodes that were initially created for a back edge
817ae497dedSDeLesley Hutchins // once the variable definitions have been computed.
818ae497dedSDeLesley Hutchins // I.e., merge the current variable map into the phi nodes for Blk.
mergePhiNodesBackEdge(const CFGBlock * Blk)819ae497dedSDeLesley Hutchins void SExprBuilder::mergePhiNodesBackEdge(const CFGBlock *Blk) {
820ae497dedSDeLesley Hutchins   til::BasicBlock *BB = lookupBlock(Blk);
821ae497dedSDeLesley Hutchins   unsigned ArgIndex = BBInfo[Blk->getBlockID()].ProcessedPredecessors;
822ae497dedSDeLesley Hutchins   assert(ArgIndex > 0 && ArgIndex < BB->numPredecessors());
823ae497dedSDeLesley Hutchins 
8244e38f100SDeLesley Hutchins   for (til::SExpr *PE : BB->arguments()) {
825bc324330SEugene Zelenko     auto *Ph = dyn_cast_or_null<til::Phi>(PE);
826ae497dedSDeLesley Hutchins     assert(Ph && "Expecting Phi Node.");
827ae497dedSDeLesley Hutchins     assert(Ph->values()[ArgIndex] == nullptr && "Wrong index for back edge.");
828ae497dedSDeLesley Hutchins 
8294e38f100SDeLesley Hutchins     til::SExpr *E = lookupVarDecl(Ph->clangDecl());
830ae497dedSDeLesley Hutchins     assert(E && "Couldn't find local variable for Phi node.");
831ae497dedSDeLesley Hutchins     Ph->values()[ArgIndex] = E;
832aab9aff0SDeLesley Hutchins   }
833aab9aff0SDeLesley Hutchins }
834aab9aff0SDeLesley Hutchins 
enterCFG(CFG * Cfg,const NamedDecl * D,const CFGBlock * First)835a7bcab75SBenjamin Kramer void SExprBuilder::enterCFG(CFG *Cfg, const NamedDecl *D,
836aab9aff0SDeLesley Hutchins                             const CFGBlock *First) {
837aab9aff0SDeLesley Hutchins   // Perform initial setup operations.
838aab9aff0SDeLesley Hutchins   unsigned NBlocks = Cfg->getNumBlockIDs();
839aab9aff0SDeLesley Hutchins   Scfg = new (Arena) til::SCFG(Arena, NBlocks);
840aab9aff0SDeLesley Hutchins 
841aab9aff0SDeLesley Hutchins   // allocate all basic blocks immediately, to handle forward references.
842aab9aff0SDeLesley Hutchins   BBInfo.resize(NBlocks);
843ae497dedSDeLesley Hutchins   BlockMap.resize(NBlocks, nullptr);
844ae497dedSDeLesley Hutchins   // create map from clang blockID to til::BasicBlocks
845aab9aff0SDeLesley Hutchins   for (auto *B : *Cfg) {
84644be81b5SDeLesley Hutchins     auto *BB = new (Arena) til::BasicBlock(Arena);
84744be81b5SDeLesley Hutchins     BB->reserveInstructions(B->size());
848ae497dedSDeLesley Hutchins     BlockMap[B->getBlockID()] = BB;
849aab9aff0SDeLesley Hutchins   }
850f8b412adSDeLesley Hutchins 
851f8b412adSDeLesley Hutchins   CurrentBB = lookupBlock(&Cfg->getEntry());
852a7bcab75SBenjamin Kramer   auto Parms = isa<ObjCMethodDecl>(D) ? cast<ObjCMethodDecl>(D)->parameters()
853a7bcab75SBenjamin Kramer                                       : cast<FunctionDecl>(D)->parameters();
854a7bcab75SBenjamin Kramer   for (auto *Pm : Parms) {
855f8b412adSDeLesley Hutchins     QualType T = Pm->getType();
856f8b412adSDeLesley Hutchins     if (!T.isTrivialType(Pm->getASTContext()))
857f8b412adSDeLesley Hutchins       continue;
858f8b412adSDeLesley Hutchins 
859f8b412adSDeLesley Hutchins     // Add parameters to local variable map.
860f8b412adSDeLesley Hutchins     // FIXME: right now we emulate params with loads; that should be fixed.
861f8b412adSDeLesley Hutchins     til::SExpr *Lp = new (Arena) til::LiteralPtr(Pm);
862f8b412adSDeLesley Hutchins     til::SExpr *Ld = new (Arena) til::Load(Lp);
863f8b412adSDeLesley Hutchins     til::SExpr *V  = addStatement(Ld, nullptr, Pm);
864f8b412adSDeLesley Hutchins     addVarDecl(Pm, V);
865f8b412adSDeLesley Hutchins   }
866b2213910SDeLesley Hutchins }
867b2213910SDeLesley Hutchins 
enterCFGBlock(const CFGBlock * B)868aab9aff0SDeLesley Hutchins void SExprBuilder::enterCFGBlock(const CFGBlock *B) {
8692a8c18d9SAlexander Kornienko   // Initialize TIL basic block and add it to the CFG.
870f8b412adSDeLesley Hutchins   CurrentBB = lookupBlock(B);
87144be81b5SDeLesley Hutchins   CurrentBB->reservePredecessors(B->pred_size());
872b2213910SDeLesley Hutchins   Scfg->add(CurrentBB);
873aab9aff0SDeLesley Hutchins 
874aab9aff0SDeLesley Hutchins   CurrentBlockInfo = &BBInfo[B->getBlockID()];
875aab9aff0SDeLesley Hutchins 
876ae497dedSDeLesley Hutchins   // CurrentLVarMap is moved to ExitMap on block exit.
877f8b412adSDeLesley Hutchins   // FIXME: the entry block will hold function parameters.
878f8b412adSDeLesley Hutchins   // assert(!CurrentLVarMap.valid() && "CurrentLVarMap already initialized.");
879b2213910SDeLesley Hutchins }
880b2213910SDeLesley Hutchins 
handlePredecessor(const CFGBlock * Pred)881aab9aff0SDeLesley Hutchins void SExprBuilder::handlePredecessor(const CFGBlock *Pred) {
882ae497dedSDeLesley Hutchins   // Compute CurrentLVarMap on entry from ExitMaps of predecessors
883aab9aff0SDeLesley Hutchins 
88444be81b5SDeLesley Hutchins   CurrentBB->addPredecessor(BlockMap[Pred->getBlockID()]);
885aab9aff0SDeLesley Hutchins   BlockInfo *PredInfo = &BBInfo[Pred->getBlockID()];
886ae497dedSDeLesley Hutchins   assert(PredInfo->UnprocessedSuccessors > 0);
887aab9aff0SDeLesley Hutchins 
888ae497dedSDeLesley Hutchins   if (--PredInfo->UnprocessedSuccessors == 0)
889aab9aff0SDeLesley Hutchins     mergeEntryMap(std::move(PredInfo->ExitMap));
890aab9aff0SDeLesley Hutchins   else
891aab9aff0SDeLesley Hutchins     mergeEntryMap(PredInfo->ExitMap.clone());
892aab9aff0SDeLesley Hutchins 
893ae497dedSDeLesley Hutchins   ++CurrentBlockInfo->ProcessedPredecessors;
894aab9aff0SDeLesley Hutchins }
895aab9aff0SDeLesley Hutchins 
handlePredecessorBackEdge(const CFGBlock * Pred)896aab9aff0SDeLesley Hutchins void SExprBuilder::handlePredecessorBackEdge(const CFGBlock *Pred) {
897ae497dedSDeLesley Hutchins   mergeEntryMapBackEdge();
898aab9aff0SDeLesley Hutchins }
899aab9aff0SDeLesley Hutchins 
enterCFGBlockBody(const CFGBlock * B)900ae497dedSDeLesley Hutchins void SExprBuilder::enterCFGBlockBody(const CFGBlock *B) {
901ae497dedSDeLesley Hutchins   // The merge*() methods have created arguments.
902ae497dedSDeLesley Hutchins   // Push those arguments onto the basic block.
903ae497dedSDeLesley Hutchins   CurrentBB->arguments().reserve(
904ae497dedSDeLesley Hutchins     static_cast<unsigned>(CurrentArguments.size()), Arena);
9054e38f100SDeLesley Hutchins   for (auto *A : CurrentArguments)
9064e38f100SDeLesley Hutchins     CurrentBB->addArgument(A);
907ae497dedSDeLesley Hutchins }
908aab9aff0SDeLesley Hutchins 
handleStatement(const Stmt * S)909aab9aff0SDeLesley Hutchins void SExprBuilder::handleStatement(const Stmt *S) {
910ea1f8338SDeLesley Hutchins   til::SExpr *E = translate(S, nullptr);
91111bb3087SDeLesley Hutchins   addStatement(E, S);
912b2213910SDeLesley Hutchins }
913b2213910SDeLesley Hutchins 
handleDestructorCall(const VarDecl * VD,const CXXDestructorDecl * DD)914aab9aff0SDeLesley Hutchins void SExprBuilder::handleDestructorCall(const VarDecl *VD,
915aab9aff0SDeLesley Hutchins                                         const CXXDestructorDecl *DD) {
91611bb3087SDeLesley Hutchins   til::SExpr *Sf = new (Arena) til::LiteralPtr(VD);
91711bb3087SDeLesley Hutchins   til::SExpr *Dr = new (Arena) til::LiteralPtr(DD);
91811bb3087SDeLesley Hutchins   til::SExpr *Ap = new (Arena) til::Apply(Dr, Sf);
9193f993c13SAaron Ballman   til::SExpr *E = new (Arena) til::Call(Ap);
92011bb3087SDeLesley Hutchins   addStatement(E, nullptr);
92111bb3087SDeLesley Hutchins }
922b2213910SDeLesley Hutchins 
exitCFGBlockBody(const CFGBlock * B)923aab9aff0SDeLesley Hutchins void SExprBuilder::exitCFGBlockBody(const CFGBlock *B) {
924ae497dedSDeLesley Hutchins   CurrentBB->instructions().reserve(
925ae497dedSDeLesley Hutchins     static_cast<unsigned>(CurrentInstructions.size()), Arena);
926ae497dedSDeLesley Hutchins   for (auto *V : CurrentInstructions)
927ae497dedSDeLesley Hutchins     CurrentBB->addInstruction(V);
928ae497dedSDeLesley Hutchins 
929ae497dedSDeLesley Hutchins   // Create an appropriate terminator
930aab9aff0SDeLesley Hutchins   unsigned N = B->succ_size();
931aab9aff0SDeLesley Hutchins   auto It = B->succ_begin();
932aab9aff0SDeLesley Hutchins   if (N == 1) {
933ae497dedSDeLesley Hutchins     til::BasicBlock *BB = *It ? lookupBlock(*It) : nullptr;
934aab9aff0SDeLesley Hutchins     // TODO: set index
935b6031921SDeLesley Hutchins     unsigned Idx = BB ? BB->findPredecessorIndex(CurrentBB) : 0;
9364e38f100SDeLesley Hutchins     auto *Tm = new (Arena) til::Goto(BB, Idx);
937aab9aff0SDeLesley Hutchins     CurrentBB->setTerminator(Tm);
938aab9aff0SDeLesley Hutchins   }
939aab9aff0SDeLesley Hutchins   else if (N == 2) {
940ea1f8338SDeLesley Hutchins     til::SExpr *C = translate(B->getTerminatorCondition(true), nullptr);
941ae497dedSDeLesley Hutchins     til::BasicBlock *BB1 = *It ? lookupBlock(*It) : nullptr;
942aab9aff0SDeLesley Hutchins     ++It;
943ae497dedSDeLesley Hutchins     til::BasicBlock *BB2 = *It ? lookupBlock(*It) : nullptr;
9442a8c18d9SAlexander Kornienko     // FIXME: make sure these aren't critical edges.
9454e38f100SDeLesley Hutchins     auto *Tm = new (Arena) til::Branch(C, BB1, BB2);
946aab9aff0SDeLesley Hutchins     CurrentBB->setTerminator(Tm);
947aab9aff0SDeLesley Hutchins   }
948aab9aff0SDeLesley Hutchins }
949aab9aff0SDeLesley Hutchins 
handleSuccessor(const CFGBlock * Succ)950aab9aff0SDeLesley Hutchins void SExprBuilder::handleSuccessor(const CFGBlock *Succ) {
951ae497dedSDeLesley Hutchins   ++CurrentBlockInfo->UnprocessedSuccessors;
952aab9aff0SDeLesley Hutchins }
953aab9aff0SDeLesley Hutchins 
handleSuccessorBackEdge(const CFGBlock * Succ)954aab9aff0SDeLesley Hutchins void SExprBuilder::handleSuccessorBackEdge(const CFGBlock *Succ) {
955ae497dedSDeLesley Hutchins   mergePhiNodesBackEdge(Succ);
956ae497dedSDeLesley Hutchins   ++BBInfo[Succ->getBlockID()].ProcessedPredecessors;
957aab9aff0SDeLesley Hutchins }
958aab9aff0SDeLesley Hutchins 
exitCFGBlock(const CFGBlock * B)959aab9aff0SDeLesley Hutchins void SExprBuilder::exitCFGBlock(const CFGBlock *B) {
960f8b412adSDeLesley Hutchins   CurrentArguments.clear();
961f8b412adSDeLesley Hutchins   CurrentInstructions.clear();
962ae497dedSDeLesley Hutchins   CurrentBlockInfo->ExitMap = std::move(CurrentLVarMap);
963aab9aff0SDeLesley Hutchins   CurrentBB = nullptr;
964aab9aff0SDeLesley Hutchins   CurrentBlockInfo = nullptr;
965b2213910SDeLesley Hutchins }
966b2213910SDeLesley Hutchins 
exitCFG(const CFGBlock * Last)967aab9aff0SDeLesley Hutchins void SExprBuilder::exitCFG(const CFGBlock *Last) {
9684e38f100SDeLesley Hutchins   for (auto *Ph : IncompleteArgs) {
9694e38f100SDeLesley Hutchins     if (Ph->status() == til::Phi::PH_Incomplete)
9704e38f100SDeLesley Hutchins       simplifyIncompleteArg(Ph);
971a9db0019SDeLesley Hutchins   }
972a9db0019SDeLesley Hutchins 
973ae497dedSDeLesley Hutchins   CurrentArguments.clear();
974ae497dedSDeLesley Hutchins   CurrentInstructions.clear();
975a9db0019SDeLesley Hutchins   IncompleteArgs.clear();
97611bb3087SDeLesley Hutchins }
977b2213910SDeLesley Hutchins 
978ea1f8338SDeLesley Hutchins /*
9794e6afcfcSAaron Puchert namespace {
9804e6afcfcSAaron Puchert 
9814e6afcfcSAaron Puchert class TILPrinter :
9824e6afcfcSAaron Puchert     public til::PrettyPrinter<TILPrinter, llvm::raw_ostream> {};
9834e6afcfcSAaron Puchert 
9844e6afcfcSAaron Puchert } // namespace
9854e6afcfcSAaron Puchert 
9864e6afcfcSAaron Puchert namespace clang {
9874e6afcfcSAaron Puchert namespace threadSafety {
9884e6afcfcSAaron Puchert 
989aab9aff0SDeLesley Hutchins void printSCFG(CFGWalker &Walker) {
990b2213910SDeLesley Hutchins   llvm::BumpPtrAllocator Bpa;
991b2213910SDeLesley Hutchins   til::MemRegionRef Arena(&Bpa);
992ea1f8338SDeLesley Hutchins   SExprBuilder SxBuilder(Arena);
993ea1f8338SDeLesley Hutchins   til::SCFG *Scfg = SxBuilder.buildCFG(Walker);
994ea1f8338SDeLesley Hutchins   TILPrinter::print(Scfg, llvm::errs());
995b2213910SDeLesley Hutchins }
9964e6afcfcSAaron Puchert 
9974e6afcfcSAaron Puchert } // namespace threadSafety
9984e6afcfcSAaron Puchert } // namespace clang
999ea1f8338SDeLesley Hutchins */
1000