clang 23.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 // SelfArg may be null for non-method callees (e.g. function pointers).
191 if (!AttrExp) {
192 if (!Ctx.SelfArg)
193 return CapabilityExpr();
194 return translateAttrExpr(cast<const Expr *>(Ctx.SelfArg), nullptr);
195 }
196
197 // For most attributes.
198 return translateAttrExpr(AttrExp, &Ctx);
199}
200
201/// Translate a clang expression in an attribute to a til::SExpr.
202// This assumes a CallingContext has already been created.
204 CallingContext *Ctx) {
205 if (!AttrExp)
206 return CapabilityExpr();
207
208 if (const auto* SLit = dyn_cast<StringLiteral>(AttrExp)) {
209 if (SLit->getString() == "*")
210 // The "*" expr is a universal lock, which essentially turns off
211 // checks until it is removed from the lockset.
212 return CapabilityExpr(new (Arena) til::Wildcard(), StringRef("wildcard"),
213 /*Neg=*/false, /*Reentrant=*/false);
214 else
215 // Ignore other string literals for now.
216 return CapabilityExpr();
217 }
218
219 bool Neg = false;
220 if (const auto *OE = dyn_cast<CXXOperatorCallExpr>(AttrExp)) {
221 if (OE->getOperator() == OO_Exclaim) {
222 Neg = true;
223 AttrExp = OE->getArg(0);
224 }
225 }
226 else if (const auto *UO = dyn_cast<UnaryOperator>(AttrExp)) {
227 if (UO->getOpcode() == UO_LNot) {
228 Neg = true;
229 AttrExp = UO->getSubExpr()->IgnoreImplicit();
230 }
231 }
232
233 const til::SExpr *E = translate(AttrExp, Ctx);
234
235 // Trap mutex expressions like nullptr, or 0.
236 // Any literal value is nonsense.
237 if (!E || isa<til::Literal>(E))
238 return CapabilityExpr();
239
240 // Hack to deal with smart pointers -- strip off top-level pointer casts.
241 if (const auto *CE = dyn_cast<til::Cast>(E)) {
242 if (CE->castOpcode() == til::CAST_objToPtr)
243 E = CE->expr();
244 }
245 return CapabilityExpr(E, AttrExp->getType(), Neg);
246}
247
249 CallingContext *Ctx) {
250 assert(VD);
251
252 // General recursion guard for x = f(x). If we are already in the process of
253 // defining VD, use its pre-assignment value to break the cycle.
254 if (VarsBeingTranslated.contains(VD->getCanonicalDecl()))
255 return new (Arena) til::LiteralPtr(VD);
256
257 // The closure captures state that is updated to correctly translate chains of
258 // aliases. Restore it when we are done with recursive translation.
259 llvm::scope_exit Cleanup([&, RestoreClosure = VarsBeingTranslated.empty()
260 ? LookupLocalVarExpr
261 : nullptr] {
262 VarsBeingTranslated.erase(VD->getCanonicalDecl());
263 if (VarsBeingTranslated.empty())
264 LookupLocalVarExpr = RestoreClosure;
265 });
266 VarsBeingTranslated.insert(VD->getCanonicalDecl());
267
268 QualType Ty = VD->getType();
269 if (!VD->isStaticLocal() && Ty->isPointerType()) {
270 // Substitute local variable aliases with a canonical definition.
271 if (LookupLocalVarExpr) {
272 // Attempt to resolve an alias through the more complex local variable map
273 // lookup. This will fail with complex control-flow graphs (where we
274 // revert to no alias resolution to retain stable variable names).
275 if (const Expr *E = LookupLocalVarExpr(VD)) {
276 til::SExpr *Result = translate(E, Ctx);
277 // Unsupported expression (such as heap allocations) will be undefined;
278 // rather than failing here, we simply revert to the pointer being the
279 // canonical variable.
281 return Result;
282 }
283 }
284 }
285
286 return new (Arena) til::LiteralPtr(VD);
287}
288
289// Translate a clang statement or expression to a TIL expression.
290// Also performs substitution of variables; Ctx provides the context.
291// Dispatches on the type of S.
293 if (!S)
294 return nullptr;
295
296 // Check if S has already been translated and cached.
297 // This handles the lookup of SSA names for DeclRefExprs here.
298 if (til::SExpr *E = lookupStmt(S))
299 return E;
300
301 switch (S->getStmtClass()) {
302 case Stmt::DeclRefExprClass:
303 return translateDeclRefExpr(cast<DeclRefExpr>(S), Ctx);
304 case Stmt::CXXThisExprClass:
305 return translateCXXThisExpr(cast<CXXThisExpr>(S), Ctx);
306 case Stmt::MemberExprClass:
307 return translateMemberExpr(cast<MemberExpr>(S), Ctx);
308 case Stmt::ObjCIvarRefExprClass:
309 return translateObjCIVarRefExpr(cast<ObjCIvarRefExpr>(S), Ctx);
310 case Stmt::CallExprClass:
311 return translateCallExpr(cast<CallExpr>(S), Ctx);
312 case Stmt::CXXMemberCallExprClass:
313 return translateCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), Ctx);
314 case Stmt::CXXOperatorCallExprClass:
315 return translateCXXOperatorCallExpr(cast<CXXOperatorCallExpr>(S), Ctx);
316 case Stmt::UnaryOperatorClass:
317 return translateUnaryOperator(cast<UnaryOperator>(S), Ctx);
318 case Stmt::BinaryOperatorClass:
319 case Stmt::CompoundAssignOperatorClass:
320 return translateBinaryOperator(cast<BinaryOperator>(S), Ctx);
321
322 case Stmt::ArraySubscriptExprClass:
323 return translateArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Ctx);
324 case Stmt::ConditionalOperatorClass:
325 return translateAbstractConditionalOperator(
327 case Stmt::BinaryConditionalOperatorClass:
328 return translateAbstractConditionalOperator(
330
331 // We treat these as no-ops
332 case Stmt::ConstantExprClass:
333 return translate(cast<ConstantExpr>(S)->getSubExpr(), Ctx);
334 case Stmt::ParenExprClass:
335 return translate(cast<ParenExpr>(S)->getSubExpr(), Ctx);
336 case Stmt::ExprWithCleanupsClass:
337 return translate(cast<ExprWithCleanups>(S)->getSubExpr(), Ctx);
338 case Stmt::CXXBindTemporaryExprClass:
339 return translate(cast<CXXBindTemporaryExpr>(S)->getSubExpr(), Ctx);
340 case Stmt::MaterializeTemporaryExprClass:
341 return translate(cast<MaterializeTemporaryExpr>(S)->getSubExpr(), Ctx);
342
343 // Collect all literals
344 case Stmt::CharacterLiteralClass:
345 return new (Arena)
347 case Stmt::CXXNullPtrLiteralExprClass:
348 case Stmt::GNUNullExprClass:
349 return new (Arena) til::LiteralT(nullptr);
350 case Stmt::CXXBoolLiteralExprClass:
351 return new (Arena) til::LiteralT(cast<CXXBoolLiteralExpr>(S)->getValue());
352 case Stmt::IntegerLiteralClass: {
353 const auto *IL = cast<IntegerLiteral>(S);
354 const auto *BT = cast<BuiltinType>(IL->getType());
355 const llvm::APInt &Value = IL->getValue();
356 if (BT->isSignedInteger())
357 return new (Arena) til::LiteralT(Value.getSExtValue());
358 else if (BT->isUnsignedInteger())
359 return new (Arena) til::LiteralT(Value.getZExtValue());
360 else
361 llvm_unreachable("Invalid integer type");
362 }
363 case Stmt::StringLiteralClass:
364 return new (Arena) til::LiteralT(cast<StringLiteral>(S)->getBytes());
365 case Stmt::ObjCStringLiteralClass:
366 return new (Arena)
367 til::LiteralT(cast<ObjCStringLiteral>(S)->getString()->getBytes());
368
369 case Stmt::DeclStmtClass:
370 return translateDeclStmt(cast<DeclStmt>(S), Ctx);
371 case Stmt::StmtExprClass:
372 return translateStmtExpr(cast<StmtExpr>(S), Ctx);
373 default:
374 break;
375 }
376 if (const auto *CE = dyn_cast<CastExpr>(S))
377 return translateCastExpr(CE, Ctx);
378
379 return new (Arena) til::Undefined(S);
380}
381
382/// Helper to extract the canonical parameter declaration from a function or
383/// function pointer. This unwraps pointer and reference types to reach the
384/// underlying function prototype.
385static const ParmVarDecl *getCanonicalParamDecl(const Decl *D, unsigned I) {
386 if (const auto *FD = dyn_cast<FunctionDecl>(D))
387 return FD->getCanonicalDecl()->getParamDecl(I);
388 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
389 return MD->getCanonicalDecl()->getParamDecl(I);
390 if (const auto *DD = dyn_cast<DeclaratorDecl>(D)) {
391 if (auto *TSI = DD->getTypeSourceInfo()) {
392 TypeLoc TL = TSI->getTypeLoc();
393 if (auto RTL = TL.getAsAdjusted<ReferenceTypeLoc>())
394 TL = RTL.getPointeeLoc();
395 // A function pointer can be multiple levels deep.
396 while (auto PTL = TL.getAsAdjusted<PointerTypeLoc>())
397 TL = PTL.getPointeeLoc();
398 if (auto FPTL = TL.getAsAdjusted<FunctionProtoTypeLoc>())
399 if (I < FPTL.getNumParams())
400 return FPTL.getParam(I);
401 }
402 }
403 return nullptr;
404}
405
406til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE,
407 CallingContext *Ctx) {
408 const auto *VD = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl());
409
410 // Function parameters require substitution and/or renaming.
411 if (const auto *PV = dyn_cast<ParmVarDecl>(VD)) {
412 unsigned I = PV->getFunctionScopeIndex();
413 const DeclContext *D = PV->getDeclContext();
414 if (Ctx && Ctx->FunArgs) {
415 const Decl *Canonical = Ctx->AttrDecl->getCanonicalDecl();
416 bool Match = false;
417 if (const auto *FD = dyn_cast<FunctionDecl>(D))
418 Match = (FD->getCanonicalDecl() == Canonical);
419 else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
420 Match = (MD->getCanonicalDecl() == Canonical);
421 else if (getCanonicalParamDecl(Canonical, I) == PV->getCanonicalDecl())
422 Match = true;
423 else
424 llvm_unreachable("ParmVarDecl does not belong to current declaration");
425
426 if (Match) {
427 // Substitute call arguments for references to function parameters
428 if (const Expr *const *FunArgs =
429 dyn_cast<const Expr *const *>(Ctx->FunArgs)) {
430 assert(I < Ctx->NumArgs);
431 return translate(FunArgs[I], Ctx->Prev);
432 }
433
434 assert(I == 0);
435 return cast<til::SExpr *>(Ctx->FunArgs);
436 }
437 }
438 // Map the param back to the param of the original function declaration
439 // for consistent comparisons.
440 if (const auto *PVD = getCanonicalParamDecl(cast<Decl>(D), I))
441 VD = PVD;
442 }
443
444 if (const auto *VarD = dyn_cast<VarDecl>(VD))
445 return translateVariable(VarD, Ctx);
446
447 // For non-local variables, treat it as a reference to a named object.
448 return new (Arena) til::LiteralPtr(VD);
449}
450
451til::SExpr *SExprBuilder::translateCXXThisExpr(const CXXThisExpr *TE,
452 CallingContext *Ctx) {
453 // Substitute for 'this'
454 if (Ctx && Ctx->SelfArg) {
455 if (const auto *SelfArg = dyn_cast<const Expr *>(Ctx->SelfArg))
456 return translate(SelfArg, Ctx->Prev);
457 else
458 return cast<til::SExpr *>(Ctx->SelfArg);
459 }
460 assert(SelfVar && "We have no variable for 'this'!");
461 return SelfVar;
462}
463
465 if (const auto *V = dyn_cast<til::Variable>(E))
466 return V->clangDecl();
467 if (const auto *Ph = dyn_cast<til::Phi>(E))
468 return Ph->clangDecl();
469 if (const auto *P = dyn_cast<til::Project>(E))
470 return P->clangDecl();
471 if (const auto *L = dyn_cast<til::LiteralPtr>(E))
472 return L->clangDecl();
473 return nullptr;
474}
475
476static bool hasAnyPointerType(const til::SExpr *E) {
477 auto *VD = getValueDeclFromSExpr(E);
478 if (VD && VD->getType()->isAnyPointerType())
479 return true;
480 if (const auto *C = dyn_cast<til::Cast>(E))
481 return C->castOpcode() == til::CAST_objToPtr;
482
483 return false;
484}
485
486// Grab the very first declaration of virtual method D
488 while (true) {
489 D = D->getCanonicalDecl();
490 auto OverriddenMethods = D->overridden_methods();
491 if (OverriddenMethods.begin() == OverriddenMethods.end())
492 return D; // Method does not override anything
493 // FIXME: this does not work with multiple inheritance.
494 D = *OverriddenMethods.begin();
495 }
496 return nullptr;
497}
498
499til::SExpr *SExprBuilder::translateMemberExpr(const MemberExpr *ME,
500 CallingContext *Ctx) {
501 til::SExpr *BE = translate(ME->getBase(), Ctx);
502 til::SExpr *E = new (Arena) til::SApply(BE);
503
504 const auto *D = cast<ValueDecl>(ME->getMemberDecl()->getCanonicalDecl());
505 if (const auto *VD = dyn_cast<CXXMethodDecl>(D))
506 D = getFirstVirtualDecl(VD);
507
508 til::Project *P = new (Arena) til::Project(E, D);
509 if (hasAnyPointerType(BE))
510 P->setArrow(true);
511 return P;
512}
513
514til::SExpr *SExprBuilder::translateObjCIVarRefExpr(const ObjCIvarRefExpr *IVRE,
515 CallingContext *Ctx) {
516 til::SExpr *BE = translate(IVRE->getBase(), Ctx);
517 til::SExpr *E = new (Arena) til::SApply(BE);
518
519 const auto *D = cast<ObjCIvarDecl>(IVRE->getDecl()->getCanonicalDecl());
520
521 til::Project *P = new (Arena) til::Project(E, D);
522 if (hasAnyPointerType(BE))
523 P->setArrow(true);
524 return P;
525}
526
527til::SExpr *SExprBuilder::translateCallExpr(const CallExpr *CE,
528 CallingContext *Ctx,
529 const Expr *SelfE) {
530 if (CapabilityExprMode) {
531 // Handle LOCK_RETURNED
532 if (const FunctionDecl *FD = CE->getDirectCallee()) {
533 FD = FD->getMostRecentDecl();
534 if (LockReturnedAttr *At = FD->getAttr<LockReturnedAttr>()) {
535 CallingContext LRCallCtx(Ctx);
536 LRCallCtx.AttrDecl = CE->getDirectCallee();
537 LRCallCtx.SelfArg = SelfE;
538 LRCallCtx.NumArgs = CE->getNumArgs();
539 LRCallCtx.FunArgs = CE->getArgs();
540 return const_cast<til::SExpr *>(
541 translateAttrExpr(At->getArg(), &LRCallCtx).sexpr());
542 }
543 }
544 }
545
546 til::SExpr *E = translate(CE->getCallee(), Ctx);
547 for (const auto *Arg : CE->arguments()) {
548 til::SExpr *A = translate(Arg, Ctx);
549 E = new (Arena) til::Apply(E, A);
550 }
551 return new (Arena) til::Call(E, CE);
552}
553
554til::SExpr *SExprBuilder::translateCXXMemberCallExpr(
555 const CXXMemberCallExpr *ME, CallingContext *Ctx) {
556 if (CapabilityExprMode) {
557 // Ignore calls to get() on smart pointers.
558 if (ME->getMethodDecl()->getNameAsString() == "get" &&
559 ME->getNumArgs() == 0) {
560 auto *E = translate(ME->getImplicitObjectArgument(), Ctx);
561 return new (Arena) til::Cast(til::CAST_objToPtr, E);
562 // return E;
563 }
564 }
565 return translateCallExpr(cast<CallExpr>(ME), Ctx,
567}
568
569til::SExpr *SExprBuilder::translateCXXOperatorCallExpr(
570 const CXXOperatorCallExpr *OCE, CallingContext *Ctx) {
571 if (CapabilityExprMode) {
572 // Ignore operator * and operator -> on smart pointers.
574 if (k == OO_Star || k == OO_Arrow) {
575 auto *E = translate(OCE->getArg(0), Ctx);
576 return new (Arena) til::Cast(til::CAST_objToPtr, E);
577 // return E;
578 }
579 }
580 return translateCallExpr(cast<CallExpr>(OCE), Ctx);
581}
582
583til::SExpr *SExprBuilder::translateUnaryOperator(const UnaryOperator *UO,
584 CallingContext *Ctx) {
585 switch (UO->getOpcode()) {
586 case UO_PostInc:
587 case UO_PostDec:
588 case UO_PreInc:
589 case UO_PreDec:
590 return new (Arena) til::Undefined(UO);
591
592 case UO_AddrOf:
593 if (CapabilityExprMode) {
594 // interpret &Graph::mu_ as an existential.
595 if (const auto *DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr())) {
596 if (DRE->getDecl()->isCXXInstanceMember()) {
597 // This is a pointer-to-member expression, e.g. &MyClass::mu_.
598 // We interpret this syntax specially, as a wildcard.
599 auto *W = new (Arena) til::Wildcard();
600 return new (Arena) til::Project(W, DRE->getDecl());
601 }
602 }
603 }
604 // otherwise, & is a no-op
605 return translate(UO->getSubExpr(), Ctx);
606
607 // We treat these as no-ops
608 case UO_Deref:
609 case UO_Plus:
610 return translate(UO->getSubExpr(), Ctx);
611
612 case UO_Minus:
613 return new (Arena)
614 til::UnaryOp(til::UOP_Minus, translate(UO->getSubExpr(), Ctx));
615 case UO_Not:
616 return new (Arena)
617 til::UnaryOp(til::UOP_BitNot, translate(UO->getSubExpr(), Ctx));
618 case UO_LNot:
619 return new (Arena)
620 til::UnaryOp(til::UOP_LogicNot, translate(UO->getSubExpr(), Ctx));
621
622 // Currently unsupported
623 case UO_Real:
624 case UO_Imag:
625 case UO_Extension:
626 case UO_Coawait:
627 return new (Arena) til::Undefined(UO);
628 }
629 return new (Arena) til::Undefined(UO);
630}
631
632til::SExpr *SExprBuilder::translateBinOp(til::TIL_BinaryOpcode Op,
633 const BinaryOperator *BO,
634 CallingContext *Ctx, bool Reverse) {
635 til::SExpr *E0 = translate(BO->getLHS(), Ctx);
636 til::SExpr *E1 = translate(BO->getRHS(), Ctx);
637 if (Reverse)
638 return new (Arena) til::BinaryOp(Op, E1, E0);
639 else
640 return new (Arena) til::BinaryOp(Op, E0, E1);
641}
642
643til::SExpr *SExprBuilder::translateBinAssign(til::TIL_BinaryOpcode Op,
644 const BinaryOperator *BO,
645 CallingContext *Ctx,
646 bool Assign) {
647 const Expr *LHS = BO->getLHS();
648 const Expr *RHS = BO->getRHS();
649 til::SExpr *E0 = translate(LHS, Ctx);
650 til::SExpr *E1 = translate(RHS, Ctx);
651
652 const ValueDecl *VD = nullptr;
653 til::SExpr *CV = nullptr;
654 if (const auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
655 VD = DRE->getDecl();
656 CV = lookupVarDecl(VD);
657 }
658
659 if (!Assign) {
660 til::SExpr *Arg = CV ? CV : new (Arena) til::Load(E0);
661 E1 = new (Arena) til::BinaryOp(Op, Arg, E1);
662 E1 = addStatement(E1, nullptr, VD);
663 }
664 if (VD && CV)
665 return updateVarDecl(VD, E1);
666 return new (Arena) til::Store(E0, E1);
667}
668
669til::SExpr *SExprBuilder::translateBinaryOperator(const BinaryOperator *BO,
670 CallingContext *Ctx) {
671 switch (BO->getOpcode()) {
672 case BO_PtrMemD:
673 case BO_PtrMemI:
674 return new (Arena) til::Undefined(BO);
675
676 case BO_Mul: return translateBinOp(til::BOP_Mul, BO, Ctx);
677 case BO_Div: return translateBinOp(til::BOP_Div, BO, Ctx);
678 case BO_Rem: return translateBinOp(til::BOP_Rem, BO, Ctx);
679 case BO_Add: return translateBinOp(til::BOP_Add, BO, Ctx);
680 case BO_Sub: return translateBinOp(til::BOP_Sub, BO, Ctx);
681 case BO_Shl: return translateBinOp(til::BOP_Shl, BO, Ctx);
682 case BO_Shr: return translateBinOp(til::BOP_Shr, BO, Ctx);
683 case BO_LT: return translateBinOp(til::BOP_Lt, BO, Ctx);
684 case BO_GT: return translateBinOp(til::BOP_Lt, BO, Ctx, true);
685 case BO_LE: return translateBinOp(til::BOP_Leq, BO, Ctx);
686 case BO_GE: return translateBinOp(til::BOP_Leq, BO, Ctx, true);
687 case BO_EQ: return translateBinOp(til::BOP_Eq, BO, Ctx);
688 case BO_NE: return translateBinOp(til::BOP_Neq, BO, Ctx);
689 case BO_Cmp: return translateBinOp(til::BOP_Cmp, BO, Ctx);
690 case BO_And: return translateBinOp(til::BOP_BitAnd, BO, Ctx);
691 case BO_Xor: return translateBinOp(til::BOP_BitXor, BO, Ctx);
692 case BO_Or: return translateBinOp(til::BOP_BitOr, BO, Ctx);
693 case BO_LAnd: return translateBinOp(til::BOP_LogicAnd, BO, Ctx);
694 case BO_LOr: return translateBinOp(til::BOP_LogicOr, BO, Ctx);
695
696 case BO_Assign: return translateBinAssign(til::BOP_Eq, BO, Ctx, true);
697 case BO_MulAssign: return translateBinAssign(til::BOP_Mul, BO, Ctx);
698 case BO_DivAssign: return translateBinAssign(til::BOP_Div, BO, Ctx);
699 case BO_RemAssign: return translateBinAssign(til::BOP_Rem, BO, Ctx);
700 case BO_AddAssign: return translateBinAssign(til::BOP_Add, BO, Ctx);
701 case BO_SubAssign: return translateBinAssign(til::BOP_Sub, BO, Ctx);
702 case BO_ShlAssign: return translateBinAssign(til::BOP_Shl, BO, Ctx);
703 case BO_ShrAssign: return translateBinAssign(til::BOP_Shr, BO, Ctx);
704 case BO_AndAssign: return translateBinAssign(til::BOP_BitAnd, BO, Ctx);
705 case BO_XorAssign: return translateBinAssign(til::BOP_BitXor, BO, Ctx);
706 case BO_OrAssign: return translateBinAssign(til::BOP_BitOr, BO, Ctx);
707
708 case BO_Comma:
709 // The clang CFG should have already processed both sides.
710 return translate(BO->getRHS(), Ctx);
711 }
712 return new (Arena) til::Undefined(BO);
713}
714
715til::SExpr *SExprBuilder::translateCastExpr(const CastExpr *CE,
716 CallingContext *Ctx) {
717 CastKind K = CE->getCastKind();
718 switch (K) {
719 case CK_LValueToRValue: {
720 if (const auto *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
721 til::SExpr *E0 = lookupVarDecl(DRE->getDecl());
722 if (E0)
723 return E0;
724 }
725 til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
726 return E0;
727 // FIXME!! -- get Load working properly
728 // return new (Arena) til::Load(E0);
729 }
730 case CK_NoOp:
731 case CK_DerivedToBase:
732 case CK_UncheckedDerivedToBase:
733 case CK_ArrayToPointerDecay:
734 case CK_FunctionToPointerDecay: {
735 til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
736 return E0;
737 }
738 default: {
739 // FIXME: handle different kinds of casts.
740 til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
741 if (CapabilityExprMode)
742 return E0;
743 return new (Arena) til::Cast(til::CAST_none, E0);
744 }
745 }
746}
747
749SExprBuilder::translateArraySubscriptExpr(const ArraySubscriptExpr *E,
750 CallingContext *Ctx) {
751 til::SExpr *E0 = translate(E->getBase(), Ctx);
752 til::SExpr *E1 = translate(E->getIdx(), Ctx);
753 return new (Arena) til::ArrayIndex(E0, E1);
754}
755
757SExprBuilder::translateAbstractConditionalOperator(
758 const AbstractConditionalOperator *CO, CallingContext *Ctx) {
759 auto *C = translate(CO->getCond(), Ctx);
760 auto *T = translate(CO->getTrueExpr(), Ctx);
761 auto *E = translate(CO->getFalseExpr(), Ctx);
762 return new (Arena) til::IfThenElse(C, T, E);
763}
764
766SExprBuilder::translateDeclStmt(const DeclStmt *S, CallingContext *Ctx) {
767 DeclGroupRef DGrp = S->getDeclGroup();
768 for (auto *I : DGrp) {
769 if (auto *VD = dyn_cast_or_null<VarDecl>(I)) {
770 Expr *E = VD->getInit();
771 til::SExpr* SE = translate(E, Ctx);
772
773 // Add local variables with trivial type to the variable map
774 QualType T = VD->getType();
775 if (T.isTrivialType(VD->getASTContext()))
776 return addVarDecl(VD, SE);
777 else {
778 // TODO: add alloca
779 }
780 }
781 }
782 return nullptr;
783}
784
785til::SExpr *SExprBuilder::translateStmtExpr(const StmtExpr *SE,
786 CallingContext *Ctx) {
787 // The value of a statement expression is the value of the last statement,
788 // which must be an expression.
789 const CompoundStmt *CS = SE->getSubStmt();
790 return CS->body_empty() ? new (Arena) til::Undefined(SE)
791 : translate(CS->body_back(), Ctx);
792}
793
794// If (E) is non-trivial, then add it to the current basic block, and
795// update the statement map so that S refers to E. Returns a new variable
796// that refers to E.
797// If E is trivial returns E.
798til::SExpr *SExprBuilder::addStatement(til::SExpr* E, const Stmt *S,
799 const ValueDecl *VD) {
800 if (!E || !CurrentBB || E->block() || til::ThreadSafetyTIL::isTrivial(E))
801 return E;
802 if (VD)
803 E = new (Arena) til::Variable(E, VD);
804 CurrentInstructions.push_back(E);
805 if (S)
806 insertStmt(S, E);
807 return E;
808}
809
810// Returns the current value of VD, if known, and nullptr otherwise.
811til::SExpr *SExprBuilder::lookupVarDecl(const ValueDecl *VD) {
812 auto It = LVarIdxMap.find(VD);
813 if (It != LVarIdxMap.end()) {
814 assert(CurrentLVarMap[It->second].first == VD);
815 return CurrentLVarMap[It->second].second;
816 }
817 return nullptr;
818}
819
820// if E is a til::Variable, update its clangDecl.
821static void maybeUpdateVD(til::SExpr *E, const ValueDecl *VD) {
822 if (!E)
823 return;
824 if (auto *V = dyn_cast<til::Variable>(E)) {
825 if (!V->clangDecl())
826 V->setClangDecl(VD);
827 }
828}
829
830// Adds a new variable declaration.
831til::SExpr *SExprBuilder::addVarDecl(const ValueDecl *VD, til::SExpr *E) {
832 maybeUpdateVD(E, VD);
833 LVarIdxMap.insert(std::make_pair(VD, CurrentLVarMap.size()));
834 CurrentLVarMap.makeWritable();
835 CurrentLVarMap.push_back(std::make_pair(VD, E));
836 return E;
837}
838
839// Updates a current variable declaration. (E.g. by assignment)
840til::SExpr *SExprBuilder::updateVarDecl(const ValueDecl *VD, til::SExpr *E) {
841 maybeUpdateVD(E, VD);
842 auto It = LVarIdxMap.find(VD);
843 if (It == LVarIdxMap.end()) {
844 til::SExpr *Ptr = new (Arena) til::LiteralPtr(VD);
845 til::SExpr *St = new (Arena) til::Store(Ptr, E);
846 return St;
847 }
848 CurrentLVarMap.makeWritable();
849 CurrentLVarMap.elem(It->second).second = E;
850 return E;
851}
852
853// Make a Phi node in the current block for the i^th variable in CurrentVarMap.
854// If E != null, sets Phi[CurrentBlockInfo->ArgIndex] = E.
855// If E == null, this is a backedge and will be set later.
856void SExprBuilder::makePhiNodeVar(unsigned i, unsigned NPreds, til::SExpr *E) {
857 unsigned ArgIndex = CurrentBlockInfo->ProcessedPredecessors;
858 assert(ArgIndex > 0 && ArgIndex < NPreds);
859
860 til::SExpr *CurrE = CurrentLVarMap[i].second;
861 if (CurrE->block() == CurrentBB) {
862 // We already have a Phi node in the current block,
863 // so just add the new variable to the Phi node.
864 auto *Ph = dyn_cast<til::Phi>(CurrE);
865 assert(Ph && "Expecting Phi node.");
866 if (E)
867 Ph->values()[ArgIndex] = E;
868 return;
869 }
870
871 // Make a new phi node: phi(..., E)
872 // All phi args up to the current index are set to the current value.
873 til::Phi *Ph = new (Arena) til::Phi(Arena, NPreds);
874 Ph->values().setValues(NPreds, nullptr);
875 for (unsigned PIdx = 0; PIdx < ArgIndex; ++PIdx)
876 Ph->values()[PIdx] = CurrE;
877 if (E)
878 Ph->values()[ArgIndex] = E;
879 Ph->setClangDecl(CurrentLVarMap[i].first);
880 // If E is from a back-edge, or either E or CurrE are incomplete, then
881 // mark this node as incomplete; we may need to remove it later.
882 if (!E || isIncompletePhi(E) || isIncompletePhi(CurrE))
884
885 // Add Phi node to current block, and update CurrentLVarMap[i]
886 CurrentArguments.push_back(Ph);
887 if (Ph->status() == til::Phi::PH_Incomplete)
888 IncompleteArgs.push_back(Ph);
889
890 CurrentLVarMap.makeWritable();
891 CurrentLVarMap.elem(i).second = Ph;
892}
893
894// Merge values from Map into the current variable map.
895// This will construct Phi nodes in the current basic block as necessary.
896void SExprBuilder::mergeEntryMap(LVarDefinitionMap Map) {
897 assert(CurrentBlockInfo && "Not processing a block!");
898
899 if (!CurrentLVarMap.valid()) {
900 // Steal Map, using copy-on-write.
901 CurrentLVarMap = std::move(Map);
902 return;
903 }
904 if (CurrentLVarMap.sameAs(Map))
905 return; // Easy merge: maps from different predecessors are unchanged.
906
907 unsigned NPreds = CurrentBB->numPredecessors();
908 unsigned ESz = CurrentLVarMap.size();
909 unsigned MSz = Map.size();
910 unsigned Sz = std::min(ESz, MSz);
911
912 for (unsigned i = 0; i < Sz; ++i) {
913 if (CurrentLVarMap[i].first != Map[i].first) {
914 // We've reached the end of variables in common.
915 CurrentLVarMap.makeWritable();
916 CurrentLVarMap.downsize(i);
917 break;
918 }
919 if (CurrentLVarMap[i].second != Map[i].second)
920 makePhiNodeVar(i, NPreds, Map[i].second);
921 }
922 if (ESz > MSz) {
923 CurrentLVarMap.makeWritable();
924 CurrentLVarMap.downsize(Map.size());
925 }
926}
927
928// Merge a back edge into the current variable map.
929// This will create phi nodes for all variables in the variable map.
930void SExprBuilder::mergeEntryMapBackEdge() {
931 // We don't have definitions for variables on the backedge, because we
932 // haven't gotten that far in the CFG. Thus, when encountering a back edge,
933 // we conservatively create Phi nodes for all variables. Unnecessary Phi
934 // nodes will be marked as incomplete, and stripped out at the end.
935 //
936 // An Phi node is unnecessary if it only refers to itself and one other
937 // variable, e.g. x = Phi(y, y, x) can be reduced to x = y.
938
939 assert(CurrentBlockInfo && "Not processing a block!");
940
941 if (CurrentBlockInfo->HasBackEdges)
942 return;
943 CurrentBlockInfo->HasBackEdges = true;
944
945 CurrentLVarMap.makeWritable();
946 unsigned Sz = CurrentLVarMap.size();
947 unsigned NPreds = CurrentBB->numPredecessors();
948
949 for (unsigned i = 0; i < Sz; ++i)
950 makePhiNodeVar(i, NPreds, nullptr);
951}
952
953// Update the phi nodes that were initially created for a back edge
954// once the variable definitions have been computed.
955// I.e., merge the current variable map into the phi nodes for Blk.
956void SExprBuilder::mergePhiNodesBackEdge(const CFGBlock *Blk) {
957 til::BasicBlock *BB = lookupBlock(Blk);
958 unsigned ArgIndex = BBInfo[Blk->getBlockID()].ProcessedPredecessors;
959 assert(ArgIndex > 0 && ArgIndex < BB->numPredecessors());
960
961 for (til::SExpr *PE : BB->arguments()) {
962 auto *Ph = dyn_cast_or_null<til::Phi>(PE);
963 assert(Ph && "Expecting Phi Node.");
964 assert(Ph->values()[ArgIndex] == nullptr && "Wrong index for back edge.");
965
966 til::SExpr *E = lookupVarDecl(Ph->clangDecl());
967 assert(E && "Couldn't find local variable for Phi node.");
968 Ph->values()[ArgIndex] = E;
969 }
970}
971
972void SExprBuilder::enterCFG(CFG *Cfg, const NamedDecl *D,
973 const CFGBlock *First) {
974 // Perform initial setup operations.
975 unsigned NBlocks = Cfg->getNumBlockIDs();
976 Scfg = new (Arena) til::SCFG(Arena, NBlocks);
977
978 // allocate all basic blocks immediately, to handle forward references.
979 BBInfo.resize(NBlocks);
980 BlockMap.resize(NBlocks, nullptr);
981 // create map from clang blockID to til::BasicBlocks
982 for (auto *B : *Cfg) {
983 auto *BB = new (Arena) til::BasicBlock(Arena);
984 BB->reserveInstructions(B->size());
985 BlockMap[B->getBlockID()] = BB;
986 }
987
988 CurrentBB = lookupBlock(&Cfg->getEntry());
989 auto Parms = isa<ObjCMethodDecl>(D) ? cast<ObjCMethodDecl>(D)->parameters()
990 : cast<FunctionDecl>(D)->parameters();
991 for (auto *Pm : Parms) {
992 QualType T = Pm->getType();
993 if (!T.isTrivialType(Pm->getASTContext()))
994 continue;
995
996 // Add parameters to local variable map.
997 // FIXME: right now we emulate params with loads; that should be fixed.
998 til::SExpr *Lp = new (Arena) til::LiteralPtr(Pm);
999 til::SExpr *Ld = new (Arena) til::Load(Lp);
1000 til::SExpr *V = addStatement(Ld, nullptr, Pm);
1001 addVarDecl(Pm, V);
1002 }
1003}
1004
1005void SExprBuilder::enterCFGBlock(const CFGBlock *B) {
1006 // Initialize TIL basic block and add it to the CFG.
1007 CurrentBB = lookupBlock(B);
1008 CurrentBB->reservePredecessors(B->pred_size());
1009 Scfg->add(CurrentBB);
1010
1011 CurrentBlockInfo = &BBInfo[B->getBlockID()];
1012
1013 // CurrentLVarMap is moved to ExitMap on block exit.
1014 // FIXME: the entry block will hold function parameters.
1015 // assert(!CurrentLVarMap.valid() && "CurrentLVarMap already initialized.");
1016}
1017
1018void SExprBuilder::handlePredecessor(const CFGBlock *Pred) {
1019 // Compute CurrentLVarMap on entry from ExitMaps of predecessors
1020
1021 CurrentBB->addPredecessor(BlockMap[Pred->getBlockID()]);
1022 BlockInfo *PredInfo = &BBInfo[Pred->getBlockID()];
1023 assert(PredInfo->UnprocessedSuccessors > 0);
1024
1025 if (--PredInfo->UnprocessedSuccessors == 0)
1026 mergeEntryMap(std::move(PredInfo->ExitMap));
1027 else
1028 mergeEntryMap(PredInfo->ExitMap.clone());
1029
1030 ++CurrentBlockInfo->ProcessedPredecessors;
1031}
1032
1033void SExprBuilder::handlePredecessorBackEdge(const CFGBlock *Pred) {
1034 mergeEntryMapBackEdge();
1035}
1036
1037void SExprBuilder::enterCFGBlockBody(const CFGBlock *B) {
1038 // The merge*() methods have created arguments.
1039 // Push those arguments onto the basic block.
1040 CurrentBB->arguments().reserve(
1041 static_cast<unsigned>(CurrentArguments.size()), Arena);
1042 for (auto *A : CurrentArguments)
1043 CurrentBB->addArgument(A);
1044}
1045
1046void SExprBuilder::handleStatement(const Stmt *S) {
1047 til::SExpr *E = translate(S, nullptr);
1048 addStatement(E, S);
1049}
1050
1051void SExprBuilder::handleDestructorCall(const VarDecl *VD,
1052 const CXXDestructorDecl *DD) {
1053 til::SExpr *Sf = new (Arena) til::LiteralPtr(VD);
1054 til::SExpr *Dr = new (Arena) til::LiteralPtr(DD);
1055 til::SExpr *Ap = new (Arena) til::Apply(Dr, Sf);
1056 til::SExpr *E = new (Arena) til::Call(Ap);
1057 addStatement(E, nullptr);
1058}
1059
1060void SExprBuilder::exitCFGBlockBody(const CFGBlock *B) {
1061 CurrentBB->instructions().reserve(
1062 static_cast<unsigned>(CurrentInstructions.size()), Arena);
1063 for (auto *V : CurrentInstructions)
1064 CurrentBB->addInstruction(V);
1065
1066 // Create an appropriate terminator
1067 unsigned N = B->succ_size();
1068 auto It = B->succ_begin();
1069 if (N == 1) {
1070 til::BasicBlock *BB = *It ? lookupBlock(*It) : nullptr;
1071 // TODO: set index
1072 unsigned Idx = BB ? BB->findPredecessorIndex(CurrentBB) : 0;
1073 auto *Tm = new (Arena) til::Goto(BB, Idx);
1074 CurrentBB->setTerminator(Tm);
1075 }
1076 else if (N == 2) {
1077 til::SExpr *C = translate(B->getTerminatorCondition(true), nullptr);
1078 til::BasicBlock *BB1 = *It ? lookupBlock(*It) : nullptr;
1079 ++It;
1080 til::BasicBlock *BB2 = *It ? lookupBlock(*It) : nullptr;
1081 // FIXME: make sure these aren't critical edges.
1082 auto *Tm = new (Arena) til::Branch(C, BB1, BB2);
1083 CurrentBB->setTerminator(Tm);
1084 }
1085}
1086
1087void SExprBuilder::handleSuccessor(const CFGBlock *Succ) {
1088 ++CurrentBlockInfo->UnprocessedSuccessors;
1089}
1090
1091void SExprBuilder::handleSuccessorBackEdge(const CFGBlock *Succ) {
1092 mergePhiNodesBackEdge(Succ);
1093 ++BBInfo[Succ->getBlockID()].ProcessedPredecessors;
1094}
1095
1096void SExprBuilder::exitCFGBlock(const CFGBlock *B) {
1097 CurrentArguments.clear();
1098 CurrentInstructions.clear();
1099 CurrentBlockInfo->ExitMap = std::move(CurrentLVarMap);
1100 CurrentBB = nullptr;
1101 CurrentBlockInfo = nullptr;
1102}
1103
1104void SExprBuilder::exitCFG(const CFGBlock *Last) {
1105 for (auto *Ph : IncompleteArgs) {
1106 if (Ph->status() == til::Phi::PH_Incomplete)
1108 }
1109
1110 CurrentArguments.clear();
1111 CurrentInstructions.clear();
1112 IncompleteArgs.clear();
1113}
1114
1115#ifndef NDEBUG
1116namespace {
1117
1118class TILPrinter :
1119 public til::PrettyPrinter<TILPrinter, llvm::raw_ostream> {};
1120
1121} // namespace
1122
1123namespace clang {
1124namespace threadSafety {
1125
1126void printSCFG(CFGWalker &Walker) {
1127 llvm::BumpPtrAllocator Bpa;
1128 til::MemRegionRef Arena(&Bpa);
1129 SExprBuilder SxBuilder(Arena);
1130 til::SCFG *Scfg = SxBuilder.buildCFG(Walker);
1131 TILPrinter::print(Scfg, llvm::errs());
1132}
1133
1134} // namespace threadSafety
1135} // namespace clang
1136#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.
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 ParmVarDecl * getCanonicalParamDecl(const Decl *D, unsigned I)
Helper to extract the canonical parameter declaration from a function or function pointer.
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:4534
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Definition Expr.h:4540
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
Definition Expr.h:4546
Expr * getLHS() const
Definition Expr.h:4091
Expr * getRHS() const
Definition Expr.h:4093
Opcode getOpcode() const
Definition Expr.h:4086
succ_iterator succ_begin()
Definition CFG.h:1017
unsigned pred_size() const
Definition CFG.h:1038
const Stmt * getTerminatorCondition(bool StripParens=true) const
Definition CFG.cpp:6515
unsigned getBlockID() const
Definition CFG.h:1134
unsigned succ_size() const
Definition CFG.h:1035
unsigned getNumBlockIDs() const
Returns the total number of BlockIDs allocated (which start at 0).
Definition CFG.h:1443
CXXMethodDecl * getMethodDecl() const
Retrieve the declaration of the called method.
Definition ExprCXX.cpp:743
Expr * getImplicitObjectArgument() const
Retrieve the implicit object argument for the member call.
Definition ExprCXX.cpp:724
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2132
overridden_method_range overridden_methods() const
Definition DeclCXX.cpp:2830
CXXMethodDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition DeclCXX.h:2241
OverloadedOperatorKind getOperator() const
Returns the kind of overloaded operator that this expression refers to.
Definition ExprCXX.h:115
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3150
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3129
Expr * getCallee()
Definition Expr.h:3093
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition Expr.h:3137
Expr ** getArgs()
Retrieve the call arguments.
Definition Expr.h:3140
arg_range arguments()
Definition Expr.h:3198
CastKind getCastKind() const
Definition Expr.h:3723
Expr * getSubExpr()
Definition Expr.h:3729
bool body_empty() const
Definition Stmt.h:1790
Stmt * body_back()
Definition Stmt.h:1814
ValueDecl * getDecl()
Definition Expr.h:1341
const DeclGroupRef getDeclGroup() const
Definition Stmt.h:1655
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
T * getAttr() const
Definition DeclBase.h:581
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:547
bool hasAttr() const
Definition DeclBase.h:585
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition DeclBase.h:991
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:3095
Expr * IgnoreImplicit() LLVM_READONLY
Skip past any implicit AST nodes which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3078
QualType getType() const
Definition Expr.h:144
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3450
Expr * getBase() const
Definition Expr.h:3444
This represents a decl that may have a name.
Definition Decl.h:274
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:317
bool isCXXInstanceMember() const
Determine whether the given declaration is an instance member of a C++ class.
Definition Decl.cpp:1975
ObjCIvarDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this field.
Definition DeclObjC.h:1991
ObjCIvarDecl * getDecl()
Definition ExprObjC.h:610
const Expr * getBase() const
Definition ExprObjC.h:614
Represents a parameter to a function.
Definition Decl.h:1808
Wrapper for source info for pointers.
Definition TypeLoc.h:1513
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isTrivialType(const ASTContext &Context) const
Return true if this is a trivial type per (C++0x [basic.types]p9)
Definition Type.cpp:2852
CompoundStmt * getSubStmt()
Definition Expr.h:4615
Stmt - This represents one statement.
Definition Stmt.h:86
StmtClass getStmtClass() const
Definition Stmt.h:1499
Represents a declaration of a type.
Definition Decl.h:3531
Base wrapper for a particular "section" of type source info.
Definition TypeLoc.h:59
T getAsAdjusted() const
Convert to the specified TypeLoc type, returning a null TypeLoc if this TypeLoc is not of the desired...
Definition TypeLoc.h:2735
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isPointerType() const
Definition TypeBase.h:8673
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
bool isPointerOrReferenceType() const
Definition TypeBase.h:8677
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9266
Expr * getSubExpr() const
Definition Expr.h:2288
Opcode getOpcode() const
Definition Expr.h:2283
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:924
VarDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition Decl.cpp:2236
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1206
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.
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
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.
@ Match
This is not an overload because the signature exactly matches an existing declaration.
Definition Sema.h:830
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
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