clang 22.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"
22#include "clang/Basic/LLVM.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/Support/Casting.h"
25#include <cstdint>
26#include <ostream>
27
28namespace clang {
29namespace threadSafety {
30namespace til {
31
32// Defines an interface used to traverse SExprs. Traversals have been made as
33// generic as possible, and are intended to handle any kind of pass over the
34// AST, e.g. visitors, copying, non-destructive rewriting, destructive
35// (in-place) rewriting, hashing, typing, etc.
36//
37// Traversals implement the functional notion of a "fold" operation on SExprs.
38// Each SExpr class provides a traverse method, which does the following:
39// * e->traverse(v):
40// // compute a result r_i for each subexpression e_i
41// for (i = 1..n) r_i = v.traverse(e_i);
42// // combine results into a result for e, where X is the class of e
43// return v.reduceX(*e, r_1, .. r_n).
44//
45// A visitor can control the traversal by overriding the following methods:
46// * v.traverse(e):
47// return v.traverseByCase(e), which returns v.traverseX(e)
48// * v.traverseX(e): (X is the class of e)
49// return e->traverse(v).
50// * v.reduceX(*e, r_1, .. r_n):
51// compute a result for a node of type X
52//
53// The reduceX methods control the kind of traversal (visitor, copy, etc.).
54// They are defined in derived classes.
55//
56// Class R defines the basic interface types (R_SExpr).
57template <class Self, class R>
58class Traversal {
59public:
60 Self *self() { return static_cast<Self *>(this); }
61
62 // Traverse an expression -- returning a result of type R_SExpr.
63 // Override this method to do something for every expression, regardless
64 // of which kind it is.
65 // E is a reference, so this can be use for in-place updates.
66 // The type T must be a subclass of SExpr.
67 template <class T>
68 typename R::R_SExpr traverse(T* &E, typename R::R_Ctx Ctx) {
69 return traverseSExpr(E, Ctx);
70 }
71
72 // Override this method to do something for every expression.
73 // Does not allow in-place updates.
74 typename R::R_SExpr traverseSExpr(SExpr *E, typename R::R_Ctx Ctx) {
75 return traverseByCase(E, Ctx);
76 }
77
78 // Helper method to call traverseX(e) on the appropriate type.
79 typename R::R_SExpr traverseByCase(SExpr *E, typename R::R_Ctx Ctx) {
80 switch (E->opcode()) {
81#define TIL_OPCODE_DEF(X) \
82 case COP_##X: \
83 return self()->traverse##X(cast<X>(E), Ctx);
84#include "ThreadSafetyOps.def"
85#undef TIL_OPCODE_DEF
86 }
87 return self()->reduceNull();
88 }
89
90// Traverse e, by static dispatch on the type "X" of e.
91// Override these methods to do something for a particular kind of term.
92#define TIL_OPCODE_DEF(X) \
93 typename R::R_SExpr traverse##X(X *e, typename R::R_Ctx Ctx) { \
94 return e->traverse(*self(), Ctx); \
95 }
96#include "ThreadSafetyOps.def"
97#undef TIL_OPCODE_DEF
98};
99
100// Base class for simple reducers that don't much care about the context.
102public:
104 // Ordinary subexpressions.
106
107 // Declarations (e.g. function bodies).
109
110 // Expressions that require lazy evaluation.
112
113 // Type expressions.
115 };
116
117 // R_Ctx defines a "context" for the traversal, which encodes information
118 // about where a term appears. This can be used to encoding the
119 // "current continuation" for CPS transforms, or other information.
121
122 // Create context for an ordinary subexpression.
124
125 // Create context for a subexpression that occurs in a declaration position
126 // (e.g. function body).
127 R_Ctx declCtx(R_Ctx Ctx) { return TRV_Decl; }
128
129 // Create context for a subexpression that occurs in a position that
130 // should be reduced lazily. (e.g. code body).
131 R_Ctx lazyCtx(R_Ctx Ctx) { return TRV_Lazy; }
132
133 // Create context for a subexpression that occurs in a type position.
134 R_Ctx typeCtx(R_Ctx Ctx) { return TRV_Type; }
135};
136
137// Base class for traversals that rewrite an SExpr to another SExpr.
139public:
140 // R_SExpr is the result type for a traversal.
141 // A copy or non-destructive rewrite returns a newly allocated term.
142 using R_SExpr = SExpr *;
144
145 // Container is a minimal interface used to store results when traversing
146 // SExprs of variable arity, such as Phi, Goto, and SCFG.
147 template <class T> class Container {
148 public:
149 // Allocate a new container with a capacity for n elements.
150 Container(CopyReducerBase &S, unsigned N) : Elems(S.Arena, N) {}
151
152 // Push a new element onto the container.
153 void push_back(T E) { Elems.push_back(E); }
154
156 };
157
159
160protected:
162};
163
164// Base class for visit traversals.
166public:
167 // A visitor returns a bool, representing success or failure.
168 using R_SExpr = bool;
170
171 // A visitor "container" is a single bool, which accumulates success.
172 template <class T> class Container {
173 public:
174 bool Success = true;
175
176 Container(VisitReducerBase &S, unsigned N) {}
177
178 void push_back(bool E) { Success = Success && E; }
179 };
180};
181
182// Implements a traversal that visits each subexpression, and returns either
183// true or false.
184template <class Self>
185class VisitReducer : public Traversal<Self, VisitReducerBase>,
186 public VisitReducerBase {
187public:
188 VisitReducer() = default;
189
190public:
191 R_SExpr reduceNull() { return true; }
192 R_SExpr reduceUndefined(Undefined &Orig) { return true; }
193 R_SExpr reduceWildcard(Wildcard &Orig) { return true; }
194
195 R_SExpr reduceLiteral(Literal &Orig) { return true; }
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(unsigned i, unsigned 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(unsigned i, unsigned 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 out additional information.
402 bool Verbose;
403
404 // Omit redundant decls.
405 bool Cleanup;
406
407 // Print exprs in C-like syntax.
408 bool CStyle;
409
410public:
411 PrettyPrinter(bool V = false, bool C = true, bool CS = true)
412 : Verbose(V), Cleanup(C), CStyle(CS) {}
413
414 static void print(const SExpr *E, StreamType &SS) {
415 Self printer;
416 printer.printSExpr(E, SS, Prec_MAX);
417 }
418
419protected:
420 Self *self() { return reinterpret_cast<Self *>(this); }
421
422 void newline(StreamType &SS) {
423 SS << "\n";
424 }
425
426 // TODO: further distinguish between binary operations.
427 static const unsigned Prec_Atom = 0;
428 static const unsigned Prec_Postfix = 1;
429 static const unsigned Prec_Unary = 2;
430 static const unsigned Prec_Binary = 3;
431 static const unsigned Prec_Other = 4;
432 static const unsigned Prec_Decl = 5;
433 static const unsigned Prec_MAX = 6;
434
435 // Return the precedence of a given node, for use in pretty printing.
436 unsigned precedence(const SExpr *E) {
437 switch (E->opcode()) {
438 case COP_Future: return Prec_Atom;
439 case COP_Undefined: return Prec_Atom;
440 case COP_Wildcard: return Prec_Atom;
441
442 case COP_Literal: return Prec_Atom;
443 case COP_LiteralPtr: return Prec_Atom;
444 case COP_Variable: return Prec_Atom;
445 case COP_Function: return Prec_Decl;
446 case COP_SFunction: return Prec_Decl;
447 case COP_Code: return Prec_Decl;
448 case COP_Field: return Prec_Decl;
449
450 case COP_Apply: return Prec_Postfix;
451 case COP_SApply: return Prec_Postfix;
452 case COP_Project: return Prec_Postfix;
453
454 case COP_Call: return Prec_Postfix;
455 case COP_Alloc: return Prec_Other;
456 case COP_Load: return Prec_Postfix;
457 case COP_Store: return Prec_Other;
458 case COP_ArrayIndex: return Prec_Postfix;
459 case COP_ArrayAdd: return Prec_Postfix;
460
461 case COP_UnaryOp: return Prec_Unary;
462 case COP_BinaryOp: return Prec_Binary;
463 case COP_Cast: return Prec_Atom;
464
465 case COP_SCFG: return Prec_Decl;
466 case COP_BasicBlock: return Prec_MAX;
467 case COP_Phi: return Prec_Atom;
468 case COP_Goto: return Prec_Atom;
469 case COP_Branch: return Prec_Atom;
470 case COP_Return: return Prec_Other;
471
472 case COP_Identifier: return Prec_Atom;
473 case COP_IfThenElse: return Prec_Other;
474 case COP_Let: return Prec_Decl;
475 }
476 return Prec_MAX;
477 }
478
479 void printBlockLabel(StreamType & SS, const BasicBlock *BB, int index) {
480 if (!BB) {
481 SS << "BB_null";
482 return;
483 }
484 SS << "BB_";
485 SS << BB->blockID();
486 if (index >= 0) {
487 SS << ":";
488 SS << index;
489 }
490 }
491
492 void printSExpr(const SExpr *E, StreamType &SS, unsigned P, bool Sub=true) {
493 if (!E) {
494 self()->printNull(SS);
495 return;
496 }
497 if (Sub && E->block() && E->opcode() != COP_Variable) {
498 SS << "_x" << E->id();
499 return;
500 }
501 if (self()->precedence(E) > P) {
502 // Wrap expr in () if necessary.
503 SS << "(";
504 self()->printSExpr(E, SS, Prec_MAX);
505 SS << ")";
506 return;
507 }
508
509 switch (E->opcode()) {
510#define TIL_OPCODE_DEF(X) \
511 case COP_##X: \
512 self()->print##X(cast<X>(E), SS); \
513 return;
514#include "ThreadSafetyOps.def"
515#undef TIL_OPCODE_DEF
516 }
517 }
518
519 void printNull(StreamType &SS) {
520 SS << "#null";
521 }
522
523 void printFuture(const Future *E, StreamType &SS) {
524 self()->printSExpr(E->maybeGetResult(), SS, Prec_Atom);
525 }
526
527 void printUndefined(const Undefined *E, StreamType &SS) {
528 SS << "#undefined";
529 }
530
531 void printWildcard(const Wildcard *E, StreamType &SS) {
532 SS << "*";
533 }
534
535 template<class T>
536 void printLiteralT(const LiteralT<T> *E, StreamType &SS) {
537 SS << E->value();
538 }
539
540 void printLiteralT(const LiteralT<uint8_t> *E, StreamType &SS) {
541 SS << "'" << E->value() << "'";
542 }
543
544 void printLiteral(const Literal *E, StreamType &SS) {
545 if (E->clangExpr()) {
546 SS << getSourceLiteralString(E->clangExpr());
547 return;
548 }
549 else {
550 ValueType VT = E->valueType();
551 switch (VT.Base) {
553 SS << "void";
554 return;
556 if (E->as<bool>().value())
557 SS << "true";
558 else
559 SS << "false";
560 return;
562 switch (VT.Size) {
563 case ValueType::ST_8:
564 if (VT.Signed)
565 printLiteralT(&E->as<int8_t>(), SS);
566 else
567 printLiteralT(&E->as<uint8_t>(), SS);
568 return;
569 case ValueType::ST_16:
570 if (VT.Signed)
571 printLiteralT(&E->as<int16_t>(), SS);
572 else
573 printLiteralT(&E->as<uint16_t>(), SS);
574 return;
575 case ValueType::ST_32:
576 if (VT.Signed)
577 printLiteralT(&E->as<int32_t>(), SS);
578 else
579 printLiteralT(&E->as<uint32_t>(), SS);
580 return;
581 case ValueType::ST_64:
582 if (VT.Signed)
583 printLiteralT(&E->as<int64_t>(), SS);
584 else
585 printLiteralT(&E->as<uint64_t>(), SS);
586 return;
587 default:
588 break;
589 }
590 break;
592 switch (VT.Size) {
593 case ValueType::ST_32:
594 printLiteralT(&E->as<float>(), SS);
595 return;
596 case ValueType::ST_64:
597 printLiteralT(&E->as<double>(), SS);
598 return;
599 default:
600 break;
601 }
602 break;
604 SS << "\"";
605 printLiteralT(&E->as<StringRef>(), SS);
606 SS << "\"";
607 return;
609 SS << "#ptr";
610 return;
612 SS << "#vref";
613 return;
614 }
615 }
616 SS << "#lit";
617 }
618
619 void printLiteralPtr(const LiteralPtr *E, StreamType &SS) {
620 if (const NamedDecl *D = E->clangDecl())
621 SS << D->getNameAsString();
622 else
623 SS << "<temporary>";
624 }
625
626 void printVariable(const Variable *V, StreamType &SS, bool IsVarDecl=false) {
627 if (CStyle && V->kind() == Variable::VK_SFun)
628 SS << "this";
629 else
630 SS << V->name() << V->id();
631 }
632
633 void printFunction(const Function *E, StreamType &SS, unsigned sugared = 0) {
634 switch (sugared) {
635 default:
636 SS << "\\("; // Lambda
637 break;
638 case 1:
639 SS << "("; // Slot declarations
640 break;
641 case 2:
642 SS << ", "; // Curried functions
643 break;
644 }
645 self()->printVariable(E->variableDecl(), SS, true);
646 SS << ": ";
647 self()->printSExpr(E->variableDecl()->definition(), SS, Prec_MAX);
648
649 const SExpr *B = E->body();
650 if (B && B->opcode() == COP_Function)
651 self()->printFunction(cast<Function>(B), SS, 2);
652 else {
653 SS << ")";
654 self()->printSExpr(B, SS, Prec_Decl);
655 }
656 }
657
658 void printSFunction(const SFunction *E, StreamType &SS) {
659 SS << "@";
660 self()->printVariable(E->variableDecl(), SS, true);
661 SS << " ";
662 self()->printSExpr(E->body(), SS, Prec_Decl);
663 }
664
665 void printCode(const Code *E, StreamType &SS) {
666 SS << ": ";
667 self()->printSExpr(E->returnType(), SS, Prec_Decl-1);
668 SS << " -> ";
669 self()->printSExpr(E->body(), SS, Prec_Decl);
670 }
671
672 void printField(const Field *E, StreamType &SS) {
673 SS << ": ";
674 self()->printSExpr(E->range(), SS, Prec_Decl-1);
675 SS << " = ";
676 self()->printSExpr(E->body(), SS, Prec_Decl);
677 }
678
679 void printApply(const Apply *E, StreamType &SS, bool sugared = false) {
680 const SExpr *F = E->fun();
681 if (F->opcode() == COP_Apply) {
682 printApply(cast<Apply>(F), SS, true);
683 SS << ", ";
684 } else {
685 self()->printSExpr(F, SS, Prec_Postfix);
686 SS << "(";
687 }
688 self()->printSExpr(E->arg(), SS, Prec_MAX);
689 if (!sugared)
690 SS << ")$";
691 }
692
693 void printSApply(const SApply *E, StreamType &SS) {
694 self()->printSExpr(E->sfun(), SS, Prec_Postfix);
695 if (E->isDelegation()) {
696 SS << "@(";
697 self()->printSExpr(E->arg(), SS, Prec_MAX);
698 SS << ")";
699 }
700 }
701
702 void printProject(const Project *E, StreamType &SS) {
703 if (CStyle) {
704 // Omit the this->
705 if (const auto *SAP = dyn_cast<SApply>(E->record())) {
706 if (const auto *V = dyn_cast<Variable>(SAP->sfun())) {
707 if (!SAP->isDelegation() && V->kind() == Variable::VK_SFun) {
708 SS << E->slotName();
709 return;
710 }
711 }
712 }
713 if (isa<Wildcard>(E->record())) {
714 // handle existentials
715 SS << "&";
716 SS << E->clangDecl()->getQualifiedNameAsString();
717 return;
718 }
719 }
720 self()->printSExpr(E->record(), SS, Prec_Postfix);
721 if (CStyle && E->isArrow())
722 SS << "->";
723 else
724 SS << ".";
725 SS << E->slotName();
726 }
727
728 void printCall(const Call *E, StreamType &SS) {
729 const SExpr *T = E->target();
730 if (T->opcode() == COP_Apply) {
731 self()->printApply(cast<Apply>(T), SS, true);
732 SS << ")";
733 }
734 else {
735 self()->printSExpr(T, SS, Prec_Postfix);
736 SS << "()";
737 }
738 }
739
740 void printAlloc(const Alloc *E, StreamType &SS) {
741 SS << "new ";
742 self()->printSExpr(E->dataType(), SS, Prec_Other-1);
743 }
744
745 void printLoad(const Load *E, StreamType &SS) {
746 self()->printSExpr(E->pointer(), SS, Prec_Postfix);
747 if (!CStyle)
748 SS << "^";
749 }
750
751 void printStore(const Store *E, StreamType &SS) {
752 self()->printSExpr(E->destination(), SS, Prec_Other-1);
753 SS << " := ";
754 self()->printSExpr(E->source(), SS, Prec_Other-1);
755 }
756
757 void printArrayIndex(const ArrayIndex *E, StreamType &SS) {
758 self()->printSExpr(E->array(), SS, Prec_Postfix);
759 SS << "[";
760 self()->printSExpr(E->index(), SS, Prec_MAX);
761 SS << "]";
762 }
763
764 void printArrayAdd(const ArrayAdd *E, StreamType &SS) {
765 self()->printSExpr(E->array(), SS, Prec_Postfix);
766 SS << " + ";
767 self()->printSExpr(E->index(), SS, Prec_Atom);
768 }
769
770 void printUnaryOp(const UnaryOp *E, StreamType &SS) {
771 SS << getUnaryOpcodeString(E->unaryOpcode());
772 self()->printSExpr(E->expr(), SS, Prec_Unary);
773 }
774
775 void printBinaryOp(const BinaryOp *E, StreamType &SS) {
776 self()->printSExpr(E->expr0(), SS, Prec_Binary-1);
777 SS << " " << getBinaryOpcodeString(E->binaryOpcode()) << " ";
778 self()->printSExpr(E->expr1(), SS, Prec_Binary-1);
779 }
780
781 void printCast(const Cast *E, StreamType &SS) {
782 if (!CStyle) {
783 SS << "cast[";
784 switch (E->castOpcode()) {
785 case CAST_none:
786 SS << "none";
787 break;
788 case CAST_extendNum:
789 SS << "extendNum";
790 break;
791 case CAST_truncNum:
792 SS << "truncNum";
793 break;
794 case CAST_toFloat:
795 SS << "toFloat";
796 break;
797 case CAST_toInt:
798 SS << "toInt";
799 break;
800 case CAST_objToPtr:
801 SS << "objToPtr";
802 break;
803 }
804 SS << "](";
805 self()->printSExpr(E->expr(), SS, Prec_Unary);
806 SS << ")";
807 return;
808 }
809 self()->printSExpr(E->expr(), SS, Prec_Unary);
810 }
811
812 void printSCFG(const SCFG *E, StreamType &SS) {
813 SS << "CFG {\n";
814 for (const auto *BBI : *E)
815 printBasicBlock(BBI, SS);
816 SS << "}";
817 newline(SS);
818 }
819
820 void printBBInstr(const SExpr *E, StreamType &SS) {
821 bool Sub = false;
822 if (E->opcode() == COP_Variable) {
823 const auto *V = cast<Variable>(E);
824 SS << "let " << V->name() << V->id() << " = ";
825 E = V->definition();
826 Sub = true;
827 }
828 else if (E->opcode() != COP_Store) {
829 SS << "let _x" << E->id() << " = ";
830 }
831 self()->printSExpr(E, SS, Prec_MAX, Sub);
832 SS << ";";
833 newline(SS);
834 }
835
836 void printBasicBlock(const BasicBlock *E, StreamType &SS) {
837 SS << "BB_" << E->blockID() << ":";
838 if (E->parent())
839 SS << " BB_" << E->parent()->blockID();
840 newline(SS);
841
842 for (const auto *A : E->arguments())
843 printBBInstr(A, SS);
844
845 for (const auto *I : E->instructions())
846 printBBInstr(I, SS);
847
848 const SExpr *T = E->terminator();
849 if (T) {
850 self()->printSExpr(T, SS, Prec_MAX, false);
851 SS << ";";
852 newline(SS);
853 }
854 newline(SS);
855 }
856
857 void printPhi(const Phi *E, StreamType &SS) {
858 SS << "phi(";
859 if (E->status() == Phi::PH_SingleVal)
860 self()->printSExpr(E->values()[0], SS, Prec_MAX);
861 else {
862 unsigned i = 0;
863 for (const auto *V : E->values()) {
864 if (i++ > 0)
865 SS << ", ";
866 self()->printSExpr(V, SS, Prec_MAX);
867 }
868 }
869 SS << ")";
870 }
871
872 void printGoto(const Goto *E, StreamType &SS) {
873 SS << "goto ";
874 printBlockLabel(SS, E->targetBlock(), E->index());
875 }
876
877 void printBranch(const Branch *E, StreamType &SS) {
878 SS << "branch (";
879 self()->printSExpr(E->condition(), SS, Prec_MAX);
880 SS << ") ";
881 printBlockLabel(SS, E->thenBlock(), -1);
882 SS << " ";
883 printBlockLabel(SS, E->elseBlock(), -1);
884 }
885
886 void printReturn(const Return *E, StreamType &SS) {
887 SS << "return ";
888 self()->printSExpr(E->returnValue(), SS, Prec_Other);
889 }
890
891 void printIdentifier(const Identifier *E, StreamType &SS) {
892 SS << E->name();
893 }
894
895 void printIfThenElse(const IfThenElse *E, StreamType &SS) {
896 if (CStyle) {
897 printSExpr(E->condition(), SS, Prec_Unary);
898 SS << " ? ";
899 printSExpr(E->thenExpr(), SS, Prec_Unary);
900 SS << " : ";
901 printSExpr(E->elseExpr(), SS, Prec_Unary);
902 return;
903 }
904 SS << "if (";
905 printSExpr(E->condition(), SS, Prec_MAX);
906 SS << ") then ";
907 printSExpr(E->thenExpr(), SS, Prec_Other);
908 SS << " else ";
909 printSExpr(E->elseExpr(), SS, Prec_Other);
910 }
911
912 void printLet(const Let *E, StreamType &SS) {
913 SS << "let ";
914 printVariable(E->variableDecl(), SS, true);
915 SS << " = ";
916 printSExpr(E->variableDecl()->definition(), SS, Prec_Decl-1);
917 SS << "; ";
918 printSExpr(E->body(), SS, Prec_Decl-1);
919 }
920};
921
922class StdPrinter : public PrettyPrinter<StdPrinter, std::ostream> {};
923
924} // namespace til
925} // namespace threadSafety
926} // namespace clang
927
928#endif // LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYTRAVERSE_H
#define V(N, I)
Definition: ASTContext.h:3597
StringRef P
const Decl * D
Expr * E
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
__device__ __2f16 float __ockl_bool s
This represents a decl that may have a name.
Definition: Decl.h:273
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.
Simple arithmetic binary operations, e.g.
A conditional branch to two other blocks.
Call a function (after all arguments have been applied).
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.
An if-then-else expression.
A let-expression, e.g.
A Literal pointer to an object allocated in memory.
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 compareIntegers(unsigned i, unsigned j)
bool compareStrings(StringRef s, StringRef r)
void enterScope(const Variable *V1, const Variable *V2)
Phi Node, for code in SSA form.
void printFuture(const Future *E, StreamType &SS)
void printCall(const Call *E, StreamType &SS)
void printLiteralT(const LiteralT< uint8_t > *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)
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)
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 printLiteralT(const LiteralT< T > *E, StreamType &SS)
PrettyPrinter(bool V=false, bool C=true, bool CS=true)
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.
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.
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.
Placeholder for expressions that cannot be represented in the TIL.
@ 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.
#define bool
Definition: gpuintrin.h:32
StringRef getBinaryOpcodeString(TIL_BinaryOpcode Op)
Return the name of a binary opcode.
StringRef getUnaryOpcodeString(TIL_UnaryOpcode Op)
Return the name of a unary opcode.
std::string getSourceLiteralString(const Expr *CE)
The JSON file list parser is used to communicate input to InstallAPI.
@ Self
'self' clause, allowed on Compute and Combined Constructs, plus 'update'.
const FunctionProtoType * T
ValueTypes are data types that can actually be held in registers.