clang 24.0.0git
ThreadSafetyTraverse.h
Go to the documentation of this file.
1//===- ThreadSafetyTraverse.h -----------------------------------*- C++ -*-===//
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// This file defines a framework for doing generic traversals and rewriting
10// operations over the Thread Safety TIL.
11//
12// UNDER CONSTRUCTION. USE AT YOUR OWN RISK.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYTRAVERSE_H
17#define LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYTRAVERSE_H
18
19#include "clang/AST/Decl.h"
20#include "clang/AST/Expr.h"
23#include "clang/Basic/LLVM.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/Support/Casting.h"
26#include <cstdint>
27#include <ostream>
28
29namespace clang {
30namespace threadSafety {
31namespace til {
32
33// Defines an interface used to traverse SExprs. Traversals have been made as
34// generic as possible, and are intended to handle any kind of pass over the
35// AST, e.g. visitors, copying, non-destructive rewriting, destructive
36// (in-place) rewriting, hashing, typing, etc.
37//
38// Traversals implement the functional notion of a "fold" operation on SExprs.
39// Each SExpr class provides a traverse method, which does the following:
40// * e->traverse(v):
41// // compute a result r_i for each subexpression e_i
42// for (i = 1..n) r_i = v.traverse(e_i);
43// // combine results into a result for e, where X is the class of e
44// return v.reduceX(*e, r_1, .. r_n).
45//
46// A visitor can control the traversal by overriding the following methods:
47// * v.traverse(e):
48// return v.traverseByCase(e), which returns v.traverseX(e)
49// * v.traverseX(e): (X is the class of e)
50// return e->traverse(v).
51// * v.reduceX(*e, r_1, .. r_n):
52// compute a result for a node of type X
53//
54// The reduceX methods control the kind of traversal (visitor, copy, etc.).
55// They are defined in derived classes.
56//
57// Class R defines the basic interface types (R_SExpr).
58template <class Self, class R>
59class Traversal {
60public:
61 Self *self() { return static_cast<Self *>(this); }
62
63 // Traverse an expression -- returning a result of type R_SExpr.
64 // Override this method to do something for every expression, regardless
65 // of which kind it is.
66 // E is a reference, so this can be use for in-place updates.
67 // The type T must be a subclass of SExpr.
68 template <class T>
69 typename R::R_SExpr traverse(T* &E, typename R::R_Ctx Ctx) {
70 return traverseSExpr(E, Ctx);
71 }
72
73 // Override this method to do something for every expression.
74 // Does not allow in-place updates.
75 typename R::R_SExpr traverseSExpr(SExpr *E, typename R::R_Ctx Ctx) {
76 return traverseByCase(E, Ctx);
77 }
78
79 // Helper method to call traverseX(e) on the appropriate type.
80 typename R::R_SExpr traverseByCase(SExpr *E, typename R::R_Ctx Ctx) {
81 switch (E->opcode()) {
82#define TIL_OPCODE_DEF(X) \
83 case COP_##X: \
84 return self()->traverse##X(cast<X>(E), Ctx);
85#include "ThreadSafetyOps.def"
86#undef TIL_OPCODE_DEF
87 }
88 return self()->reduceNull();
89 }
90
91// Traverse e, by static dispatch on the type "X" of e.
92// Override these methods to do something for a particular kind of term.
93#define TIL_OPCODE_DEF(X) \
94 typename R::R_SExpr traverse##X(X *e, typename R::R_Ctx Ctx) { \
95 return e->traverse(*self(), Ctx); \
96 }
97#include "ThreadSafetyOps.def"
98#undef TIL_OPCODE_DEF
99};
100
101// Base class for simple reducers that don't much care about the context.
103public:
105 // Ordinary subexpressions.
107
108 // Declarations (e.g. function bodies).
110
111 // Expressions that require lazy evaluation.
113
114 // Type expressions.
116 };
117
118 // R_Ctx defines a "context" for the traversal, which encodes information
119 // about where a term appears. This can be used to encoding the
120 // "current continuation" for CPS transforms, or other information.
122
123 // Create context for an ordinary subexpression.
125
126 // Create context for a subexpression that occurs in a declaration position
127 // (e.g. function body).
128 R_Ctx declCtx(R_Ctx Ctx) { return TRV_Decl; }
129
130 // Create context for a subexpression that occurs in a position that
131 // should be reduced lazily. (e.g. code body).
132 R_Ctx lazyCtx(R_Ctx Ctx) { return TRV_Lazy; }
133
134 // Create context for a subexpression that occurs in a type position.
135 R_Ctx typeCtx(R_Ctx Ctx) { return TRV_Type; }
136};
137
138// Base class for traversals that rewrite an SExpr to another SExpr.
140public:
141 // R_SExpr is the result type for a traversal.
142 // A copy or non-destructive rewrite returns a newly allocated term.
143 using R_SExpr = SExpr *;
145
146 // Container is a minimal interface used to store results when traversing
147 // SExprs of variable arity, such as Phi, Goto, and SCFG.
148 template <class T> class Container {
149 public:
150 // Allocate a new container with a capacity for n elements.
151 Container(CopyReducerBase &S, unsigned N) : Elems(S.Arena, N) {}
152
153 // Push a new element onto the container.
154 void push_back(T E) { Elems.push_back(E); }
155
157 };
158
160
161protected:
163};
164
165// Base class for visit traversals.
167public:
168 // A visitor returns a bool, representing success or failure.
169 using R_SExpr = bool;
171
172 // A visitor "container" is a single bool, which accumulates success.
173 template <class T> class Container {
174 public:
175 bool Success = true;
176
177 Container(VisitReducerBase &S, unsigned N) {}
178
179 void push_back(bool E) { Success = Success && E; }
180 };
181};
182
183// Implements a traversal that visits each subexpression, and returns either
184// true or false.
185template <class Self>
186class VisitReducer : public Traversal<Self, VisitReducerBase>,
187 public VisitReducerBase {
188public:
189 VisitReducer() = default;
190
191public:
192 R_SExpr reduceNull() { return true; }
193 R_SExpr reduceUndefined(Undefined &Orig) { return true; }
194 R_SExpr reduceWildcard(Wildcard &Orig) { return true; }
195
196 template<class T>
197 R_SExpr reduceLiteralT(LiteralT<T> &Orig) { return true; }
198 R_SExpr reduceLiteralPtr(Literal &Orig) { return true; }
199
201 return Nvd && E0;
202 }
203
205 return Nvd && E0;
206 }
207
209 return E0 && E1;
210 }
211
213 return E0 && E1;
214 }
215
217 return E0 && E1;
218 }
219
221 return E0 && E1;
222 }
223
224 R_SExpr reduceProject(Project &Orig, R_SExpr E0) { return E0; }
225 R_SExpr reduceCall(Call &Orig, R_SExpr E0) { return E0; }
226 R_SExpr reduceAlloc(Alloc &Orig, R_SExpr E0) { return E0; }
227 R_SExpr reduceLoad(Load &Orig, R_SExpr E0) { return E0; }
228 R_SExpr reduceStore(Store &Orig, R_SExpr E0, R_SExpr E1) { return E0 && E1; }
229
231 return E0 && E1;
232 }
233
235 return E0 && E1;
236 }
237
238 R_SExpr reduceUnaryOp(UnaryOp &Orig, R_SExpr E0) { return E0; }
239
241 return E0 && E1;
242 }
243
244 R_SExpr reduceCast(Cast &Orig, R_SExpr E0) { return E0; }
245
247 return Bbs.Success;
248 }
249
252 return (As.Success && Is.Success && T);
253 }
254
256 return As.Success;
257 }
258
260 return true;
261 }
262
264 return C;
265 }
266
268 return E;
269 }
270
272 return true;
273 }
274
276 return C && T && E;
277 }
278
280 return Nvd && B;
281 }
282
283 Variable *enterScope(Variable &Orig, R_SExpr E0) { return &Orig; }
284 void exitScope(const Variable &Orig) {}
285 void enterCFG(SCFG &Cfg) {}
286 void exitCFG(SCFG &Cfg) {}
289
290 Variable *reduceVariableRef(Variable *Ovd) { return Ovd; }
292
293public:
295 Success = Success && this->traverseByCase(E);
296 return Success;
297 }
298
299 static bool visit(SExpr *E) {
300 Self Visitor;
301 return Visitor.traverse(E, TRV_Normal);
302 }
303
304private:
305 bool Success;
306};
307
308// Basic class for comparison operations over expressions.
309template <typename Self>
311protected:
312 Self *self() { return reinterpret_cast<Self *>(this); }
313
314public:
315 bool compare(const SExpr *E1, const SExpr *E2) {
316 if (E1->opcode() != E2->opcode())
317 return false;
318 switch (E1->opcode()) {
319#define TIL_OPCODE_DEF(X) \
320 case COP_##X: \
321 return cast<X>(E1)->compare(cast<X>(E2), *self());
322#include "ThreadSafetyOps.def"
323#undef TIL_OPCODE_DEF
324 }
325 return false;
326 }
327};
328
329class EqualsComparator : public Comparator<EqualsComparator> {
330public:
331 // Result type for the comparison, e.g. bool for simple equality,
332 // or int for lexigraphic comparison (-1, 0, 1). Must have one value which
333 // denotes "true".
334 using CType = bool;
335
336 CType trueResult() { return true; }
337 bool notTrue(CType ct) { return !ct; }
338
339 bool compareIntegers(uint64_t i, uint64_t j) { return i == j; }
340 bool compareStrings (StringRef s, StringRef r) { return s == r; }
341 bool comparePointers(const void* P, const void* Q) { return P == Q; }
342
343 // TODO -- handle alpha-renaming of variables
344 void enterScope(const Variable *V1, const Variable *V2) {}
345 void leaveScope() {}
346
347 bool compareVariableRefs(const Variable *V1, const Variable *V2) {
348 return V1 == V2;
349 }
350
351 static bool compareExprs(const SExpr *E1, const SExpr* E2) {
353 return Eq.compare(E1, E2);
354 }
355};
356
357class MatchComparator : public Comparator<MatchComparator> {
358public:
359 // Result type for the comparison, e.g. bool for simple equality,
360 // or int for lexigraphic comparison (-1, 0, 1). Must have one value which
361 // denotes "true".
362 using CType = bool;
363
364 CType trueResult() { return true; }
365 bool notTrue(CType ct) { return !ct; }
366
367 bool compareIntegers(uint64_t i, uint64_t j) { return i == j; }
368 bool compareStrings (StringRef s, StringRef r) { return s == r; }
369 bool comparePointers(const void *P, const void *Q) { return P == Q; }
370
371 bool compare(const SExpr *E1, const SExpr *E2) {
372 // Wildcards match anything.
373 if (E1->opcode() == COP_Wildcard || E2->opcode() == COP_Wildcard)
374 return true;
375 // otherwise normal equality.
376 return Comparator::compare(E1, E2);
377 }
378
379 // TODO -- handle alpha-renaming of variables
380 void enterScope(const Variable* V1, const Variable* V2) {}
381 void leaveScope() {}
382
383 bool compareVariableRefs(const Variable* V1, const Variable* V2) {
384 return V1 == V2;
385 }
386
387 static bool compareExprs(const SExpr *E1, const SExpr* E2) {
388 MatchComparator Matcher;
389 return Matcher.compare(E1, E2);
390 }
391};
392
393// inline std::ostream& operator<<(std::ostream& SS, StringRef R) {
394// return SS.write(R.data(), R.size());
395// }
396
397// Pretty printer for TIL expressions
398template <typename Self, typename StreamType>
400private:
401 // Print exprs in C-like syntax.
402 bool CStyle;
403
404public:
405 PrettyPrinter(bool CS = true) : CStyle(CS) {}
406
407 static void print(const SExpr *E, StreamType &SS) {
408 Self printer;
409 printer.printSExpr(E, SS, Prec_MAX);
410 }
411
412protected:
413 Self *self() { return reinterpret_cast<Self *>(this); }
414
415 void newline(StreamType &SS) {
416 SS << "\n";
417 }
418
419 // TODO: further distinguish between binary operations.
420 static const unsigned Prec_Atom = 0;
421 static const unsigned Prec_Postfix = 1;
422 static const unsigned Prec_Unary = 2;
423 static const unsigned Prec_Binary = 3;
424 static const unsigned Prec_Other = 4;
425 static const unsigned Prec_Decl = 5;
426 static const unsigned Prec_MAX = 6;
427
428 // Return the precedence of a given node, for use in pretty printing.
429 unsigned precedence(const SExpr *E) {
430 switch (E->opcode()) {
431 case COP_Future: return Prec_Atom;
432 case COP_Undefined: return Prec_Atom;
433 case COP_Wildcard: return Prec_Atom;
434
435 case COP_Literal: return Prec_Atom;
436 case COP_LiteralPtr: return Prec_Atom;
437 case COP_Variable: return Prec_Atom;
438 case COP_Function: return Prec_Decl;
439 case COP_SFunction: return Prec_Decl;
440 case COP_Code: return Prec_Decl;
441 case COP_Field: return Prec_Decl;
442
443 case COP_Apply: return Prec_Postfix;
444 case COP_SApply: return Prec_Postfix;
445 case COP_Project: return Prec_Postfix;
446
447 case COP_Call: return Prec_Postfix;
448 case COP_Alloc: return Prec_Other;
449 case COP_Load: return Prec_Postfix;
450 case COP_Store: return Prec_Other;
451 case COP_ArrayIndex: return Prec_Postfix;
452 case COP_ArrayAdd: return Prec_Postfix;
453
454 case COP_UnaryOp: return Prec_Unary;
455 case COP_BinaryOp: return Prec_Binary;
456 case COP_Cast: return Prec_Atom;
457
458 case COP_SCFG: return Prec_Decl;
459 case COP_BasicBlock: return Prec_MAX;
460 case COP_Phi: return Prec_Atom;
461 case COP_Goto: return Prec_Atom;
462 case COP_Branch: return Prec_Atom;
463 case COP_Return: return Prec_Other;
464
465 case COP_Identifier: return Prec_Atom;
466 case COP_IfThenElse: return Prec_Other;
467 case COP_Let: return Prec_Decl;
468 }
469 return Prec_MAX;
470 }
471
472 void printBlockLabel(StreamType & SS, const BasicBlock *BB, int index) {
473 if (!BB) {
474 SS << "BB_null";
475 return;
476 }
477 SS << "BB_";
478 SS << BB->blockID();
479 if (index >= 0) {
480 SS << ":";
481 SS << index;
482 }
483 }
484
485 void printSExpr(const SExpr *E, StreamType &SS, unsigned P, bool Sub=true) {
486 if (!E) {
487 self()->printNull(SS);
488 return;
489 }
490 if (Sub && E->block() && E->opcode() != COP_Variable) {
491 SS << "_x" << E->id();
492 return;
493 }
494 if (self()->precedence(E) > P) {
495 // Wrap expr in () if necessary.
496 SS << "(";
497 self()->printSExpr(E, SS, Prec_MAX);
498 SS << ")";
499 return;
500 }
501
502 switch (E->opcode()) {
503#define TIL_OPCODE_DEF(X) \
504 case COP_##X: \
505 self()->print##X(cast<X>(E), SS); \
506 return;
507#include "ThreadSafetyOps.def"
508#undef TIL_OPCODE_DEF
509 }
510 }
511
512 void printNull(StreamType &SS) {
513 SS << "#null";
514 }
515
516 void printFuture(const Future *E, StreamType &SS) {
517 self()->printSExpr(E->maybeGetResult(), SS, Prec_Atom);
518 }
519
520 void printUndefined(const Undefined *E, StreamType &SS) {
521 SS << "#undefined";
522 }
523
524 void printWildcard(const Wildcard *E, StreamType &SS) {
525 SS << "*";
526 }
527
528 void printLiteral(const Literal *E, StreamType &SS) {
529 ValueType VT = E->valueType();
530 switch (VT.Base) {
532 if (E->as<bool>().value())
533 SS << "true";
534 else
535 SS << "false";
536 return;
538 CharacterLiteral::print(E->as<char32_t>().value(),
540 return;
542 SS << E->as<int64_t>().value();
543 return;
545 SS << E->as<uint64_t>().value();
546 return;
548 SS << '\"' << E->as<StringRef>().value() << '\"';
549 return;
551 assert(E->as<std::nullptr_t>().value() == nullptr);
552 SS << "nullptr";
553 return;
554 }
555 llvm_unreachable("Invalid BaseType");
556 }
557
558 void printLiteralPtr(const LiteralPtr *E, StreamType &SS) {
559 if (const NamedDecl *D = E->clangDecl())
560 SS << D->getNameAsString();
561 else
562 SS << "<temporary>";
563 }
564
565 void printVariable(const Variable *V, StreamType &SS, bool IsVarDecl=false) {
566 if (CStyle && V->kind() == Variable::VK_SFun)
567 SS << "this";
568 else
569 SS << V->name() << V->id();
570 }
571
572 void printFunction(const Function *E, StreamType &SS, unsigned sugared = 0) {
573 switch (sugared) {
574 default:
575 SS << "\\("; // Lambda
576 break;
577 case 1:
578 SS << "("; // Slot declarations
579 break;
580 case 2:
581 SS << ", "; // Curried functions
582 break;
583 }
584 self()->printVariable(E->variableDecl(), SS, true);
585 SS << ": ";
586 self()->printSExpr(E->variableDecl()->definition(), SS, Prec_MAX);
587
588 const SExpr *B = E->body();
589 if (B && B->opcode() == COP_Function)
590 self()->printFunction(cast<Function>(B), SS, 2);
591 else {
592 SS << ")";
593 self()->printSExpr(B, SS, Prec_Decl);
594 }
595 }
596
597 void printSFunction(const SFunction *E, StreamType &SS) {
598 SS << "@";
599 self()->printVariable(E->variableDecl(), SS, true);
600 SS << " ";
601 self()->printSExpr(E->body(), SS, Prec_Decl);
602 }
603
604 void printCode(const Code *E, StreamType &SS) {
605 SS << ": ";
606 self()->printSExpr(E->returnType(), SS, Prec_Decl-1);
607 SS << " -> ";
608 self()->printSExpr(E->body(), SS, Prec_Decl);
609 }
610
611 void printField(const Field *E, StreamType &SS) {
612 SS << ": ";
613 self()->printSExpr(E->range(), SS, Prec_Decl-1);
614 SS << " = ";
615 self()->printSExpr(E->body(), SS, Prec_Decl);
616 }
617
618 void printApply(const Apply *E, StreamType &SS, bool sugared = false) {
619 const SExpr *F = E->fun();
620 if (F->opcode() == COP_Apply) {
621 printApply(cast<Apply>(F), SS, true);
622 SS << ", ";
623 } else {
624 self()->printSExpr(F, SS, Prec_Postfix);
625 SS << "(";
626 }
627 self()->printSExpr(E->arg(), SS, Prec_MAX);
628 if (!sugared)
629 SS << ")$";
630 }
631
632 void printSApply(const SApply *E, StreamType &SS) {
633 self()->printSExpr(E->sfun(), SS, Prec_Postfix);
634 if (E->isDelegation()) {
635 SS << "@(";
636 self()->printSExpr(E->arg(), SS, Prec_MAX);
637 SS << ")";
638 }
639 }
640
641 // An anonymous struct/union member has no name to print. This includes the
642 // implicit member -fms-extensions adds for a tagged anonymous struct.
643 static bool isAnonymousProjection(const Project *E) {
644 return E->clangDecl()->getDeclName().isEmpty();
645 }
646
647 // Qualify with the nearest named record, skipping anonymous ones.
648 static void printQualifiedSlotName(const Project *E, StreamType &SS) {
649 const DeclContext *DC = E->clangDecl()->getDeclContext();
650 while (const auto *RD = dyn_cast<RecordDecl>(DC)) {
651 if (!RD->isAnonymousStructOrUnion())
652 break;
653 DC = RD->getParent();
654 }
655 if (const auto *ND = dyn_cast<NamedDecl>(DC)) {
656 ND->printQualifiedName(SS);
657 SS << "::";
658 }
659 SS << E->slotName();
660 }
661
662 static void printSlotName(const Project *E, StreamType &SS) {
663 if (E->isQualified())
665 else
666 SS << E->slotName();
667 }
668
669 void printProject(const Project *E, StreamType &SS) {
670 if (isAnonymousProjection(E)) {
671 self()->printSExpr(E->record(), SS, Prec_Postfix);
672 return;
673 }
674 // Skip anonymous members below this one too, keeping any arrow that
675 // reached them, so that "s-><anon>.lock" prints as "s->lock".
676 bool Arrow = E->isArrow();
677 const SExpr *Rec = E->record();
678 while (const auto *SAP = dyn_cast<SApply>(Rec)) {
679 const auto *P = dyn_cast<Project>(SAP->sfun());
680 if (!P || !isAnonymousProjection(P))
681 break;
682 Arrow |= P->isArrow();
683 Rec = P->record();
684 }
685 // Print only the field when the record has no name: `this` in C style, or
686 // the unnamed object of a namespace-scope anonymous union.
687 if (const auto *SAP = dyn_cast<SApply>(Rec)) {
688 const auto *V = dyn_cast<Variable>(SAP->sfun());
689 const auto *L = dyn_cast<LiteralPtr>(SAP->sfun());
690 if ((CStyle && V && V->kind() == Variable::VK_SFun) ||
691 (L && L->clangDecl() && L->clangDecl()->getDeclName().isEmpty())) {
692 printSlotName(E, SS);
693 return;
694 }
695 }
696 if (CStyle && isa<Wildcard>(Rec)) {
697 // handle existentials
698 SS << "&";
700 return;
701 }
702 self()->printSExpr(Rec, SS, Prec_Postfix);
703 if (CStyle && Arrow)
704 SS << "->";
705 else
706 SS << ".";
707 printSlotName(E, SS);
708 }
709
710 void printCall(const Call *E, StreamType &SS) {
711 const SExpr *T = E->target();
712 if (T->opcode() == COP_Apply) {
713 self()->printApply(cast<Apply>(T), SS, true);
714 SS << ")";
715 }
716 else {
717 self()->printSExpr(T, SS, Prec_Postfix);
718 SS << "()";
719 }
720 }
721
722 void printAlloc(const Alloc *E, StreamType &SS) {
723 SS << "new ";
724 self()->printSExpr(E->dataType(), SS, Prec_Other-1);
725 }
726
727 void printLoad(const Load *E, StreamType &SS) {
728 self()->printSExpr(E->pointer(), SS, Prec_Postfix);
729 if (!CStyle)
730 SS << "^";
731 }
732
733 void printStore(const Store *E, StreamType &SS) {
734 self()->printSExpr(E->destination(), SS, Prec_Other-1);
735 SS << " := ";
736 self()->printSExpr(E->source(), SS, Prec_Other-1);
737 }
738
739 void printArrayIndex(const ArrayIndex *E, StreamType &SS) {
740 self()->printSExpr(E->array(), SS, Prec_Postfix);
741 SS << "[";
742 self()->printSExpr(E->index(), SS, Prec_MAX);
743 SS << "]";
744 }
745
746 void printArrayAdd(const ArrayAdd *E, StreamType &SS) {
747 self()->printSExpr(E->array(), SS, Prec_Postfix);
748 SS << " + ";
749 self()->printSExpr(E->index(), SS, Prec_Atom);
750 }
751
752 void printUnaryOp(const UnaryOp *E, StreamType &SS) {
754 self()->printSExpr(E->expr(), SS, Prec_Unary);
755 }
756
757 void printBinaryOp(const BinaryOp *E, StreamType &SS) {
758 self()->printSExpr(E->expr0(), SS, Prec_Binary-1);
759 SS << " " << getBinaryOpcodeString(E->binaryOpcode()) << " ";
760 self()->printSExpr(E->expr1(), SS, Prec_Binary-1);
761 }
762
763 void printCast(const Cast *E, StreamType &SS) {
764 if (!CStyle) {
765 SS << "cast[";
766 switch (E->castOpcode()) {
767 case CAST_none:
768 SS << "none";
769 break;
770 case CAST_extendNum:
771 SS << "extendNum";
772 break;
773 case CAST_truncNum:
774 SS << "truncNum";
775 break;
776 case CAST_toFloat:
777 SS << "toFloat";
778 break;
779 case CAST_toInt:
780 SS << "toInt";
781 break;
782 case CAST_objToPtr:
783 SS << "objToPtr";
784 break;
785 }
786 SS << "](";
787 self()->printSExpr(E->expr(), SS, Prec_Unary);
788 SS << ")";
789 return;
790 }
791 self()->printSExpr(E->expr(), SS, Prec_Unary);
792 }
793
794 void printSCFG(const SCFG *E, StreamType &SS) {
795 SS << "CFG {\n";
796 for (const auto *BBI : *E)
797 printBasicBlock(BBI, SS);
798 SS << "}";
799 newline(SS);
800 }
801
802 void printBBInstr(const SExpr *E, StreamType &SS) {
803 bool Sub = false;
804 if (E->opcode() == COP_Variable) {
805 const auto *V = cast<Variable>(E);
806 SS << "let " << V->name() << V->id() << " = ";
807 E = V->definition();
808 Sub = true;
809 }
810 else if (E->opcode() != COP_Store) {
811 SS << "let _x" << E->id() << " = ";
812 }
813 self()->printSExpr(E, SS, Prec_MAX, Sub);
814 SS << ";";
815 newline(SS);
816 }
817
818 void printBasicBlock(const BasicBlock *E, StreamType &SS) {
819 SS << "BB_" << E->blockID() << ":";
820 if (E->parent())
821 SS << " BB_" << E->parent()->blockID();
822 newline(SS);
823
824 for (const auto *A : E->arguments())
825 printBBInstr(A, SS);
826
827 for (const auto *I : E->instructions())
828 printBBInstr(I, SS);
829
830 const SExpr *T = E->terminator();
831 if (T) {
832 self()->printSExpr(T, SS, Prec_MAX, false);
833 SS << ";";
834 newline(SS);
835 }
836 newline(SS);
837 }
838
839 void printPhi(const Phi *E, StreamType &SS) {
840 SS << "phi(";
841 if (E->status() == Phi::PH_SingleVal)
842 self()->printSExpr(E->values()[0], SS, Prec_MAX);
843 else {
844 unsigned i = 0;
845 for (const auto *V : E->values()) {
846 if (i++ > 0)
847 SS << ", ";
848 self()->printSExpr(V, SS, Prec_MAX);
849 }
850 }
851 SS << ")";
852 }
853
854 void printGoto(const Goto *E, StreamType &SS) {
855 SS << "goto ";
856 printBlockLabel(SS, E->targetBlock(), E->index());
857 }
858
859 void printBranch(const Branch *E, StreamType &SS) {
860 SS << "branch (";
861 self()->printSExpr(E->condition(), SS, Prec_MAX);
862 SS << ") ";
863 printBlockLabel(SS, E->thenBlock(), -1);
864 SS << " ";
865 printBlockLabel(SS, E->elseBlock(), -1);
866 }
867
868 void printReturn(const Return *E, StreamType &SS) {
869 SS << "return ";
870 self()->printSExpr(E->returnValue(), SS, Prec_Other);
871 }
872
873 void printIdentifier(const Identifier *E, StreamType &SS) {
874 SS << E->name();
875 }
876
877 void printIfThenElse(const IfThenElse *E, StreamType &SS) {
878 if (CStyle) {
880 SS << " ? ";
881 printSExpr(E->thenExpr(), SS, Prec_Unary);
882 SS << " : ";
883 printSExpr(E->elseExpr(), SS, Prec_Unary);
884 return;
885 }
886 SS << "if (";
887 printSExpr(E->condition(), SS, Prec_MAX);
888 SS << ") then ";
889 printSExpr(E->thenExpr(), SS, Prec_Other);
890 SS << " else ";
891 printSExpr(E->elseExpr(), SS, Prec_Other);
892 }
893
894 void printLet(const Let *E, StreamType &SS) {
895 SS << "let ";
896 printVariable(E->variableDecl(), SS, true);
897 SS << " = ";
899 SS << "; ";
900 printSExpr(E->body(), SS, Prec_Decl-1);
901 }
902};
903
904class StdPrinter : public PrettyPrinter<StdPrinter, llvm::raw_ostream> {};
905
906} // namespace til
907} // namespace threadSafety
908} // namespace clang
909
910#endif // LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYTRAVERSE_H
#define V(N, I)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
static void print(unsigned val, CharacterLiteralKind Kind, raw_ostream &OS)
Definition Expr.cpp:1030
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1468
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2113
DeclContext * getDeclContext()
Definition DeclBase.h:458
bool isEmpty() const
Evaluates true when this declaration name is empty.
This represents a decl that may have a name.
Definition Decl.h:276
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:342
Allocate memory for a new value on the heap or stack.
Apply an argument to a function.
Pointer arithmetic, restricted to arrays only.
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.
int blockID() const
Returns the block ID. Every block has a unique ID in the CFG.
const InstrArray & arguments() const
const Terminator * terminator() const
const BasicBlock * parent() const
Simple arithmetic binary operations, e.g.
TIL_BinaryOpcode binaryOpcode() const
A conditional branch to two other blocks.
const BasicBlock * elseBlock() const
const BasicBlock * thenBlock() const
Call a function (after all arguments have been applied).
TIL_CastOpcode castOpcode() const
A block of code – e.g. the body of a function.
bool compare(const SExpr *E1, const SExpr *E2)
bool comparePointers(const void *P, const void *Q)
bool compareStrings(StringRef s, StringRef r)
void enterScope(const Variable *V1, const Variable *V2)
bool compareVariableRefs(const Variable *V1, const Variable *V2)
static bool compareExprs(const SExpr *E1, const SExpr *E2)
A typed, writable location in memory.
A function – a.k.a.
Placeholder for an expression that has not yet been created.
Jump to another basic block.
unsigned index() const
Returns the index into the.
const BasicBlock * targetBlock() const
A Literal pointer to an object allocated in memory.
const ValueDecl * clangDecl() const
const LiteralT< T > & as() const
Load a value from memory.
bool compareVariableRefs(const Variable *V1, const Variable *V2)
bool comparePointers(const void *P, const void *Q)
static bool compareExprs(const SExpr *E1, const SExpr *E2)
bool compare(const SExpr *E1, const SExpr *E2)
bool compareStrings(StringRef s, StringRef r)
void enterScope(const Variable *V1, const Variable *V2)
Phi Node, for code in SSA form.
const ValArray & values() const
static void printSlotName(const Project *E, StreamType &SS)
void printFuture(const Future *E, StreamType &SS)
void printCall(const Call *E, StreamType &SS)
void printApply(const Apply *E, StreamType &SS, bool sugared=false)
void printProject(const Project *E, StreamType &SS)
void printWildcard(const Wildcard *E, StreamType &SS)
void printBranch(const Branch *E, StreamType &SS)
void printLet(const Let *E, StreamType &SS)
void printLiteral(const Literal *E, StreamType &SS)
void printField(const Field *E, StreamType &SS)
void printUnaryOp(const UnaryOp *E, StreamType &SS)
void printUndefined(const Undefined *E, StreamType &SS)
void printIdentifier(const Identifier *E, StreamType &SS)
static void print(const SExpr *E, StreamType &SS)
void printReturn(const Return *E, StreamType &SS)
void printVariable(const Variable *V, StreamType &SS, bool IsVarDecl=false)
void printCode(const Code *E, StreamType &SS)
void printStore(const Store *E, StreamType &SS)
static bool isAnonymousProjection(const Project *E)
void printCast(const Cast *E, StreamType &SS)
void printIfThenElse(const IfThenElse *E, StreamType &SS)
void printBasicBlock(const BasicBlock *E, StreamType &SS)
void printLiteralPtr(const LiteralPtr *E, StreamType &SS)
void printGoto(const Goto *E, StreamType &SS)
void printBBInstr(const SExpr *E, StreamType &SS)
void printAlloc(const Alloc *E, StreamType &SS)
void printPhi(const Phi *E, StreamType &SS)
void printSCFG(const SCFG *E, StreamType &SS)
void printFunction(const Function *E, StreamType &SS, unsigned sugared=0)
static void printQualifiedSlotName(const Project *E, StreamType &SS)
void printBinaryOp(const BinaryOp *E, StreamType &SS)
void printArrayAdd(const ArrayAdd *E, StreamType &SS)
void printSFunction(const SFunction *E, StreamType &SS)
void printArrayIndex(const ArrayIndex *E, StreamType &SS)
void printSApply(const SApply *E, StreamType &SS)
void printLoad(const Load *E, StreamType &SS)
void printBlockLabel(StreamType &SS, const BasicBlock *BB, int index)
void printSExpr(const SExpr *E, StreamType &SS, unsigned P, bool Sub=true)
Project a named slot from a C++ struct or class.
const ValueDecl * clangDecl() const
bool isQualified() const
Set when the record is not the field's record, nor derived from it: the owning object could not be re...
Return from the enclosing function, passing the return value to the caller.
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.
unsigned id() const
Returns the instruction ID for this expression.
A self-applicable function.
Store a value to memory.
R::R_SExpr traverse(T *&E, typename R::R_Ctx Ctx)
R::R_SExpr traverseSExpr(SExpr *E, typename R::R_Ctx Ctx)
R::R_SExpr traverseByCase(SExpr *E, typename R::R_Ctx Ctx)
Simple arithmetic unary operations, e.g.
TIL_UnaryOpcode unaryOpcode() const
Placeholder for expressions that cannot be represented in the TIL.
SExpr * definition()
Return the definition of the variable.
@ VK_SFun
SFunction (self) parameter.
Variable * enterScope(Variable &Orig, R_SExpr E0)
R_SExpr reduceBinaryOp(BinaryOp &Orig, R_SExpr E0, R_SExpr E1)
R_SExpr reduceStore(Store &Orig, R_SExpr E0, R_SExpr E1)
R_SExpr reduceProject(Project &Orig, R_SExpr E0)
R_SExpr reduceLet(Let &Orig, Variable *Nvd, R_SExpr B)
R_SExpr reduceArrayAdd(Store &Orig, R_SExpr E0, R_SExpr E1)
R_SExpr reduceAlloc(Alloc &Orig, R_SExpr E0)
R_SExpr reduceUnaryOp(UnaryOp &Orig, R_SExpr E0)
R_SExpr reduceSCFG(SCFG &Orig, Container< BasicBlock * > Bbs)
R_SExpr reduceSApply(SApply &Orig, R_SExpr E0, R_SExpr E1)
R_BasicBlock reduceBasicBlock(BasicBlock &Orig, Container< R_SExpr > &As, Container< R_SExpr > &Is, R_SExpr T)
BasicBlock * reduceBasicBlockRef(BasicBlock *Obb)
R_SExpr reduceGoto(Goto &Orig, BasicBlock *B)
bool traverse(SExpr *E, TraversalKind K=TRV_Normal)
R_SExpr reduceField(Field &Orig, R_SExpr E0, R_SExpr E1)
R_SExpr reducePhi(Phi &Orig, Container< R_SExpr > &As)
R_SExpr reduceLoad(Load &Orig, R_SExpr E0)
R_SExpr reduceCode(Code &Orig, R_SExpr E0, R_SExpr E1)
R_SExpr reduceSFunction(SFunction &Orig, Variable *Nvd, R_SExpr E0)
R_SExpr reduceApply(Apply &Orig, R_SExpr E0, R_SExpr E1)
R_SExpr reduceFunction(Function &Orig, Variable *Nvd, R_SExpr E0)
R_SExpr reduceLiteralT(LiteralT< T > &Orig)
R_SExpr reduceBranch(Branch &O, R_SExpr C, BasicBlock *B0, BasicBlock *B1)
R_SExpr reduceReturn(Return &O, R_SExpr E)
R_SExpr reduceCast(Cast &Orig, R_SExpr E0)
R_SExpr reduceCall(Call &Orig, R_SExpr E0)
R_SExpr reduceArrayIndex(Store &Orig, R_SExpr E0, R_SExpr E1)
R_SExpr reduceIfThenElse(IfThenElse &Orig, R_SExpr C, R_SExpr T, R_SExpr E)
Placeholder for a wildcard that matches any other expression.
StringRef getBinaryOpcodeString(TIL_BinaryOpcode Op)
Return the name of a binary opcode.
StringRef getUnaryOpcodeString(TIL_UnaryOpcode Op)
Return the name of a unary opcode.
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ Success
Annotation was successful.
Definition Parser.h:66
@ Self
'self' clause, allowed on Compute and Combined Constructs, plus 'update'.
U cast(CodeGen::Address addr)
Definition Address.h:327
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t int64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
ValueTypes are data types that can actually be held in registers.