clang 22.0.0git
ThreadSafetyCommon.cpp
Go to the documentation of this file.
1//===- ThreadSafetyCommon.cpp ---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Implementation of the interfaces declared in ThreadSafetyCommon.h
10//
11//===----------------------------------------------------------------------===//
12
14#include "clang/AST/Attr.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclCXX.h"
17#include "clang/AST/DeclGroup.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/ExprCXX.h"
22#include "clang/AST/Stmt.h"
23#include "clang/AST/Type.h"
25#include "clang/Analysis/CFG.h"
26#include "clang/Basic/LLVM.h"
29#include "llvm/ADT/ScopeExit.h"
30#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/StringRef.h"
32#include <algorithm>
33#include <cassert>
34#include <string>
35#include <utility>
36
37using namespace clang;
38using namespace threadSafety;
39
40// From ThreadSafetyUtil.h
42 switch (CE->getStmtClass()) {
43 case Stmt::IntegerLiteralClass:
44 return toString(cast<IntegerLiteral>(CE)->getValue(), 10, true);
45 case Stmt::StringLiteralClass: {
46 std::string ret("\"");
47 ret += cast<StringLiteral>(CE)->getString();
48 ret += "\"";
49 return ret;
50 }
51 case Stmt::CharacterLiteralClass:
52 case Stmt::CXXNullPtrLiteralExprClass:
53 case Stmt::GNUNullExprClass:
54 case Stmt::CXXBoolLiteralExprClass:
55 case Stmt::FloatingLiteralClass:
56 case Stmt::ImaginaryLiteralClass:
57 case Stmt::ObjCStringLiteralClass:
58 default:
59 return "#lit";
60 }
61}
62
63// Return true if E is a variable that points to an incomplete Phi node.
64static bool isIncompletePhi(const til::SExpr *E) {
65 if (const auto *Ph = dyn_cast<til::Phi>(E))
66 return Ph->status() == til::Phi::PH_Incomplete;
67 return false;
68}
69
70static constexpr std::pair<StringRef, bool> ClassifyCapabilityFallback{
71 /*Kind=*/StringRef("mutex"),
72 /*Reentrant=*/false};
73
74// Returns pair (Kind, Reentrant).
75static std::pair<StringRef, bool> classifyCapability(const TypeDecl &TD) {
76 if (const auto *CA = TD.getAttr<CapabilityAttr>())
77 return {CA->getName(), TD.hasAttr<ReentrantCapabilityAttr>()};
78
80}
81
82// Returns pair (Kind, Reentrant).
83static std::pair<StringRef, bool> classifyCapability(QualType QT) {
84 // We need to look at the declaration of the type of the value to determine
85 // which it is. The type should either be a record or a typedef, or a pointer
86 // or reference thereof.
87 if (const auto *RD = QT->getAsRecordDecl())
88 return classifyCapability(*RD);
89 if (const auto *TT = QT->getAs<TypedefType>())
90 return classifyCapability(*TT->getDecl());
93
95}
96
98 const auto &[Kind, Reentrant] = classifyCapability(QT);
99 *this = CapabilityExpr(E, Kind, Neg, Reentrant);
100}
101
103
104til::SExpr *SExprBuilder::lookupStmt(const Stmt *S) { return SMap.lookup(S); }
105
107 Walker.walk(*this);
108 return Scfg;
109}
110
111static bool isCalleeArrow(const Expr *E) {
112 const auto *ME = dyn_cast<MemberExpr>(E->IgnoreParenCasts());
113 return ME ? ME->isArrow() : false;
114}
115
116/// Translate a clang expression in an attribute to a til::SExpr.
117/// Constructs the context from D, DeclExp, and SelfDecl.
118///
119/// \param AttrExp The expression to translate.
120/// \param D The declaration to which the attribute is attached.
121/// \param DeclExp An expression involving the Decl to which the attribute
122/// is attached. E.g. the call to a function.
123/// \param Self S-expression to substitute for a \ref CXXThisExpr in a call,
124/// or argument to a cleanup function.
126 const NamedDecl *D,
127 const Expr *DeclExp,
128 til::SExpr *Self) {
129 // If we are processing a raw attribute expression, with no substitutions.
130 if (!DeclExp && !Self)
131 return translateAttrExpr(AttrExp, nullptr);
132
133 CallingContext Ctx(nullptr, D);
134
135 // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute
136 // for formal parameters when we call buildMutexID later.
137 if (!DeclExp)
138 /* We'll use Self. */;
139 else if (const auto *ME = dyn_cast<MemberExpr>(DeclExp)) {
140 Ctx.SelfArg = ME->getBase();
141 Ctx.SelfArrow = ME->isArrow();
142 } else if (const auto *CE = dyn_cast<CXXMemberCallExpr>(DeclExp)) {
143 Ctx.SelfArg = CE->getImplicitObjectArgument();
144 Ctx.SelfArrow = isCalleeArrow(CE->getCallee());
145 Ctx.NumArgs = CE->getNumArgs();
146 Ctx.FunArgs = CE->getArgs();
147 } else if (const auto *CE = dyn_cast<CallExpr>(DeclExp)) {
148 // Calls to operators that are members need to be treated like member calls.
150 Ctx.SelfArg = CE->getArg(0);
151 Ctx.SelfArrow = false;
152 Ctx.NumArgs = CE->getNumArgs() - 1;
153 Ctx.FunArgs = CE->getArgs() + 1;
154 } else {
155 Ctx.NumArgs = CE->getNumArgs();
156 Ctx.FunArgs = CE->getArgs();
157 }
158 } else if (const auto *CE = dyn_cast<CXXConstructExpr>(DeclExp)) {
159 Ctx.SelfArg = nullptr; // Will be set below
160 Ctx.NumArgs = CE->getNumArgs();
161 Ctx.FunArgs = CE->getArgs();
162 }
163
164 // Usually we want to substitute the self-argument for "this", but lambdas
165 // are an exception: "this" on or in a lambda call operator doesn't refer
166 // to the lambda, but to captured "this" in the context it was created in.
167 // This can happen for operator calls and member calls, so fix it up here.
168 if (const auto *CMD = dyn_cast<CXXMethodDecl>(D))
169 if (CMD->getParent()->isLambda())
170 Ctx.SelfArg = nullptr;
171
172 if (Self) {
173 assert(!Ctx.SelfArg && "Ambiguous self argument");
174 assert(isa<FunctionDecl>(D) && "Self argument requires function");
175 if (isa<CXXMethodDecl>(D))
176 Ctx.SelfArg = Self;
177 else
178 Ctx.FunArgs = Self;
179
180 // If the attribute has no arguments, then assume the argument is "this".
181 if (!AttrExp)
182 return CapabilityExpr(
183 Self, cast<CXXMethodDecl>(D)->getFunctionObjectParameterType(),
184 false);
185 else // For most attributes.
186 return translateAttrExpr(AttrExp, &Ctx);
187 }
188
189 // If the attribute has no arguments, then assume the argument is "this".
190 if (!AttrExp)
191 return translateAttrExpr(cast<const Expr *>(Ctx.SelfArg), nullptr);
192 else // For most attributes.
193 return translateAttrExpr(AttrExp, &Ctx);
194}
195
196/// Translate a clang expression in an attribute to a til::SExpr.
197// This assumes a CallingContext has already been created.
199 CallingContext *Ctx) {
200 if (!AttrExp)
201 return CapabilityExpr();
202
203 if (const auto* SLit = dyn_cast<StringLiteral>(AttrExp)) {
204 if (SLit->getString() == "*")
205 // The "*" expr is a universal lock, which essentially turns off
206 // checks until it is removed from the lockset.
207 return CapabilityExpr(new (Arena) til::Wildcard(), StringRef("wildcard"),
208 /*Neg=*/false, /*Reentrant=*/false);
209 else
210 // Ignore other string literals for now.
211 return CapabilityExpr();
212 }
213
214 bool Neg = false;
215 if (const auto *OE = dyn_cast<CXXOperatorCallExpr>(AttrExp)) {
216 if (OE->getOperator() == OO_Exclaim) {
217 Neg = true;
218 AttrExp = OE->getArg(0);
219 }
220 }
221 else if (const auto *UO = dyn_cast<UnaryOperator>(AttrExp)) {
222 if (UO->getOpcode() == UO_LNot) {
223 Neg = true;
224 AttrExp = UO->getSubExpr()->IgnoreImplicit();
225 }
226 }
227
228 const til::SExpr *E = translate(AttrExp, Ctx);
229
230 // Trap mutex expressions like nullptr, or 0.
231 // Any literal value is nonsense.
232 if (!E || isa<til::Literal>(E))
233 return CapabilityExpr();
234
235 // Hack to deal with smart pointers -- strip off top-level pointer casts.
236 if (const auto *CE = dyn_cast<til::Cast>(E)) {
237 if (CE->castOpcode() == til::CAST_objToPtr)
238 E = CE->expr();
239 }
240 return CapabilityExpr(E, AttrExp->getType(), Neg);
241}
242
244 CallingContext *Ctx) {
245 assert(VD);
246
247 // General recursion guard for x = f(x). If we are already in the process of
248 // defining VD, use its pre-assignment value to break the cycle.
249 if (VarsBeingTranslated.contains(VD->getCanonicalDecl()))
250 return new (Arena) til::LiteralPtr(VD);
251 VarsBeingTranslated.insert(VD->getCanonicalDecl());
252 auto Cleanup = llvm::make_scope_exit(
253 [&] { VarsBeingTranslated.erase(VD->getCanonicalDecl()); });
254
255 QualType Ty = VD->getType();
256 if (!VD->isStaticLocal() && Ty->isPointerType()) {
257 // Substitute local variable aliases with a canonical definition.
258 if (LookupLocalVarExpr) {
259 // Attempt to resolve an alias through the more complex local variable map
260 // lookup. This will fail with complex control-flow graphs (where we
261 // revert to no alias resolution to retain stable variable names).
262 if (const Expr *E = LookupLocalVarExpr(VD)) {
263 til::SExpr *Result = translate(E, Ctx);
264 // Unsupported expression (such as heap allocations) will be undefined;
265 // rather than failing here, we simply revert to the pointer being the
266 // canonical variable.
268 return Result;
269 }
270 }
271 }
272
273 return new (Arena) til::LiteralPtr(VD);
274}
275
276// Translate a clang statement or expression to a TIL expression.
277// Also performs substitution of variables; Ctx provides the context.
278// Dispatches on the type of S.
280 if (!S)
281 return nullptr;
282
283 // Check if S has already been translated and cached.
284 // This handles the lookup of SSA names for DeclRefExprs here.
285 if (til::SExpr *E = lookupStmt(S))
286 return E;
287
288 switch (S->getStmtClass()) {
289 case Stmt::DeclRefExprClass:
290 return translateDeclRefExpr(cast<DeclRefExpr>(S), Ctx);
291 case Stmt::CXXThisExprClass:
292 return translateCXXThisExpr(cast<CXXThisExpr>(S), Ctx);
293 case Stmt::MemberExprClass:
294 return translateMemberExpr(cast<MemberExpr>(S), Ctx);
295 case Stmt::ObjCIvarRefExprClass:
296 return translateObjCIVarRefExpr(cast<ObjCIvarRefExpr>(S), Ctx);
297 case Stmt::CallExprClass:
298 return translateCallExpr(cast<CallExpr>(S), Ctx);
299 case Stmt::CXXMemberCallExprClass:
300 return translateCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), Ctx);
301 case Stmt::CXXOperatorCallExprClass:
302 return translateCXXOperatorCallExpr(cast<CXXOperatorCallExpr>(S), Ctx);
303 case Stmt::UnaryOperatorClass:
304 return translateUnaryOperator(cast<UnaryOperator>(S), Ctx);
305 case Stmt::BinaryOperatorClass:
306 case Stmt::CompoundAssignOperatorClass:
307 return translateBinaryOperator(cast<BinaryOperator>(S), Ctx);
308
309 case Stmt::ArraySubscriptExprClass:
310 return translateArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Ctx);
311 case Stmt::ConditionalOperatorClass:
312 return translateAbstractConditionalOperator(
314 case Stmt::BinaryConditionalOperatorClass:
315 return translateAbstractConditionalOperator(
317
318 // We treat these as no-ops
319 case Stmt::ConstantExprClass:
320 return translate(cast<ConstantExpr>(S)->getSubExpr(), Ctx);
321 case Stmt::ParenExprClass:
322 return translate(cast<ParenExpr>(S)->getSubExpr(), Ctx);
323 case Stmt::ExprWithCleanupsClass:
324 return translate(cast<ExprWithCleanups>(S)->getSubExpr(), Ctx);
325 case Stmt::CXXBindTemporaryExprClass:
326 return translate(cast<CXXBindTemporaryExpr>(S)->getSubExpr(), Ctx);
327 case Stmt::MaterializeTemporaryExprClass:
328 return translate(cast<MaterializeTemporaryExpr>(S)->getSubExpr(), Ctx);
329
330 // Collect all literals
331 case Stmt::CharacterLiteralClass:
332 case Stmt::CXXNullPtrLiteralExprClass:
333 case Stmt::GNUNullExprClass:
334 case Stmt::CXXBoolLiteralExprClass:
335 case Stmt::FloatingLiteralClass:
336 case Stmt::ImaginaryLiteralClass:
337 case Stmt::IntegerLiteralClass:
338 case Stmt::StringLiteralClass:
339 case Stmt::ObjCStringLiteralClass:
340 return new (Arena) til::Literal(cast<Expr>(S));
341
342 case Stmt::DeclStmtClass:
343 return translateDeclStmt(cast<DeclStmt>(S), Ctx);
344 case Stmt::StmtExprClass:
345 return translateStmtExpr(cast<StmtExpr>(S), Ctx);
346 default:
347 break;
348 }
349 if (const auto *CE = dyn_cast<CastExpr>(S))
350 return translateCastExpr(CE, Ctx);
351
352 return new (Arena) til::Undefined(S);
353}
354
355til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE,
356 CallingContext *Ctx) {
357 const auto *VD = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl());
358
359 // Function parameters require substitution and/or renaming.
360 if (const auto *PV = dyn_cast<ParmVarDecl>(VD)) {
361 unsigned I = PV->getFunctionScopeIndex();
362 const DeclContext *D = PV->getDeclContext();
363 if (Ctx && Ctx->FunArgs) {
364 const Decl *Canonical = Ctx->AttrDecl->getCanonicalDecl();
365 if (isa<FunctionDecl>(D)
366 ? (cast<FunctionDecl>(D)->getCanonicalDecl() == Canonical)
367 : (cast<ObjCMethodDecl>(D)->getCanonicalDecl() == Canonical)) {
368 // Substitute call arguments for references to function parameters
369 if (const Expr *const *FunArgs =
370 dyn_cast<const Expr *const *>(Ctx->FunArgs)) {
371 assert(I < Ctx->NumArgs);
372 return translate(FunArgs[I], Ctx->Prev);
373 }
374
375 assert(I == 0);
376 return cast<til::SExpr *>(Ctx->FunArgs);
377 }
378 }
379 // Map the param back to the param of the original function declaration
380 // for consistent comparisons.
381 VD = isa<FunctionDecl>(D)
382 ? cast<FunctionDecl>(D)->getCanonicalDecl()->getParamDecl(I)
383 : cast<ObjCMethodDecl>(D)->getCanonicalDecl()->getParamDecl(I);
384 }
385
386 if (const auto *VarD = dyn_cast<VarDecl>(VD))
387 return translateVariable(VarD, Ctx);
388
389 // For non-local variables, treat it as a reference to a named object.
390 return new (Arena) til::LiteralPtr(VD);
391}
392
393til::SExpr *SExprBuilder::translateCXXThisExpr(const CXXThisExpr *TE,
394 CallingContext *Ctx) {
395 // Substitute for 'this'
396 if (Ctx && Ctx->SelfArg) {
397 if (const auto *SelfArg = dyn_cast<const Expr *>(Ctx->SelfArg))
398 return translate(SelfArg, Ctx->Prev);
399 else
400 return cast<til::SExpr *>(Ctx->SelfArg);
401 }
402 assert(SelfVar && "We have no variable for 'this'!");
403 return SelfVar;
404}
405
407 if (const auto *V = dyn_cast<til::Variable>(E))
408 return V->clangDecl();
409 if (const auto *Ph = dyn_cast<til::Phi>(E))
410 return Ph->clangDecl();
411 if (const auto *P = dyn_cast<til::Project>(E))
412 return P->clangDecl();
413 if (const auto *L = dyn_cast<til::LiteralPtr>(E))
414 return L->clangDecl();
415 return nullptr;
416}
417
418static bool hasAnyPointerType(const til::SExpr *E) {
419 auto *VD = getValueDeclFromSExpr(E);
420 if (VD && VD->getType()->isAnyPointerType())
421 return true;
422 if (const auto *C = dyn_cast<til::Cast>(E))
423 return C->castOpcode() == til::CAST_objToPtr;
424
425 return false;
426}
427
428// Grab the very first declaration of virtual method D
430 while (true) {
431 D = D->getCanonicalDecl();
432 auto OverriddenMethods = D->overridden_methods();
433 if (OverriddenMethods.begin() == OverriddenMethods.end())
434 return D; // Method does not override anything
435 // FIXME: this does not work with multiple inheritance.
436 D = *OverriddenMethods.begin();
437 }
438 return nullptr;
439}
440
441til::SExpr *SExprBuilder::translateMemberExpr(const MemberExpr *ME,
442 CallingContext *Ctx) {
443 til::SExpr *BE = translate(ME->getBase(), Ctx);
444 til::SExpr *E = new (Arena) til::SApply(BE);
445
446 const auto *D = cast<ValueDecl>(ME->getMemberDecl()->getCanonicalDecl());
447 if (const auto *VD = dyn_cast<CXXMethodDecl>(D))
448 D = getFirstVirtualDecl(VD);
449
450 til::Project *P = new (Arena) til::Project(E, D);
451 if (hasAnyPointerType(BE))
452 P->setArrow(true);
453 return P;
454}
455
456til::SExpr *SExprBuilder::translateObjCIVarRefExpr(const ObjCIvarRefExpr *IVRE,
457 CallingContext *Ctx) {
458 til::SExpr *BE = translate(IVRE->getBase(), Ctx);
459 til::SExpr *E = new (Arena) til::SApply(BE);
460
461 const auto *D = cast<ObjCIvarDecl>(IVRE->getDecl()->getCanonicalDecl());
462
463 til::Project *P = new (Arena) til::Project(E, D);
464 if (hasAnyPointerType(BE))
465 P->setArrow(true);
466 return P;
467}
468
469til::SExpr *SExprBuilder::translateCallExpr(const CallExpr *CE,
470 CallingContext *Ctx,
471 const Expr *SelfE) {
472 if (CapabilityExprMode) {
473 // Handle LOCK_RETURNED
474 if (const FunctionDecl *FD = CE->getDirectCallee()) {
475 FD = FD->getMostRecentDecl();
476 if (LockReturnedAttr *At = FD->getAttr<LockReturnedAttr>()) {
477 CallingContext LRCallCtx(Ctx);
478 LRCallCtx.AttrDecl = CE->getDirectCallee();
479 LRCallCtx.SelfArg = SelfE;
480 LRCallCtx.NumArgs = CE->getNumArgs();
481 LRCallCtx.FunArgs = CE->getArgs();
482 return const_cast<til::SExpr *>(
483 translateAttrExpr(At->getArg(), &LRCallCtx).sexpr());
484 }
485 }
486 }
487
488 til::SExpr *E = translate(CE->getCallee(), Ctx);
489 for (const auto *Arg : CE->arguments()) {
490 til::SExpr *A = translate(Arg, Ctx);
491 E = new (Arena) til::Apply(E, A);
492 }
493 return new (Arena) til::Call(E, CE);
494}
495
496til::SExpr *SExprBuilder::translateCXXMemberCallExpr(
497 const CXXMemberCallExpr *ME, CallingContext *Ctx) {
498 if (CapabilityExprMode) {
499 // Ignore calls to get() on smart pointers.
500 if (ME->getMethodDecl()->getNameAsString() == "get" &&
501 ME->getNumArgs() == 0) {
502 auto *E = translate(ME->getImplicitObjectArgument(), Ctx);
503 return new (Arena) til::Cast(til::CAST_objToPtr, E);
504 // return E;
505 }
506 }
507 return translateCallExpr(cast<CallExpr>(ME), Ctx,
509}
510
511til::SExpr *SExprBuilder::translateCXXOperatorCallExpr(
512 const CXXOperatorCallExpr *OCE, CallingContext *Ctx) {
513 if (CapabilityExprMode) {
514 // Ignore operator * and operator -> on smart pointers.
516 if (k == OO_Star || k == OO_Arrow) {
517 auto *E = translate(OCE->getArg(0), Ctx);
518 return new (Arena) til::Cast(til::CAST_objToPtr, E);
519 // return E;
520 }
521 }
522 return translateCallExpr(cast<CallExpr>(OCE), Ctx);
523}
524
525til::SExpr *SExprBuilder::translateUnaryOperator(const UnaryOperator *UO,
526 CallingContext *Ctx) {
527 switch (UO->getOpcode()) {
528 case UO_PostInc:
529 case UO_PostDec:
530 case UO_PreInc:
531 case UO_PreDec:
532 return new (Arena) til::Undefined(UO);
533
534 case UO_AddrOf:
535 if (CapabilityExprMode) {
536 // interpret &Graph::mu_ as an existential.
537 if (const auto *DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr())) {
538 if (DRE->getDecl()->isCXXInstanceMember()) {
539 // This is a pointer-to-member expression, e.g. &MyClass::mu_.
540 // We interpret this syntax specially, as a wildcard.
541 auto *W = new (Arena) til::Wildcard();
542 return new (Arena) til::Project(W, DRE->getDecl());
543 }
544 }
545 }
546 // otherwise, & is a no-op
547 return translate(UO->getSubExpr(), Ctx);
548
549 // We treat these as no-ops
550 case UO_Deref:
551 case UO_Plus:
552 return translate(UO->getSubExpr(), Ctx);
553
554 case UO_Minus:
555 return new (Arena)
556 til::UnaryOp(til::UOP_Minus, translate(UO->getSubExpr(), Ctx));
557 case UO_Not:
558 return new (Arena)
559 til::UnaryOp(til::UOP_BitNot, translate(UO->getSubExpr(), Ctx));
560 case UO_LNot:
561 return new (Arena)
562 til::UnaryOp(til::UOP_LogicNot, translate(UO->getSubExpr(), Ctx));
563
564 // Currently unsupported
565 case UO_Real:
566 case UO_Imag:
567 case UO_Extension:
568 case UO_Coawait:
569 return new (Arena) til::Undefined(UO);
570 }
571 return new (Arena) til::Undefined(UO);
572}
573
574til::SExpr *SExprBuilder::translateBinOp(til::TIL_BinaryOpcode Op,
575 const BinaryOperator *BO,
576 CallingContext *Ctx, bool Reverse) {
577 til::SExpr *E0 = translate(BO->getLHS(), Ctx);
578 til::SExpr *E1 = translate(BO->getRHS(), Ctx);
579 if (Reverse)
580 return new (Arena) til::BinaryOp(Op, E1, E0);
581 else
582 return new (Arena) til::BinaryOp(Op, E0, E1);
583}
584
585til::SExpr *SExprBuilder::translateBinAssign(til::TIL_BinaryOpcode Op,
586 const BinaryOperator *BO,
587 CallingContext *Ctx,
588 bool Assign) {
589 const Expr *LHS = BO->getLHS();
590 const Expr *RHS = BO->getRHS();
591 til::SExpr *E0 = translate(LHS, Ctx);
592 til::SExpr *E1 = translate(RHS, Ctx);
593
594 const ValueDecl *VD = nullptr;
595 til::SExpr *CV = nullptr;
596 if (const auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
597 VD = DRE->getDecl();
598 CV = lookupVarDecl(VD);
599 }
600
601 if (!Assign) {
602 til::SExpr *Arg = CV ? CV : new (Arena) til::Load(E0);
603 E1 = new (Arena) til::BinaryOp(Op, Arg, E1);
604 E1 = addStatement(E1, nullptr, VD);
605 }
606 if (VD && CV)
607 return updateVarDecl(VD, E1);
608 return new (Arena) til::Store(E0, E1);
609}
610
611til::SExpr *SExprBuilder::translateBinaryOperator(const BinaryOperator *BO,
612 CallingContext *Ctx) {
613 switch (BO->getOpcode()) {
614 case BO_PtrMemD:
615 case BO_PtrMemI:
616 return new (Arena) til::Undefined(BO);
617
618 case BO_Mul: return translateBinOp(til::BOP_Mul, BO, Ctx);
619 case BO_Div: return translateBinOp(til::BOP_Div, BO, Ctx);
620 case BO_Rem: return translateBinOp(til::BOP_Rem, BO, Ctx);
621 case BO_Add: return translateBinOp(til::BOP_Add, BO, Ctx);
622 case BO_Sub: return translateBinOp(til::BOP_Sub, BO, Ctx);
623 case BO_Shl: return translateBinOp(til::BOP_Shl, BO, Ctx);
624 case BO_Shr: return translateBinOp(til::BOP_Shr, BO, Ctx);
625 case BO_LT: return translateBinOp(til::BOP_Lt, BO, Ctx);
626 case BO_GT: return translateBinOp(til::BOP_Lt, BO, Ctx, true);
627 case BO_LE: return translateBinOp(til::BOP_Leq, BO, Ctx);
628 case BO_GE: return translateBinOp(til::BOP_Leq, BO, Ctx, true);
629 case BO_EQ: return translateBinOp(til::BOP_Eq, BO, Ctx);
630 case BO_NE: return translateBinOp(til::BOP_Neq, BO, Ctx);
631 case BO_Cmp: return translateBinOp(til::BOP_Cmp, BO, Ctx);
632 case BO_And: return translateBinOp(til::BOP_BitAnd, BO, Ctx);
633 case BO_Xor: return translateBinOp(til::BOP_BitXor, BO, Ctx);
634 case BO_Or: return translateBinOp(til::BOP_BitOr, BO, Ctx);
635 case BO_LAnd: return translateBinOp(til::BOP_LogicAnd, BO, Ctx);
636 case BO_LOr: return translateBinOp(til::BOP_LogicOr, BO, Ctx);
637
638 case BO_Assign: return translateBinAssign(til::BOP_Eq, BO, Ctx, true);
639 case BO_MulAssign: return translateBinAssign(til::BOP_Mul, BO, Ctx);
640 case BO_DivAssign: return translateBinAssign(til::BOP_Div, BO, Ctx);
641 case BO_RemAssign: return translateBinAssign(til::BOP_Rem, BO, Ctx);
642 case BO_AddAssign: return translateBinAssign(til::BOP_Add, BO, Ctx);
643 case BO_SubAssign: return translateBinAssign(til::BOP_Sub, BO, Ctx);
644 case BO_ShlAssign: return translateBinAssign(til::BOP_Shl, BO, Ctx);
645 case BO_ShrAssign: return translateBinAssign(til::BOP_Shr, BO, Ctx);
646 case BO_AndAssign: return translateBinAssign(til::BOP_BitAnd, BO, Ctx);
647 case BO_XorAssign: return translateBinAssign(til::BOP_BitXor, BO, Ctx);
648 case BO_OrAssign: return translateBinAssign(til::BOP_BitOr, BO, Ctx);
649
650 case BO_Comma:
651 // The clang CFG should have already processed both sides.
652 return translate(BO->getRHS(), Ctx);
653 }
654 return new (Arena) til::Undefined(BO);
655}
656
657til::SExpr *SExprBuilder::translateCastExpr(const CastExpr *CE,
658 CallingContext *Ctx) {
659 CastKind K = CE->getCastKind();
660 switch (K) {
661 case CK_LValueToRValue: {
662 if (const auto *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
663 til::SExpr *E0 = lookupVarDecl(DRE->getDecl());
664 if (E0)
665 return E0;
666 }
667 til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
668 return E0;
669 // FIXME!! -- get Load working properly
670 // return new (Arena) til::Load(E0);
671 }
672 case CK_NoOp:
673 case CK_DerivedToBase:
674 case CK_UncheckedDerivedToBase:
675 case CK_ArrayToPointerDecay:
676 case CK_FunctionToPointerDecay: {
677 til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
678 return E0;
679 }
680 default: {
681 // FIXME: handle different kinds of casts.
682 til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
683 if (CapabilityExprMode)
684 return E0;
685 return new (Arena) til::Cast(til::CAST_none, E0);
686 }
687 }
688}
689
691SExprBuilder::translateArraySubscriptExpr(const ArraySubscriptExpr *E,
692 CallingContext *Ctx) {
693 til::SExpr *E0 = translate(E->getBase(), Ctx);
694 til::SExpr *E1 = translate(E->getIdx(), Ctx);
695 return new (Arena) til::ArrayIndex(E0, E1);
696}
697
699SExprBuilder::translateAbstractConditionalOperator(
700 const AbstractConditionalOperator *CO, CallingContext *Ctx) {
701 auto *C = translate(CO->getCond(), Ctx);
702 auto *T = translate(CO->getTrueExpr(), Ctx);
703 auto *E = translate(CO->getFalseExpr(), Ctx);
704 return new (Arena) til::IfThenElse(C, T, E);
705}
706
708SExprBuilder::translateDeclStmt(const DeclStmt *S, CallingContext *Ctx) {
709 DeclGroupRef DGrp = S->getDeclGroup();
710 for (auto *I : DGrp) {
711 if (auto *VD = dyn_cast_or_null<VarDecl>(I)) {
712 Expr *E = VD->getInit();
713 til::SExpr* SE = translate(E, Ctx);
714
715 // Add local variables with trivial type to the variable map
716 QualType T = VD->getType();
717 if (T.isTrivialType(VD->getASTContext()))
718 return addVarDecl(VD, SE);
719 else {
720 // TODO: add alloca
721 }
722 }
723 }
724 return nullptr;
725}
726
727til::SExpr *SExprBuilder::translateStmtExpr(const StmtExpr *SE,
728 CallingContext *Ctx) {
729 // The value of a statement expression is the value of the last statement,
730 // which must be an expression.
731 const CompoundStmt *CS = SE->getSubStmt();
732 return CS->body_empty() ? new (Arena) til::Undefined(SE)
733 : translate(CS->body_back(), Ctx);
734}
735
736// If (E) is non-trivial, then add it to the current basic block, and
737// update the statement map so that S refers to E. Returns a new variable
738// that refers to E.
739// If E is trivial returns E.
740til::SExpr *SExprBuilder::addStatement(til::SExpr* E, const Stmt *S,
741 const ValueDecl *VD) {
742 if (!E || !CurrentBB || E->block() || til::ThreadSafetyTIL::isTrivial(E))
743 return E;
744 if (VD)
745 E = new (Arena) til::Variable(E, VD);
746 CurrentInstructions.push_back(E);
747 if (S)
748 insertStmt(S, E);
749 return E;
750}
751
752// Returns the current value of VD, if known, and nullptr otherwise.
753til::SExpr *SExprBuilder::lookupVarDecl(const ValueDecl *VD) {
754 auto It = LVarIdxMap.find(VD);
755 if (It != LVarIdxMap.end()) {
756 assert(CurrentLVarMap[It->second].first == VD);
757 return CurrentLVarMap[It->second].second;
758 }
759 return nullptr;
760}
761
762// if E is a til::Variable, update its clangDecl.
763static void maybeUpdateVD(til::SExpr *E, const ValueDecl *VD) {
764 if (!E)
765 return;
766 if (auto *V = dyn_cast<til::Variable>(E)) {
767 if (!V->clangDecl())
768 V->setClangDecl(VD);
769 }
770}
771
772// Adds a new variable declaration.
773til::SExpr *SExprBuilder::addVarDecl(const ValueDecl *VD, til::SExpr *E) {
774 maybeUpdateVD(E, VD);
775 LVarIdxMap.insert(std::make_pair(VD, CurrentLVarMap.size()));
776 CurrentLVarMap.makeWritable();
777 CurrentLVarMap.push_back(std::make_pair(VD, E));
778 return E;
779}
780
781// Updates a current variable declaration. (E.g. by assignment)
782til::SExpr *SExprBuilder::updateVarDecl(const ValueDecl *VD, til::SExpr *E) {
783 maybeUpdateVD(E, VD);
784 auto It = LVarIdxMap.find(VD);
785 if (It == LVarIdxMap.end()) {
786 til::SExpr *Ptr = new (Arena) til::LiteralPtr(VD);
787 til::SExpr *St = new (Arena) til::Store(Ptr, E);
788 return St;
789 }
790 CurrentLVarMap.makeWritable();
791 CurrentLVarMap.elem(It->second).second = E;
792 return E;
793}
794
795// Make a Phi node in the current block for the i^th variable in CurrentVarMap.
796// If E != null, sets Phi[CurrentBlockInfo->ArgIndex] = E.
797// If E == null, this is a backedge and will be set later.
798void SExprBuilder::makePhiNodeVar(unsigned i, unsigned NPreds, til::SExpr *E) {
799 unsigned ArgIndex = CurrentBlockInfo->ProcessedPredecessors;
800 assert(ArgIndex > 0 && ArgIndex < NPreds);
801
802 til::SExpr *CurrE = CurrentLVarMap[i].second;
803 if (CurrE->block() == CurrentBB) {
804 // We already have a Phi node in the current block,
805 // so just add the new variable to the Phi node.
806 auto *Ph = dyn_cast<til::Phi>(CurrE);
807 assert(Ph && "Expecting Phi node.");
808 if (E)
809 Ph->values()[ArgIndex] = E;
810 return;
811 }
812
813 // Make a new phi node: phi(..., E)
814 // All phi args up to the current index are set to the current value.
815 til::Phi *Ph = new (Arena) til::Phi(Arena, NPreds);
816 Ph->values().setValues(NPreds, nullptr);
817 for (unsigned PIdx = 0; PIdx < ArgIndex; ++PIdx)
818 Ph->values()[PIdx] = CurrE;
819 if (E)
820 Ph->values()[ArgIndex] = E;
821 Ph->setClangDecl(CurrentLVarMap[i].first);
822 // If E is from a back-edge, or either E or CurrE are incomplete, then
823 // mark this node as incomplete; we may need to remove it later.
824 if (!E || isIncompletePhi(E) || isIncompletePhi(CurrE))
826
827 // Add Phi node to current block, and update CurrentLVarMap[i]
828 CurrentArguments.push_back(Ph);
829 if (Ph->status() == til::Phi::PH_Incomplete)
830 IncompleteArgs.push_back(Ph);
831
832 CurrentLVarMap.makeWritable();
833 CurrentLVarMap.elem(i).second = Ph;
834}
835
836// Merge values from Map into the current variable map.
837// This will construct Phi nodes in the current basic block as necessary.
838void SExprBuilder::mergeEntryMap(LVarDefinitionMap Map) {
839 assert(CurrentBlockInfo && "Not processing a block!");
840
841 if (!CurrentLVarMap.valid()) {
842 // Steal Map, using copy-on-write.
843 CurrentLVarMap = std::move(Map);
844 return;
845 }
846 if (CurrentLVarMap.sameAs(Map))
847 return; // Easy merge: maps from different predecessors are unchanged.
848
849 unsigned NPreds = CurrentBB->numPredecessors();
850 unsigned ESz = CurrentLVarMap.size();
851 unsigned MSz = Map.size();
852 unsigned Sz = std::min(ESz, MSz);
853
854 for (unsigned i = 0; i < Sz; ++i) {
855 if (CurrentLVarMap[i].first != Map[i].first) {
856 // We've reached the end of variables in common.
857 CurrentLVarMap.makeWritable();
858 CurrentLVarMap.downsize(i);
859 break;
860 }
861 if (CurrentLVarMap[i].second != Map[i].second)
862 makePhiNodeVar(i, NPreds, Map[i].second);
863 }
864 if (ESz > MSz) {
865 CurrentLVarMap.makeWritable();
866 CurrentLVarMap.downsize(Map.size());
867 }
868}
869
870// Merge a back edge into the current variable map.
871// This will create phi nodes for all variables in the variable map.
872void SExprBuilder::mergeEntryMapBackEdge() {
873 // We don't have definitions for variables on the backedge, because we
874 // haven't gotten that far in the CFG. Thus, when encountering a back edge,
875 // we conservatively create Phi nodes for all variables. Unnecessary Phi
876 // nodes will be marked as incomplete, and stripped out at the end.
877 //
878 // An Phi node is unnecessary if it only refers to itself and one other
879 // variable, e.g. x = Phi(y, y, x) can be reduced to x = y.
880
881 assert(CurrentBlockInfo && "Not processing a block!");
882
883 if (CurrentBlockInfo->HasBackEdges)
884 return;
885 CurrentBlockInfo->HasBackEdges = true;
886
887 CurrentLVarMap.makeWritable();
888 unsigned Sz = CurrentLVarMap.size();
889 unsigned NPreds = CurrentBB->numPredecessors();
890
891 for (unsigned i = 0; i < Sz; ++i)
892 makePhiNodeVar(i, NPreds, nullptr);
893}
894
895// Update the phi nodes that were initially created for a back edge
896// once the variable definitions have been computed.
897// I.e., merge the current variable map into the phi nodes for Blk.
898void SExprBuilder::mergePhiNodesBackEdge(const CFGBlock *Blk) {
899 til::BasicBlock *BB = lookupBlock(Blk);
900 unsigned ArgIndex = BBInfo[Blk->getBlockID()].ProcessedPredecessors;
901 assert(ArgIndex > 0 && ArgIndex < BB->numPredecessors());
902
903 for (til::SExpr *PE : BB->arguments()) {
904 auto *Ph = dyn_cast_or_null<til::Phi>(PE);
905 assert(Ph && "Expecting Phi Node.");
906 assert(Ph->values()[ArgIndex] == nullptr && "Wrong index for back edge.");
907
908 til::SExpr *E = lookupVarDecl(Ph->clangDecl());
909 assert(E && "Couldn't find local variable for Phi node.");
910 Ph->values()[ArgIndex] = E;
911 }
912}
913
914void SExprBuilder::enterCFG(CFG *Cfg, const NamedDecl *D,
915 const CFGBlock *First) {
916 // Perform initial setup operations.
917 unsigned NBlocks = Cfg->getNumBlockIDs();
918 Scfg = new (Arena) til::SCFG(Arena, NBlocks);
919
920 // allocate all basic blocks immediately, to handle forward references.
921 BBInfo.resize(NBlocks);
922 BlockMap.resize(NBlocks, nullptr);
923 // create map from clang blockID to til::BasicBlocks
924 for (auto *B : *Cfg) {
925 auto *BB = new (Arena) til::BasicBlock(Arena);
926 BB->reserveInstructions(B->size());
927 BlockMap[B->getBlockID()] = BB;
928 }
929
930 CurrentBB = lookupBlock(&Cfg->getEntry());
931 auto Parms = isa<ObjCMethodDecl>(D) ? cast<ObjCMethodDecl>(D)->parameters()
932 : cast<FunctionDecl>(D)->parameters();
933 for (auto *Pm : Parms) {
934 QualType T = Pm->getType();
935 if (!T.isTrivialType(Pm->getASTContext()))
936 continue;
937
938 // Add parameters to local variable map.
939 // FIXME: right now we emulate params with loads; that should be fixed.
940 til::SExpr *Lp = new (Arena) til::LiteralPtr(Pm);
941 til::SExpr *Ld = new (Arena) til::Load(Lp);
942 til::SExpr *V = addStatement(Ld, nullptr, Pm);
943 addVarDecl(Pm, V);
944 }
945}
946
947void SExprBuilder::enterCFGBlock(const CFGBlock *B) {
948 // Initialize TIL basic block and add it to the CFG.
949 CurrentBB = lookupBlock(B);
950 CurrentBB->reservePredecessors(B->pred_size());
951 Scfg->add(CurrentBB);
952
953 CurrentBlockInfo = &BBInfo[B->getBlockID()];
954
955 // CurrentLVarMap is moved to ExitMap on block exit.
956 // FIXME: the entry block will hold function parameters.
957 // assert(!CurrentLVarMap.valid() && "CurrentLVarMap already initialized.");
958}
959
960void SExprBuilder::handlePredecessor(const CFGBlock *Pred) {
961 // Compute CurrentLVarMap on entry from ExitMaps of predecessors
962
963 CurrentBB->addPredecessor(BlockMap[Pred->getBlockID()]);
964 BlockInfo *PredInfo = &BBInfo[Pred->getBlockID()];
965 assert(PredInfo->UnprocessedSuccessors > 0);
966
967 if (--PredInfo->UnprocessedSuccessors == 0)
968 mergeEntryMap(std::move(PredInfo->ExitMap));
969 else
970 mergeEntryMap(PredInfo->ExitMap.clone());
971
972 ++CurrentBlockInfo->ProcessedPredecessors;
973}
974
975void SExprBuilder::handlePredecessorBackEdge(const CFGBlock *Pred) {
976 mergeEntryMapBackEdge();
977}
978
979void SExprBuilder::enterCFGBlockBody(const CFGBlock *B) {
980 // The merge*() methods have created arguments.
981 // Push those arguments onto the basic block.
982 CurrentBB->arguments().reserve(
983 static_cast<unsigned>(CurrentArguments.size()), Arena);
984 for (auto *A : CurrentArguments)
985 CurrentBB->addArgument(A);
986}
987
988void SExprBuilder::handleStatement(const Stmt *S) {
989 til::SExpr *E = translate(S, nullptr);
990 addStatement(E, S);
991}
992
993void SExprBuilder::handleDestructorCall(const VarDecl *VD,
994 const CXXDestructorDecl *DD) {
995 til::SExpr *Sf = new (Arena) til::LiteralPtr(VD);
996 til::SExpr *Dr = new (Arena) til::LiteralPtr(DD);
997 til::SExpr *Ap = new (Arena) til::Apply(Dr, Sf);
998 til::SExpr *E = new (Arena) til::Call(Ap);
999 addStatement(E, nullptr);
1000}
1001
1002void SExprBuilder::exitCFGBlockBody(const CFGBlock *B) {
1003 CurrentBB->instructions().reserve(
1004 static_cast<unsigned>(CurrentInstructions.size()), Arena);
1005 for (auto *V : CurrentInstructions)
1006 CurrentBB->addInstruction(V);
1007
1008 // Create an appropriate terminator
1009 unsigned N = B->succ_size();
1010 auto It = B->succ_begin();
1011 if (N == 1) {
1012 til::BasicBlock *BB = *It ? lookupBlock(*It) : nullptr;
1013 // TODO: set index
1014 unsigned Idx = BB ? BB->findPredecessorIndex(CurrentBB) : 0;
1015 auto *Tm = new (Arena) til::Goto(BB, Idx);
1016 CurrentBB->setTerminator(Tm);
1017 }
1018 else if (N == 2) {
1019 til::SExpr *C = translate(B->getTerminatorCondition(true), nullptr);
1020 til::BasicBlock *BB1 = *It ? lookupBlock(*It) : nullptr;
1021 ++It;
1022 til::BasicBlock *BB2 = *It ? lookupBlock(*It) : nullptr;
1023 // FIXME: make sure these aren't critical edges.
1024 auto *Tm = new (Arena) til::Branch(C, BB1, BB2);
1025 CurrentBB->setTerminator(Tm);
1026 }
1027}
1028
1029void SExprBuilder::handleSuccessor(const CFGBlock *Succ) {
1030 ++CurrentBlockInfo->UnprocessedSuccessors;
1031}
1032
1033void SExprBuilder::handleSuccessorBackEdge(const CFGBlock *Succ) {
1034 mergePhiNodesBackEdge(Succ);
1035 ++BBInfo[Succ->getBlockID()].ProcessedPredecessors;
1036}
1037
1038void SExprBuilder::exitCFGBlock(const CFGBlock *B) {
1039 CurrentArguments.clear();
1040 CurrentInstructions.clear();
1041 CurrentBlockInfo->ExitMap = std::move(CurrentLVarMap);
1042 CurrentBB = nullptr;
1043 CurrentBlockInfo = nullptr;
1044}
1045
1046void SExprBuilder::exitCFG(const CFGBlock *Last) {
1047 for (auto *Ph : IncompleteArgs) {
1048 if (Ph->status() == til::Phi::PH_Incomplete)
1050 }
1051
1052 CurrentArguments.clear();
1053 CurrentInstructions.clear();
1054 IncompleteArgs.clear();
1055}
1056
1057#ifndef NDEBUG
1058namespace {
1059
1060class TILPrinter :
1061 public til::PrettyPrinter<TILPrinter, llvm::raw_ostream> {};
1062
1063} // namespace
1064
1065namespace clang {
1066namespace threadSafety {
1067
1068void printSCFG(CFGWalker &Walker) {
1069 llvm::BumpPtrAllocator Bpa;
1070 til::MemRegionRef Arena(&Bpa);
1071 SExprBuilder SxBuilder(Arena);
1072 til::SCFG *Scfg = SxBuilder.buildCFG(Walker);
1073 TILPrinter::print(Scfg, llvm::errs());
1074}
1075
1076} // namespace threadSafety
1077} // namespace clang
1078#endif // NDEBUG
#define V(N, I)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
static const Decl * getCanonicalDecl(const Decl *D)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines an enumeration for C++ overloaded operators.
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
Defines various enumerations that describe declaration and type specifiers.
static bool isIncompletePhi(const til::SExpr *E)
SExprBuilder::CallingContext CallingContext
static const ValueDecl * getValueDeclFromSExpr(const til::SExpr *E)
static void maybeUpdateVD(til::SExpr *E, const ValueDecl *VD)
static bool hasAnyPointerType(const til::SExpr *E)
static const CXXMethodDecl * getFirstVirtualDecl(const CXXMethodDecl *D)
static std::pair< StringRef, bool > classifyCapability(const TypeDecl &TD)
static bool isCalleeArrow(const Expr *E)
static constexpr std::pair< StringRef, bool > ClassifyCapabilityFallback
C Language Family Type Representation.
Expr * getCond() const
getCond - Return the expression representing the condition for the ?
Definition Expr.h:4467
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Definition Expr.h:4473
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
Definition Expr.h:4479
Expr * getLHS() const
Definition Expr.h:4024
Expr * getRHS() const
Definition Expr.h:4026
Opcode getOpcode() const
Definition Expr.h:4019
succ_iterator succ_begin()
Definition CFG.h:990
unsigned pred_size() const
Definition CFG.h:1011
unsigned getBlockID() const
Definition CFG.h:1111
Stmt * getTerminatorCondition(bool StripParens=true)
Definition CFG.cpp:6378
unsigned succ_size() const
Definition CFG.h:1008
unsigned getNumBlockIDs() const
Returns the total number of BlockIDs allocated (which start at 0).
Definition CFG.h:1409
CXXMethodDecl * getMethodDecl() const
Retrieve the declaration of the called method.
Definition ExprCXX.cpp:741
Expr * getImplicitObjectArgument() const
Retrieve the implicit object argument for the member call.
Definition ExprCXX.cpp:722
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
overridden_method_range overridden_methods() const
Definition DeclCXX.cpp:2778
CXXMethodDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition DeclCXX.h:2225
OverloadedOperatorKind getOperator() const
Returns the kind of overloaded operator that this expression refers to.
Definition ExprCXX.h:114
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3083
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3062
Expr * getCallee()
Definition Expr.h:3026
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition Expr.h:3070
Expr ** getArgs()
Retrieve the call arguments.
Definition Expr.h:3073
arg_range arguments()
Definition Expr.h:3131
CastKind getCastKind() const
Definition Expr.h:3656
Expr * getSubExpr()
Definition Expr.h:3662
bool body_empty() const
Definition Stmt.h:1764
Stmt * body_back()
Definition Stmt.h:1788
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1449
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1272
ValueDecl * getDecl()
Definition Expr.h:1340
const DeclGroupRef getDeclGroup() const
Definition Stmt.h:1629
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
T * getAttr() const
Definition DeclBase.h:573
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:524
bool hasAttr() const
Definition DeclBase.h:577
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition DeclBase.h:978
This represents one expression.
Definition Expr.h:112
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition Expr.cpp:3078
Expr * IgnoreImplicit() LLVM_READONLY
Skip past any implicit AST nodes which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3061
QualType getType() const
Definition Expr.h:144
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3383
Expr * getBase() const
Definition Expr.h:3377
This represents a decl that may have a name.
Definition Decl.h:273
std::string getNameAsString() const
Get a human-readable name for the declaration, even if it is one of the special kinds of names (C++ c...
Definition Decl.h:316
bool isCXXInstanceMember() const
Determine whether the given declaration is an instance member of a C++ class.
Definition Decl.cpp:1962
ObjCIvarDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this field.
Definition DeclObjC.h:1991
ObjCIvarDecl * getDecl()
Definition ExprObjC.h:578
const Expr * getBase() const
Definition ExprObjC.h:582
A (possibly-)qualified type.
Definition TypeBase.h:937
CompoundStmt * getSubStmt()
Definition Expr.h:4548
Stmt - This represents one statement.
Definition Stmt.h:85
StmtClass getStmtClass() const
Definition Stmt.h:1472
Represents a declaration of a type.
Definition Decl.h:3510
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:752
bool isPointerOrReferenceType() const
Definition TypeBase.h:8526
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9101
Expr * getSubExpr() const
Definition Expr.h:2287
Opcode getOpcode() const
Definition Expr.h:2282
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:711
QualType getType() const
Definition Decl.h:722
Represents a variable declaration or definition.
Definition Decl.h:925
VarDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition Decl.cpp:2257
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1207
CapabilityExpr translateAttrExpr(const Expr *AttrExp, const NamedDecl *D, const Expr *DeclExp, til::SExpr *Self=nullptr)
Translate a clang expression in an attribute to a til::SExpr.
til::SExpr * translate(const Stmt *S, CallingContext *Ctx)
til::SExpr * lookupStmt(const Stmt *S)
til::SCFG * buildCFG(CFGWalker &Walker)
til::SExpr * translateVariable(const VarDecl *VD, CallingContext *Ctx)
til::BasicBlock * lookupBlock(const CFGBlock *B)
const InstrArray & arguments() const
unsigned findPredecessorIndex(const BasicBlock *BB) const
Return the index of BB, or Predecessors.size if BB is not a predecessor.
A Literal pointer to an object allocated in memory.
const ValueDecl * clangDecl() const
Return the clang declaration of the variable for this Phi node, if any.
void setClangDecl(const ValueDecl *Cvd)
Set the clang variable associated with this Phi node.
const ValArray & values() const
An SCFG is a control-flow graph.
Base class for AST nodes in the typed intermediate language.
BasicBlock * block() const
Returns the block, if this is an instruction in a basic block, otherwise returns null.
void setValues(unsigned Sz, const T &C)
Placeholder for expressions that cannot be represented in the TIL.
Placeholder for a wildcard that matches any other expression.
void simplifyIncompleteArg(til::Phi *Ph)
TIL_BinaryOpcode
Opcode for binary arithmetic operations.
void printSCFG(CFGWalker &Walker)
std::string getSourceLiteralString(const Expr *CE)
The JSON file list parser is used to communicate input to InstallAPI.
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ Self
'self' clause, allowed on Compute and Combined Constructs, plus 'update'.
nullptr
This class represents a compute construct, representing a 'Kind' of β€˜parallel’, 'serial',...
@ Result
The result type of a method or function.
Definition TypeBase.h:905
const FunctionProtoType * T
CastKind
CastKind - The kind of operation required for a conversion.
U cast(CodeGen::Address addr)
Definition Address.h:327
Encapsulates the lexical context of a function call.
llvm::PointerUnion< const Expr *const *, til::SExpr * > FunArgs
llvm::PointerUnion< const Expr *, til::SExpr * > SelfArg