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