llvm-project

Форк
0
/
StmtPrinter.cpp 
2830 строк · 78.6 Кб
1
//===- StmtPrinter.cpp - Printing implementation for Stmt ASTs ------------===//
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 implements the Stmt::dumpPretty/Stmt::printPretty methods, which
10
// pretty print the AST back out to C code.
11
//
12
//===----------------------------------------------------------------------===//
13

14
#include "clang/AST/ASTContext.h"
15
#include "clang/AST/Attr.h"
16
#include "clang/AST/Decl.h"
17
#include "clang/AST/DeclBase.h"
18
#include "clang/AST/DeclCXX.h"
19
#include "clang/AST/DeclObjC.h"
20
#include "clang/AST/DeclOpenMP.h"
21
#include "clang/AST/DeclTemplate.h"
22
#include "clang/AST/Expr.h"
23
#include "clang/AST/ExprCXX.h"
24
#include "clang/AST/ExprObjC.h"
25
#include "clang/AST/ExprOpenMP.h"
26
#include "clang/AST/NestedNameSpecifier.h"
27
#include "clang/AST/OpenMPClause.h"
28
#include "clang/AST/PrettyPrinter.h"
29
#include "clang/AST/Stmt.h"
30
#include "clang/AST/StmtCXX.h"
31
#include "clang/AST/StmtObjC.h"
32
#include "clang/AST/StmtOpenMP.h"
33
#include "clang/AST/StmtVisitor.h"
34
#include "clang/AST/TemplateBase.h"
35
#include "clang/AST/Type.h"
36
#include "clang/Basic/CharInfo.h"
37
#include "clang/Basic/ExpressionTraits.h"
38
#include "clang/Basic/IdentifierTable.h"
39
#include "clang/Basic/JsonSupport.h"
40
#include "clang/Basic/LLVM.h"
41
#include "clang/Basic/Lambda.h"
42
#include "clang/Basic/OpenMPKinds.h"
43
#include "clang/Basic/OperatorKinds.h"
44
#include "clang/Basic/SourceLocation.h"
45
#include "clang/Basic/TypeTraits.h"
46
#include "clang/Lex/Lexer.h"
47
#include "llvm/ADT/ArrayRef.h"
48
#include "llvm/ADT/SmallString.h"
49
#include "llvm/ADT/SmallVector.h"
50
#include "llvm/ADT/StringExtras.h"
51
#include "llvm/ADT/StringRef.h"
52
#include "llvm/Support/Casting.h"
53
#include "llvm/Support/Compiler.h"
54
#include "llvm/Support/ErrorHandling.h"
55
#include "llvm/Support/raw_ostream.h"
56
#include <cassert>
57
#include <optional>
58
#include <string>
59

60
using namespace clang;
61

62
//===----------------------------------------------------------------------===//
63
// StmtPrinter Visitor
64
//===----------------------------------------------------------------------===//
65

66
namespace {
67

68
  class StmtPrinter : public StmtVisitor<StmtPrinter> {
69
    raw_ostream &OS;
70
    unsigned IndentLevel;
71
    PrinterHelper* Helper;
72
    PrintingPolicy Policy;
73
    std::string NL;
74
    const ASTContext *Context;
75

76
  public:
77
    StmtPrinter(raw_ostream &os, PrinterHelper *helper,
78
                const PrintingPolicy &Policy, unsigned Indentation = 0,
79
                StringRef NL = "\n", const ASTContext *Context = nullptr)
80
        : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy),
81
          NL(NL), Context(Context) {}
82

83
    void PrintStmt(Stmt *S) { PrintStmt(S, Policy.Indentation); }
84

85
    void PrintStmt(Stmt *S, int SubIndent) {
86
      IndentLevel += SubIndent;
87
      if (isa_and_nonnull<Expr>(S)) {
88
        // If this is an expr used in a stmt context, indent and newline it.
89
        Indent();
90
        Visit(S);
91
        OS << ";" << NL;
92
      } else if (S) {
93
        Visit(S);
94
      } else {
95
        Indent() << "<<<NULL STATEMENT>>>" << NL;
96
      }
97
      IndentLevel -= SubIndent;
98
    }
99

100
    void PrintInitStmt(Stmt *S, unsigned PrefixWidth) {
101
      // FIXME: Cope better with odd prefix widths.
102
      IndentLevel += (PrefixWidth + 1) / 2;
103
      if (auto *DS = dyn_cast<DeclStmt>(S))
104
        PrintRawDeclStmt(DS);
105
      else
106
        PrintExpr(cast<Expr>(S));
107
      OS << "; ";
108
      IndentLevel -= (PrefixWidth + 1) / 2;
109
    }
110

111
    void PrintControlledStmt(Stmt *S) {
112
      if (auto *CS = dyn_cast<CompoundStmt>(S)) {
113
        OS << " ";
114
        PrintRawCompoundStmt(CS);
115
        OS << NL;
116
      } else {
117
        OS << NL;
118
        PrintStmt(S);
119
      }
120
    }
121

122
    void PrintRawCompoundStmt(CompoundStmt *S);
123
    void PrintRawDecl(Decl *D);
124
    void PrintRawDeclStmt(const DeclStmt *S);
125
    void PrintRawIfStmt(IfStmt *If);
126
    void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
127
    void PrintCallArgs(CallExpr *E);
128
    void PrintRawSEHExceptHandler(SEHExceptStmt *S);
129
    void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
130
    void PrintOMPExecutableDirective(OMPExecutableDirective *S,
131
                                     bool ForceNoStmt = false);
132
    void PrintFPPragmas(CompoundStmt *S);
133

134
    void PrintExpr(Expr *E) {
135
      if (E)
136
        Visit(E);
137
      else
138
        OS << "<null expr>";
139
    }
140

141
    raw_ostream &Indent(int Delta = 0) {
142
      for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
143
        OS << "  ";
144
      return OS;
145
    }
146

147
    void Visit(Stmt* S) {
148
      if (Helper && Helper->handledStmt(S,OS))
149
          return;
150
      else StmtVisitor<StmtPrinter>::Visit(S);
151
    }
152

153
    void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
154
      Indent() << "<<unknown stmt type>>" << NL;
155
    }
156

157
    void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
158
      OS << "<<unknown expr type>>";
159
    }
160

161
    void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
162

163
#define ABSTRACT_STMT(CLASS)
164
#define STMT(CLASS, PARENT) \
165
    void Visit##CLASS(CLASS *Node);
166
#include "clang/AST/StmtNodes.inc"
167
  };
168

169
} // namespace
170

171
//===----------------------------------------------------------------------===//
172
//  Stmt printing methods.
173
//===----------------------------------------------------------------------===//
174

175
/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
176
/// with no newline after the }.
177
void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
178
  assert(Node && "Compound statement cannot be null");
179
  OS << "{" << NL;
180
  PrintFPPragmas(Node);
181
  for (auto *I : Node->body())
182
    PrintStmt(I);
183

184
  Indent() << "}";
185
}
186

187
void StmtPrinter::PrintFPPragmas(CompoundStmt *S) {
188
  if (!S->hasStoredFPFeatures())
189
    return;
190
  FPOptionsOverride FPO = S->getStoredFPFeatures();
191
  bool FEnvAccess = false;
192
  if (FPO.hasAllowFEnvAccessOverride()) {
193
    FEnvAccess = FPO.getAllowFEnvAccessOverride();
194
    Indent() << "#pragma STDC FENV_ACCESS " << (FEnvAccess ? "ON" : "OFF")
195
             << NL;
196
  }
197
  if (FPO.hasSpecifiedExceptionModeOverride()) {
198
    LangOptions::FPExceptionModeKind EM =
199
        FPO.getSpecifiedExceptionModeOverride();
200
    if (!FEnvAccess || EM != LangOptions::FPE_Strict) {
201
      Indent() << "#pragma clang fp exceptions(";
202
      switch (FPO.getSpecifiedExceptionModeOverride()) {
203
      default:
204
        break;
205
      case LangOptions::FPE_Ignore:
206
        OS << "ignore";
207
        break;
208
      case LangOptions::FPE_MayTrap:
209
        OS << "maytrap";
210
        break;
211
      case LangOptions::FPE_Strict:
212
        OS << "strict";
213
        break;
214
      }
215
      OS << ")\n";
216
    }
217
  }
218
  if (FPO.hasConstRoundingModeOverride()) {
219
    LangOptions::RoundingMode RM = FPO.getConstRoundingModeOverride();
220
    Indent() << "#pragma STDC FENV_ROUND ";
221
    switch (RM) {
222
    case llvm::RoundingMode::TowardZero:
223
      OS << "FE_TOWARDZERO";
224
      break;
225
    case llvm::RoundingMode::NearestTiesToEven:
226
      OS << "FE_TONEAREST";
227
      break;
228
    case llvm::RoundingMode::TowardPositive:
229
      OS << "FE_UPWARD";
230
      break;
231
    case llvm::RoundingMode::TowardNegative:
232
      OS << "FE_DOWNWARD";
233
      break;
234
    case llvm::RoundingMode::NearestTiesToAway:
235
      OS << "FE_TONEARESTFROMZERO";
236
      break;
237
    case llvm::RoundingMode::Dynamic:
238
      OS << "FE_DYNAMIC";
239
      break;
240
    default:
241
      llvm_unreachable("Invalid rounding mode");
242
    }
243
    OS << NL;
244
  }
245
}
246

247
void StmtPrinter::PrintRawDecl(Decl *D) {
248
  D->print(OS, Policy, IndentLevel);
249
}
250

251
void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
252
  SmallVector<Decl *, 2> Decls(S->decls());
253
  Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
254
}
255

256
void StmtPrinter::VisitNullStmt(NullStmt *Node) {
257
  Indent() << ";" << NL;
258
}
259

260
void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
261
  Indent();
262
  PrintRawDeclStmt(Node);
263
  OS << ";" << NL;
264
}
265

266
void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
267
  Indent();
268
  PrintRawCompoundStmt(Node);
269
  OS << "" << NL;
270
}
271

272
void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
273
  Indent(-1) << "case ";
274
  PrintExpr(Node->getLHS());
275
  if (Node->getRHS()) {
276
    OS << " ... ";
277
    PrintExpr(Node->getRHS());
278
  }
279
  OS << ":" << NL;
280

281
  PrintStmt(Node->getSubStmt(), 0);
282
}
283

284
void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
285
  Indent(-1) << "default:" << NL;
286
  PrintStmt(Node->getSubStmt(), 0);
287
}
288

289
void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
290
  Indent(-1) << Node->getName() << ":" << NL;
291
  PrintStmt(Node->getSubStmt(), 0);
292
}
293

294
void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
295
  llvm::ArrayRef<const Attr *> Attrs = Node->getAttrs();
296
  for (const auto *Attr : Attrs) {
297
    Attr->printPretty(OS, Policy);
298
    if (Attr != Attrs.back())
299
      OS << ' ';
300
  }
301

302
  PrintStmt(Node->getSubStmt(), 0);
303
}
304

305
void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
306
  if (If->isConsteval()) {
307
    OS << "if ";
308
    if (If->isNegatedConsteval())
309
      OS << "!";
310
    OS << "consteval";
311
    OS << NL;
312
    PrintStmt(If->getThen());
313
    if (Stmt *Else = If->getElse()) {
314
      Indent();
315
      OS << "else";
316
      PrintStmt(Else);
317
      OS << NL;
318
    }
319
    return;
320
  }
321

322
  OS << "if (";
323
  if (If->getInit())
324
    PrintInitStmt(If->getInit(), 4);
325
  if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
326
    PrintRawDeclStmt(DS);
327
  else
328
    PrintExpr(If->getCond());
329
  OS << ')';
330

331
  if (auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
332
    OS << ' ';
333
    PrintRawCompoundStmt(CS);
334
    OS << (If->getElse() ? " " : NL);
335
  } else {
336
    OS << NL;
337
    PrintStmt(If->getThen());
338
    if (If->getElse()) Indent();
339
  }
340

341
  if (Stmt *Else = If->getElse()) {
342
    OS << "else";
343

344
    if (auto *CS = dyn_cast<CompoundStmt>(Else)) {
345
      OS << ' ';
346
      PrintRawCompoundStmt(CS);
347
      OS << NL;
348
    } else if (auto *ElseIf = dyn_cast<IfStmt>(Else)) {
349
      OS << ' ';
350
      PrintRawIfStmt(ElseIf);
351
    } else {
352
      OS << NL;
353
      PrintStmt(If->getElse());
354
    }
355
  }
356
}
357

358
void StmtPrinter::VisitIfStmt(IfStmt *If) {
359
  Indent();
360
  PrintRawIfStmt(If);
361
}
362

363
void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
364
  Indent() << "switch (";
365
  if (Node->getInit())
366
    PrintInitStmt(Node->getInit(), 8);
367
  if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
368
    PrintRawDeclStmt(DS);
369
  else
370
    PrintExpr(Node->getCond());
371
  OS << ")";
372
  PrintControlledStmt(Node->getBody());
373
}
374

375
void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
376
  Indent() << "while (";
377
  if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
378
    PrintRawDeclStmt(DS);
379
  else
380
    PrintExpr(Node->getCond());
381
  OS << ")" << NL;
382
  PrintStmt(Node->getBody());
383
}
384

385
void StmtPrinter::VisitDoStmt(DoStmt *Node) {
386
  Indent() << "do ";
387
  if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
388
    PrintRawCompoundStmt(CS);
389
    OS << " ";
390
  } else {
391
    OS << NL;
392
    PrintStmt(Node->getBody());
393
    Indent();
394
  }
395

396
  OS << "while (";
397
  PrintExpr(Node->getCond());
398
  OS << ");" << NL;
399
}
400

401
void StmtPrinter::VisitForStmt(ForStmt *Node) {
402
  Indent() << "for (";
403
  if (Node->getInit())
404
    PrintInitStmt(Node->getInit(), 5);
405
  else
406
    OS << (Node->getCond() ? "; " : ";");
407
  if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
408
    PrintRawDeclStmt(DS);
409
  else if (Node->getCond())
410
    PrintExpr(Node->getCond());
411
  OS << ";";
412
  if (Node->getInc()) {
413
    OS << " ";
414
    PrintExpr(Node->getInc());
415
  }
416
  OS << ")";
417
  PrintControlledStmt(Node->getBody());
418
}
419

420
void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
421
  Indent() << "for (";
422
  if (auto *DS = dyn_cast<DeclStmt>(Node->getElement()))
423
    PrintRawDeclStmt(DS);
424
  else
425
    PrintExpr(cast<Expr>(Node->getElement()));
426
  OS << " in ";
427
  PrintExpr(Node->getCollection());
428
  OS << ")";
429
  PrintControlledStmt(Node->getBody());
430
}
431

432
void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
433
  Indent() << "for (";
434
  if (Node->getInit())
435
    PrintInitStmt(Node->getInit(), 5);
436
  PrintingPolicy SubPolicy(Policy);
437
  SubPolicy.SuppressInitializers = true;
438
  Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
439
  OS << " : ";
440
  PrintExpr(Node->getRangeInit());
441
  OS << ")";
442
  PrintControlledStmt(Node->getBody());
443
}
444

445
void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
446
  Indent();
447
  if (Node->isIfExists())
448
    OS << "__if_exists (";
449
  else
450
    OS << "__if_not_exists (";
451

452
  if (NestedNameSpecifier *Qualifier
453
        = Node->getQualifierLoc().getNestedNameSpecifier())
454
    Qualifier->print(OS, Policy);
455

456
  OS << Node->getNameInfo() << ") ";
457

458
  PrintRawCompoundStmt(Node->getSubStmt());
459
}
460

461
void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
462
  Indent() << "goto " << Node->getLabel()->getName() << ";";
463
  if (Policy.IncludeNewlines) OS << NL;
464
}
465

466
void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
467
  Indent() << "goto *";
468
  PrintExpr(Node->getTarget());
469
  OS << ";";
470
  if (Policy.IncludeNewlines) OS << NL;
471
}
472

473
void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
474
  Indent() << "continue;";
475
  if (Policy.IncludeNewlines) OS << NL;
476
}
477

478
void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
479
  Indent() << "break;";
480
  if (Policy.IncludeNewlines) OS << NL;
481
}
482

483
void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
484
  Indent() << "return";
485
  if (Node->getRetValue()) {
486
    OS << " ";
487
    PrintExpr(Node->getRetValue());
488
  }
489
  OS << ";";
490
  if (Policy.IncludeNewlines) OS << NL;
491
}
492

493
void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
494
  Indent() << "asm ";
495

496
  if (Node->isVolatile())
497
    OS << "volatile ";
498

499
  if (Node->isAsmGoto())
500
    OS << "goto ";
501

502
  OS << "(";
503
  VisitStringLiteral(Node->getAsmString());
504

505
  // Outputs
506
  if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
507
      Node->getNumClobbers() != 0 || Node->getNumLabels() != 0)
508
    OS << " : ";
509

510
  for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
511
    if (i != 0)
512
      OS << ", ";
513

514
    if (!Node->getOutputName(i).empty()) {
515
      OS << '[';
516
      OS << Node->getOutputName(i);
517
      OS << "] ";
518
    }
519

520
    VisitStringLiteral(Node->getOutputConstraintLiteral(i));
521
    OS << " (";
522
    Visit(Node->getOutputExpr(i));
523
    OS << ")";
524
  }
525

526
  // Inputs
527
  if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0 ||
528
      Node->getNumLabels() != 0)
529
    OS << " : ";
530

531
  for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
532
    if (i != 0)
533
      OS << ", ";
534

535
    if (!Node->getInputName(i).empty()) {
536
      OS << '[';
537
      OS << Node->getInputName(i);
538
      OS << "] ";
539
    }
540

541
    VisitStringLiteral(Node->getInputConstraintLiteral(i));
542
    OS << " (";
543
    Visit(Node->getInputExpr(i));
544
    OS << ")";
545
  }
546

547
  // Clobbers
548
  if (Node->getNumClobbers() != 0 || Node->getNumLabels())
549
    OS << " : ";
550

551
  for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
552
    if (i != 0)
553
      OS << ", ";
554

555
    VisitStringLiteral(Node->getClobberStringLiteral(i));
556
  }
557

558
  // Labels
559
  if (Node->getNumLabels() != 0)
560
    OS << " : ";
561

562
  for (unsigned i = 0, e = Node->getNumLabels(); i != e; ++i) {
563
    if (i != 0)
564
      OS << ", ";
565
    OS << Node->getLabelName(i);
566
  }
567

568
  OS << ");";
569
  if (Policy.IncludeNewlines) OS << NL;
570
}
571

572
void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
573
  // FIXME: Implement MS style inline asm statement printer.
574
  Indent() << "__asm ";
575
  if (Node->hasBraces())
576
    OS << "{" << NL;
577
  OS << Node->getAsmString() << NL;
578
  if (Node->hasBraces())
579
    Indent() << "}" << NL;
580
}
581

582
void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
583
  PrintStmt(Node->getCapturedDecl()->getBody());
584
}
585

586
void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
587
  Indent() << "@try";
588
  if (auto *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
589
    PrintRawCompoundStmt(TS);
590
    OS << NL;
591
  }
592

593
  for (ObjCAtCatchStmt *catchStmt : Node->catch_stmts()) {
594
    Indent() << "@catch(";
595
    if (Decl *DS = catchStmt->getCatchParamDecl())
596
      PrintRawDecl(DS);
597
    OS << ")";
598
    if (auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
599
      PrintRawCompoundStmt(CS);
600
      OS << NL;
601
    }
602
  }
603

604
  if (auto *FS = static_cast<ObjCAtFinallyStmt *>(Node->getFinallyStmt())) {
605
    Indent() << "@finally";
606
    if (auto *CS = dyn_cast<CompoundStmt>(FS->getFinallyBody())) {
607
      PrintRawCompoundStmt(CS);
608
      OS << NL;
609
    }
610
  }
611
}
612

613
void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
614
}
615

616
void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
617
  Indent() << "@catch (...) { /* todo */ } " << NL;
618
}
619

620
void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
621
  Indent() << "@throw";
622
  if (Node->getThrowExpr()) {
623
    OS << " ";
624
    PrintExpr(Node->getThrowExpr());
625
  }
626
  OS << ";" << NL;
627
}
628

629
void StmtPrinter::VisitObjCAvailabilityCheckExpr(
630
    ObjCAvailabilityCheckExpr *Node) {
631
  OS << "@available(...)";
632
}
633

634
void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
635
  Indent() << "@synchronized (";
636
  PrintExpr(Node->getSynchExpr());
637
  OS << ")";
638
  PrintRawCompoundStmt(Node->getSynchBody());
639
  OS << NL;
640
}
641

642
void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
643
  Indent() << "@autoreleasepool";
644
  PrintRawCompoundStmt(cast<CompoundStmt>(Node->getSubStmt()));
645
  OS << NL;
646
}
647

648
void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
649
  OS << "catch (";
650
  if (Decl *ExDecl = Node->getExceptionDecl())
651
    PrintRawDecl(ExDecl);
652
  else
653
    OS << "...";
654
  OS << ") ";
655
  PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
656
}
657

658
void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
659
  Indent();
660
  PrintRawCXXCatchStmt(Node);
661
  OS << NL;
662
}
663

664
void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
665
  Indent() << "try ";
666
  PrintRawCompoundStmt(Node->getTryBlock());
667
  for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
668
    OS << " ";
669
    PrintRawCXXCatchStmt(Node->getHandler(i));
670
  }
671
  OS << NL;
672
}
673

674
void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
675
  Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
676
  PrintRawCompoundStmt(Node->getTryBlock());
677
  SEHExceptStmt *E = Node->getExceptHandler();
678
  SEHFinallyStmt *F = Node->getFinallyHandler();
679
  if(E)
680
    PrintRawSEHExceptHandler(E);
681
  else {
682
    assert(F && "Must have a finally block...");
683
    PrintRawSEHFinallyStmt(F);
684
  }
685
  OS << NL;
686
}
687

688
void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
689
  OS << "__finally ";
690
  PrintRawCompoundStmt(Node->getBlock());
691
  OS << NL;
692
}
693

694
void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
695
  OS << "__except (";
696
  VisitExpr(Node->getFilterExpr());
697
  OS << ")" << NL;
698
  PrintRawCompoundStmt(Node->getBlock());
699
  OS << NL;
700
}
701

702
void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
703
  Indent();
704
  PrintRawSEHExceptHandler(Node);
705
  OS << NL;
706
}
707

708
void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
709
  Indent();
710
  PrintRawSEHFinallyStmt(Node);
711
  OS << NL;
712
}
713

714
void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
715
  Indent() << "__leave;";
716
  if (Policy.IncludeNewlines) OS << NL;
717
}
718

719
//===----------------------------------------------------------------------===//
720
//  OpenMP directives printing methods
721
//===----------------------------------------------------------------------===//
722

723
void StmtPrinter::VisitOMPCanonicalLoop(OMPCanonicalLoop *Node) {
724
  PrintStmt(Node->getLoopStmt());
725
}
726

727
void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
728
                                              bool ForceNoStmt) {
729
  OMPClausePrinter Printer(OS, Policy);
730
  ArrayRef<OMPClause *> Clauses = S->clauses();
731
  for (auto *Clause : Clauses)
732
    if (Clause && !Clause->isImplicit()) {
733
      OS << ' ';
734
      Printer.Visit(Clause);
735
    }
736
  OS << NL;
737
  if (!ForceNoStmt && S->hasAssociatedStmt())
738
    PrintStmt(S->getRawStmt());
739
}
740

741
void StmtPrinter::VisitOMPMetaDirective(OMPMetaDirective *Node) {
742
  Indent() << "#pragma omp metadirective";
743
  PrintOMPExecutableDirective(Node);
744
}
745

746
void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
747
  Indent() << "#pragma omp parallel";
748
  PrintOMPExecutableDirective(Node);
749
}
750

751
void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
752
  Indent() << "#pragma omp simd";
753
  PrintOMPExecutableDirective(Node);
754
}
755

756
void StmtPrinter::VisitOMPTileDirective(OMPTileDirective *Node) {
757
  Indent() << "#pragma omp tile";
758
  PrintOMPExecutableDirective(Node);
759
}
760

761
void StmtPrinter::VisitOMPUnrollDirective(OMPUnrollDirective *Node) {
762
  Indent() << "#pragma omp unroll";
763
  PrintOMPExecutableDirective(Node);
764
}
765

766
void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
767
  Indent() << "#pragma omp for";
768
  PrintOMPExecutableDirective(Node);
769
}
770

771
void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
772
  Indent() << "#pragma omp for simd";
773
  PrintOMPExecutableDirective(Node);
774
}
775

776
void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
777
  Indent() << "#pragma omp sections";
778
  PrintOMPExecutableDirective(Node);
779
}
780

781
void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
782
  Indent() << "#pragma omp section";
783
  PrintOMPExecutableDirective(Node);
784
}
785

786
void StmtPrinter::VisitOMPScopeDirective(OMPScopeDirective *Node) {
787
  Indent() << "#pragma omp scope";
788
  PrintOMPExecutableDirective(Node);
789
}
790

791
void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
792
  Indent() << "#pragma omp single";
793
  PrintOMPExecutableDirective(Node);
794
}
795

796
void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
797
  Indent() << "#pragma omp master";
798
  PrintOMPExecutableDirective(Node);
799
}
800

801
void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
802
  Indent() << "#pragma omp critical";
803
  if (Node->getDirectiveName().getName()) {
804
    OS << " (";
805
    Node->getDirectiveName().printName(OS, Policy);
806
    OS << ")";
807
  }
808
  PrintOMPExecutableDirective(Node);
809
}
810

811
void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
812
  Indent() << "#pragma omp parallel for";
813
  PrintOMPExecutableDirective(Node);
814
}
815

816
void StmtPrinter::VisitOMPParallelForSimdDirective(
817
    OMPParallelForSimdDirective *Node) {
818
  Indent() << "#pragma omp parallel for simd";
819
  PrintOMPExecutableDirective(Node);
820
}
821

822
void StmtPrinter::VisitOMPParallelMasterDirective(
823
    OMPParallelMasterDirective *Node) {
824
  Indent() << "#pragma omp parallel master";
825
  PrintOMPExecutableDirective(Node);
826
}
827

828
void StmtPrinter::VisitOMPParallelMaskedDirective(
829
    OMPParallelMaskedDirective *Node) {
830
  Indent() << "#pragma omp parallel masked";
831
  PrintOMPExecutableDirective(Node);
832
}
833

834
void StmtPrinter::VisitOMPParallelSectionsDirective(
835
    OMPParallelSectionsDirective *Node) {
836
  Indent() << "#pragma omp parallel sections";
837
  PrintOMPExecutableDirective(Node);
838
}
839

840
void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
841
  Indent() << "#pragma omp task";
842
  PrintOMPExecutableDirective(Node);
843
}
844

845
void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
846
  Indent() << "#pragma omp taskyield";
847
  PrintOMPExecutableDirective(Node);
848
}
849

850
void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
851
  Indent() << "#pragma omp barrier";
852
  PrintOMPExecutableDirective(Node);
853
}
854

855
void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
856
  Indent() << "#pragma omp taskwait";
857
  PrintOMPExecutableDirective(Node);
858
}
859

860
void StmtPrinter::VisitOMPErrorDirective(OMPErrorDirective *Node) {
861
  Indent() << "#pragma omp error";
862
  PrintOMPExecutableDirective(Node);
863
}
864

865
void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
866
  Indent() << "#pragma omp taskgroup";
867
  PrintOMPExecutableDirective(Node);
868
}
869

870
void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
871
  Indent() << "#pragma omp flush";
872
  PrintOMPExecutableDirective(Node);
873
}
874

875
void StmtPrinter::VisitOMPDepobjDirective(OMPDepobjDirective *Node) {
876
  Indent() << "#pragma omp depobj";
877
  PrintOMPExecutableDirective(Node);
878
}
879

880
void StmtPrinter::VisitOMPScanDirective(OMPScanDirective *Node) {
881
  Indent() << "#pragma omp scan";
882
  PrintOMPExecutableDirective(Node);
883
}
884

885
void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
886
  Indent() << "#pragma omp ordered";
887
  PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>());
888
}
889

890
void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
891
  Indent() << "#pragma omp atomic";
892
  PrintOMPExecutableDirective(Node);
893
}
894

895
void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
896
  Indent() << "#pragma omp target";
897
  PrintOMPExecutableDirective(Node);
898
}
899

900
void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
901
  Indent() << "#pragma omp target data";
902
  PrintOMPExecutableDirective(Node);
903
}
904

905
void StmtPrinter::VisitOMPTargetEnterDataDirective(
906
    OMPTargetEnterDataDirective *Node) {
907
  Indent() << "#pragma omp target enter data";
908
  PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
909
}
910

911
void StmtPrinter::VisitOMPTargetExitDataDirective(
912
    OMPTargetExitDataDirective *Node) {
913
  Indent() << "#pragma omp target exit data";
914
  PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
915
}
916

917
void StmtPrinter::VisitOMPTargetParallelDirective(
918
    OMPTargetParallelDirective *Node) {
919
  Indent() << "#pragma omp target parallel";
920
  PrintOMPExecutableDirective(Node);
921
}
922

923
void StmtPrinter::VisitOMPTargetParallelForDirective(
924
    OMPTargetParallelForDirective *Node) {
925
  Indent() << "#pragma omp target parallel for";
926
  PrintOMPExecutableDirective(Node);
927
}
928

929
void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
930
  Indent() << "#pragma omp teams";
931
  PrintOMPExecutableDirective(Node);
932
}
933

934
void StmtPrinter::VisitOMPCancellationPointDirective(
935
    OMPCancellationPointDirective *Node) {
936
  Indent() << "#pragma omp cancellation point "
937
           << getOpenMPDirectiveName(Node->getCancelRegion());
938
  PrintOMPExecutableDirective(Node);
939
}
940

941
void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
942
  Indent() << "#pragma omp cancel "
943
           << getOpenMPDirectiveName(Node->getCancelRegion());
944
  PrintOMPExecutableDirective(Node);
945
}
946

947
void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
948
  Indent() << "#pragma omp taskloop";
949
  PrintOMPExecutableDirective(Node);
950
}
951

952
void StmtPrinter::VisitOMPTaskLoopSimdDirective(
953
    OMPTaskLoopSimdDirective *Node) {
954
  Indent() << "#pragma omp taskloop simd";
955
  PrintOMPExecutableDirective(Node);
956
}
957

958
void StmtPrinter::VisitOMPMasterTaskLoopDirective(
959
    OMPMasterTaskLoopDirective *Node) {
960
  Indent() << "#pragma omp master taskloop";
961
  PrintOMPExecutableDirective(Node);
962
}
963

964
void StmtPrinter::VisitOMPMaskedTaskLoopDirective(
965
    OMPMaskedTaskLoopDirective *Node) {
966
  Indent() << "#pragma omp masked taskloop";
967
  PrintOMPExecutableDirective(Node);
968
}
969

970
void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective(
971
    OMPMasterTaskLoopSimdDirective *Node) {
972
  Indent() << "#pragma omp master taskloop simd";
973
  PrintOMPExecutableDirective(Node);
974
}
975

976
void StmtPrinter::VisitOMPMaskedTaskLoopSimdDirective(
977
    OMPMaskedTaskLoopSimdDirective *Node) {
978
  Indent() << "#pragma omp masked taskloop simd";
979
  PrintOMPExecutableDirective(Node);
980
}
981

982
void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective(
983
    OMPParallelMasterTaskLoopDirective *Node) {
984
  Indent() << "#pragma omp parallel master taskloop";
985
  PrintOMPExecutableDirective(Node);
986
}
987

988
void StmtPrinter::VisitOMPParallelMaskedTaskLoopDirective(
989
    OMPParallelMaskedTaskLoopDirective *Node) {
990
  Indent() << "#pragma omp parallel masked taskloop";
991
  PrintOMPExecutableDirective(Node);
992
}
993

994
void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective(
995
    OMPParallelMasterTaskLoopSimdDirective *Node) {
996
  Indent() << "#pragma omp parallel master taskloop simd";
997
  PrintOMPExecutableDirective(Node);
998
}
999

1000
void StmtPrinter::VisitOMPParallelMaskedTaskLoopSimdDirective(
1001
    OMPParallelMaskedTaskLoopSimdDirective *Node) {
1002
  Indent() << "#pragma omp parallel masked taskloop simd";
1003
  PrintOMPExecutableDirective(Node);
1004
}
1005

1006
void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
1007
  Indent() << "#pragma omp distribute";
1008
  PrintOMPExecutableDirective(Node);
1009
}
1010

1011
void StmtPrinter::VisitOMPTargetUpdateDirective(
1012
    OMPTargetUpdateDirective *Node) {
1013
  Indent() << "#pragma omp target update";
1014
  PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
1015
}
1016

1017
void StmtPrinter::VisitOMPDistributeParallelForDirective(
1018
    OMPDistributeParallelForDirective *Node) {
1019
  Indent() << "#pragma omp distribute parallel for";
1020
  PrintOMPExecutableDirective(Node);
1021
}
1022

1023
void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
1024
    OMPDistributeParallelForSimdDirective *Node) {
1025
  Indent() << "#pragma omp distribute parallel for simd";
1026
  PrintOMPExecutableDirective(Node);
1027
}
1028

1029
void StmtPrinter::VisitOMPDistributeSimdDirective(
1030
    OMPDistributeSimdDirective *Node) {
1031
  Indent() << "#pragma omp distribute simd";
1032
  PrintOMPExecutableDirective(Node);
1033
}
1034

1035
void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
1036
    OMPTargetParallelForSimdDirective *Node) {
1037
  Indent() << "#pragma omp target parallel for simd";
1038
  PrintOMPExecutableDirective(Node);
1039
}
1040

1041
void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
1042
  Indent() << "#pragma omp target simd";
1043
  PrintOMPExecutableDirective(Node);
1044
}
1045

1046
void StmtPrinter::VisitOMPTeamsDistributeDirective(
1047
    OMPTeamsDistributeDirective *Node) {
1048
  Indent() << "#pragma omp teams distribute";
1049
  PrintOMPExecutableDirective(Node);
1050
}
1051

1052
void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
1053
    OMPTeamsDistributeSimdDirective *Node) {
1054
  Indent() << "#pragma omp teams distribute simd";
1055
  PrintOMPExecutableDirective(Node);
1056
}
1057

1058
void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
1059
    OMPTeamsDistributeParallelForSimdDirective *Node) {
1060
  Indent() << "#pragma omp teams distribute parallel for simd";
1061
  PrintOMPExecutableDirective(Node);
1062
}
1063

1064
void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
1065
    OMPTeamsDistributeParallelForDirective *Node) {
1066
  Indent() << "#pragma omp teams distribute parallel for";
1067
  PrintOMPExecutableDirective(Node);
1068
}
1069

1070
void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
1071
  Indent() << "#pragma omp target teams";
1072
  PrintOMPExecutableDirective(Node);
1073
}
1074

1075
void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
1076
    OMPTargetTeamsDistributeDirective *Node) {
1077
  Indent() << "#pragma omp target teams distribute";
1078
  PrintOMPExecutableDirective(Node);
1079
}
1080

1081
void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
1082
    OMPTargetTeamsDistributeParallelForDirective *Node) {
1083
  Indent() << "#pragma omp target teams distribute parallel for";
1084
  PrintOMPExecutableDirective(Node);
1085
}
1086

1087
void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
1088
    OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
1089
  Indent() << "#pragma omp target teams distribute parallel for simd";
1090
  PrintOMPExecutableDirective(Node);
1091
}
1092

1093
void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
1094
    OMPTargetTeamsDistributeSimdDirective *Node) {
1095
  Indent() << "#pragma omp target teams distribute simd";
1096
  PrintOMPExecutableDirective(Node);
1097
}
1098

1099
void StmtPrinter::VisitOMPInteropDirective(OMPInteropDirective *Node) {
1100
  Indent() << "#pragma omp interop";
1101
  PrintOMPExecutableDirective(Node);
1102
}
1103

1104
void StmtPrinter::VisitOMPDispatchDirective(OMPDispatchDirective *Node) {
1105
  Indent() << "#pragma omp dispatch";
1106
  PrintOMPExecutableDirective(Node);
1107
}
1108

1109
void StmtPrinter::VisitOMPMaskedDirective(OMPMaskedDirective *Node) {
1110
  Indent() << "#pragma omp masked";
1111
  PrintOMPExecutableDirective(Node);
1112
}
1113

1114
void StmtPrinter::VisitOMPGenericLoopDirective(OMPGenericLoopDirective *Node) {
1115
  Indent() << "#pragma omp loop";
1116
  PrintOMPExecutableDirective(Node);
1117
}
1118

1119
void StmtPrinter::VisitOMPTeamsGenericLoopDirective(
1120
    OMPTeamsGenericLoopDirective *Node) {
1121
  Indent() << "#pragma omp teams loop";
1122
  PrintOMPExecutableDirective(Node);
1123
}
1124

1125
void StmtPrinter::VisitOMPTargetTeamsGenericLoopDirective(
1126
    OMPTargetTeamsGenericLoopDirective *Node) {
1127
  Indent() << "#pragma omp target teams loop";
1128
  PrintOMPExecutableDirective(Node);
1129
}
1130

1131
void StmtPrinter::VisitOMPParallelGenericLoopDirective(
1132
    OMPParallelGenericLoopDirective *Node) {
1133
  Indent() << "#pragma omp parallel loop";
1134
  PrintOMPExecutableDirective(Node);
1135
}
1136

1137
void StmtPrinter::VisitOMPTargetParallelGenericLoopDirective(
1138
    OMPTargetParallelGenericLoopDirective *Node) {
1139
  Indent() << "#pragma omp target parallel loop";
1140
  PrintOMPExecutableDirective(Node);
1141
}
1142

1143
//===----------------------------------------------------------------------===//
1144
//  OpenACC construct printing methods
1145
//===----------------------------------------------------------------------===//
1146
void StmtPrinter::VisitOpenACCComputeConstruct(OpenACCComputeConstruct *S) {
1147
  Indent() << "#pragma acc " << S->getDirectiveKind();
1148

1149
  if (!S->clauses().empty()) {
1150
    OS << ' ';
1151
    OpenACCClausePrinter Printer(OS, Policy);
1152
    Printer.VisitClauseList(S->clauses());
1153
  }
1154
  OS << '\n';
1155

1156
  PrintStmt(S->getStructuredBlock());
1157
}
1158

1159
void StmtPrinter::VisitOpenACCLoopConstruct(OpenACCLoopConstruct *S) {
1160
  Indent() << "#pragma acc loop";
1161

1162
  if (!S->clauses().empty()) {
1163
    OS << ' ';
1164
    OpenACCClausePrinter Printer(OS, Policy);
1165
    Printer.VisitClauseList(S->clauses());
1166
  }
1167
  OS << '\n';
1168

1169
  PrintStmt(S->getLoop());
1170
}
1171

1172
//===----------------------------------------------------------------------===//
1173
//  Expr printing methods.
1174
//===----------------------------------------------------------------------===//
1175

1176
void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
1177
  OS << Node->getBuiltinStr() << "()";
1178
}
1179

1180
void StmtPrinter::VisitEmbedExpr(EmbedExpr *Node) {
1181
  llvm::report_fatal_error("Not implemented");
1182
}
1183

1184
void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
1185
  PrintExpr(Node->getSubExpr());
1186
}
1187

1188
void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
1189
  if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1190
    OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1191
    return;
1192
  }
1193
  if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(Node->getDecl())) {
1194
    TPOD->printAsExpr(OS, Policy);
1195
    return;
1196
  }
1197
  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1198
    Qualifier->print(OS, Policy);
1199
  if (Node->hasTemplateKeyword())
1200
    OS << "template ";
1201
  if (Policy.CleanUglifiedParameters &&
1202
      isa<ParmVarDecl, NonTypeTemplateParmDecl>(Node->getDecl()) &&
1203
      Node->getDecl()->getIdentifier())
1204
    OS << Node->getDecl()->getIdentifier()->deuglifiedName();
1205
  else
1206
    Node->getNameInfo().printName(OS, Policy);
1207
  if (Node->hasExplicitTemplateArgs()) {
1208
    const TemplateParameterList *TPL = nullptr;
1209
    if (!Node->hadMultipleCandidates())
1210
      if (auto *TD = dyn_cast<TemplateDecl>(Node->getDecl()))
1211
        TPL = TD->getTemplateParameters();
1212
    printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL);
1213
  }
1214
}
1215

1216
void StmtPrinter::VisitDependentScopeDeclRefExpr(
1217
                                           DependentScopeDeclRefExpr *Node) {
1218
  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1219
    Qualifier->print(OS, Policy);
1220
  if (Node->hasTemplateKeyword())
1221
    OS << "template ";
1222
  OS << Node->getNameInfo();
1223
  if (Node->hasExplicitTemplateArgs())
1224
    printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1225
}
1226

1227
void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
1228
  if (Node->getQualifier())
1229
    Node->getQualifier()->print(OS, Policy);
1230
  if (Node->hasTemplateKeyword())
1231
    OS << "template ";
1232
  OS << Node->getNameInfo();
1233
  if (Node->hasExplicitTemplateArgs())
1234
    printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1235
}
1236

1237
static bool isImplicitSelf(const Expr *E) {
1238
  if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
1239
    if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
1240
      if (PD->getParameterKind() == ImplicitParamKind::ObjCSelf &&
1241
          DRE->getBeginLoc().isInvalid())
1242
        return true;
1243
    }
1244
  }
1245
  return false;
1246
}
1247

1248
void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1249
  if (Node->getBase()) {
1250
    if (!Policy.SuppressImplicitBase ||
1251
        !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
1252
      PrintExpr(Node->getBase());
1253
      OS << (Node->isArrow() ? "->" : ".");
1254
    }
1255
  }
1256
  OS << *Node->getDecl();
1257
}
1258

1259
void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1260
  if (Node->isSuperReceiver())
1261
    OS << "super.";
1262
  else if (Node->isObjectReceiver() && Node->getBase()) {
1263
    PrintExpr(Node->getBase());
1264
    OS << ".";
1265
  } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1266
    OS << Node->getClassReceiver()->getName() << ".";
1267
  }
1268

1269
  if (Node->isImplicitProperty()) {
1270
    if (const auto *Getter = Node->getImplicitPropertyGetter())
1271
      Getter->getSelector().print(OS);
1272
    else
1273
      OS << SelectorTable::getPropertyNameFromSetterSelector(
1274
          Node->getImplicitPropertySetter()->getSelector());
1275
  } else
1276
    OS << Node->getExplicitProperty()->getName();
1277
}
1278

1279
void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1280
  PrintExpr(Node->getBaseExpr());
1281
  OS << "[";
1282
  PrintExpr(Node->getKeyExpr());
1283
  OS << "]";
1284
}
1285

1286
void StmtPrinter::VisitSYCLUniqueStableNameExpr(
1287
    SYCLUniqueStableNameExpr *Node) {
1288
  OS << "__builtin_sycl_unique_stable_name(";
1289
  Node->getTypeSourceInfo()->getType().print(OS, Policy);
1290
  OS << ")";
1291
}
1292

1293
void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1294
  OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
1295
}
1296

1297
void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1298
  CharacterLiteral::print(Node->getValue(), Node->getKind(), OS);
1299
}
1300

1301
/// Prints the given expression using the original source text. Returns true on
1302
/// success, false otherwise.
1303
static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1304
                               const ASTContext *Context) {
1305
  if (!Context)
1306
    return false;
1307
  bool Invalid = false;
1308
  StringRef Source = Lexer::getSourceText(
1309
      CharSourceRange::getTokenRange(E->getSourceRange()),
1310
      Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1311
  if (!Invalid) {
1312
    OS << Source;
1313
    return true;
1314
  }
1315
  return false;
1316
}
1317

1318
void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1319
  if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1320
    return;
1321
  bool isSigned = Node->getType()->isSignedIntegerType();
1322
  OS << toString(Node->getValue(), 10, isSigned);
1323

1324
  if (isa<BitIntType>(Node->getType())) {
1325
    OS << (isSigned ? "wb" : "uwb");
1326
    return;
1327
  }
1328

1329
  // Emit suffixes.  Integer literals are always a builtin integer type.
1330
  switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1331
  default: llvm_unreachable("Unexpected type for integer literal!");
1332
  case BuiltinType::Char_S:
1333
  case BuiltinType::Char_U:    OS << "i8"; break;
1334
  case BuiltinType::UChar:     OS << "Ui8"; break;
1335
  case BuiltinType::SChar:     OS << "i8"; break;
1336
  case BuiltinType::Short:     OS << "i16"; break;
1337
  case BuiltinType::UShort:    OS << "Ui16"; break;
1338
  case BuiltinType::Int:       break; // no suffix.
1339
  case BuiltinType::UInt:      OS << 'U'; break;
1340
  case BuiltinType::Long:      OS << 'L'; break;
1341
  case BuiltinType::ULong:     OS << "UL"; break;
1342
  case BuiltinType::LongLong:  OS << "LL"; break;
1343
  case BuiltinType::ULongLong: OS << "ULL"; break;
1344
  case BuiltinType::Int128:
1345
    break; // no suffix.
1346
  case BuiltinType::UInt128:
1347
    break; // no suffix.
1348
  case BuiltinType::WChar_S:
1349
  case BuiltinType::WChar_U:
1350
    break; // no suffix
1351
  }
1352
}
1353

1354
void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1355
  if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1356
    return;
1357
  OS << Node->getValueAsString(/*Radix=*/10);
1358

1359
  switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1360
    default: llvm_unreachable("Unexpected type for fixed point literal!");
1361
    case BuiltinType::ShortFract:   OS << "hr"; break;
1362
    case BuiltinType::ShortAccum:   OS << "hk"; break;
1363
    case BuiltinType::UShortFract:  OS << "uhr"; break;
1364
    case BuiltinType::UShortAccum:  OS << "uhk"; break;
1365
    case BuiltinType::Fract:        OS << "r"; break;
1366
    case BuiltinType::Accum:        OS << "k"; break;
1367
    case BuiltinType::UFract:       OS << "ur"; break;
1368
    case BuiltinType::UAccum:       OS << "uk"; break;
1369
    case BuiltinType::LongFract:    OS << "lr"; break;
1370
    case BuiltinType::LongAccum:    OS << "lk"; break;
1371
    case BuiltinType::ULongFract:   OS << "ulr"; break;
1372
    case BuiltinType::ULongAccum:   OS << "ulk"; break;
1373
  }
1374
}
1375

1376
static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1377
                                 bool PrintSuffix) {
1378
  SmallString<16> Str;
1379
  Node->getValue().toString(Str);
1380
  OS << Str;
1381
  if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1382
    OS << '.'; // Trailing dot in order to separate from ints.
1383

1384
  if (!PrintSuffix)
1385
    return;
1386

1387
  // Emit suffixes.  Float literals are always a builtin float type.
1388
  switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1389
  default: llvm_unreachable("Unexpected type for float literal!");
1390
  case BuiltinType::Half:       break; // FIXME: suffix?
1391
  case BuiltinType::Ibm128:     break; // FIXME: No suffix for ibm128 literal
1392
  case BuiltinType::Double:     break; // no suffix.
1393
  case BuiltinType::Float16:    OS << "F16"; break;
1394
  case BuiltinType::Float:      OS << 'F'; break;
1395
  case BuiltinType::LongDouble: OS << 'L'; break;
1396
  case BuiltinType::Float128:   OS << 'Q'; break;
1397
  }
1398
}
1399

1400
void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1401
  if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1402
    return;
1403
  PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1404
}
1405

1406
void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1407
  PrintExpr(Node->getSubExpr());
1408
  OS << "i";
1409
}
1410

1411
void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1412
  Str->outputString(OS);
1413
}
1414

1415
void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1416
  OS << "(";
1417
  PrintExpr(Node->getSubExpr());
1418
  OS << ")";
1419
}
1420

1421
void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1422
  if (!Node->isPostfix()) {
1423
    OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1424

1425
    // Print a space if this is an "identifier operator" like __real, or if
1426
    // it might be concatenated incorrectly like '+'.
1427
    switch (Node->getOpcode()) {
1428
    default: break;
1429
    case UO_Real:
1430
    case UO_Imag:
1431
    case UO_Extension:
1432
      OS << ' ';
1433
      break;
1434
    case UO_Plus:
1435
    case UO_Minus:
1436
      if (isa<UnaryOperator>(Node->getSubExpr()))
1437
        OS << ' ';
1438
      break;
1439
    }
1440
  }
1441
  PrintExpr(Node->getSubExpr());
1442

1443
  if (Node->isPostfix())
1444
    OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1445
}
1446

1447
void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1448
  OS << "__builtin_offsetof(";
1449
  Node->getTypeSourceInfo()->getType().print(OS, Policy);
1450
  OS << ", ";
1451
  bool PrintedSomething = false;
1452
  for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1453
    OffsetOfNode ON = Node->getComponent(i);
1454
    if (ON.getKind() == OffsetOfNode::Array) {
1455
      // Array node
1456
      OS << "[";
1457
      PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1458
      OS << "]";
1459
      PrintedSomething = true;
1460
      continue;
1461
    }
1462

1463
    // Skip implicit base indirections.
1464
    if (ON.getKind() == OffsetOfNode::Base)
1465
      continue;
1466

1467
    // Field or identifier node.
1468
    const IdentifierInfo *Id = ON.getFieldName();
1469
    if (!Id)
1470
      continue;
1471

1472
    if (PrintedSomething)
1473
      OS << ".";
1474
    else
1475
      PrintedSomething = true;
1476
    OS << Id->getName();
1477
  }
1478
  OS << ")";
1479
}
1480

1481
void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(
1482
    UnaryExprOrTypeTraitExpr *Node) {
1483
  const char *Spelling = getTraitSpelling(Node->getKind());
1484
  if (Node->getKind() == UETT_AlignOf) {
1485
    if (Policy.Alignof)
1486
      Spelling = "alignof";
1487
    else if (Policy.UnderscoreAlignof)
1488
      Spelling = "_Alignof";
1489
    else
1490
      Spelling = "__alignof";
1491
  }
1492

1493
  OS << Spelling;
1494

1495
  if (Node->isArgumentType()) {
1496
    OS << '(';
1497
    Node->getArgumentType().print(OS, Policy);
1498
    OS << ')';
1499
  } else {
1500
    OS << " ";
1501
    PrintExpr(Node->getArgumentExpr());
1502
  }
1503
}
1504

1505
void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1506
  OS << "_Generic(";
1507
  if (Node->isExprPredicate())
1508
    PrintExpr(Node->getControllingExpr());
1509
  else
1510
    Node->getControllingType()->getType().print(OS, Policy);
1511

1512
  for (const GenericSelectionExpr::Association &Assoc : Node->associations()) {
1513
    OS << ", ";
1514
    QualType T = Assoc.getType();
1515
    if (T.isNull())
1516
      OS << "default";
1517
    else
1518
      T.print(OS, Policy);
1519
    OS << ": ";
1520
    PrintExpr(Assoc.getAssociationExpr());
1521
  }
1522
  OS << ")";
1523
}
1524

1525
void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1526
  PrintExpr(Node->getLHS());
1527
  OS << "[";
1528
  PrintExpr(Node->getRHS());
1529
  OS << "]";
1530
}
1531

1532
void StmtPrinter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *Node) {
1533
  PrintExpr(Node->getBase());
1534
  OS << "[";
1535
  PrintExpr(Node->getRowIdx());
1536
  OS << "]";
1537
  OS << "[";
1538
  PrintExpr(Node->getColumnIdx());
1539
  OS << "]";
1540
}
1541

1542
void StmtPrinter::VisitArraySectionExpr(ArraySectionExpr *Node) {
1543
  PrintExpr(Node->getBase());
1544
  OS << "[";
1545
  if (Node->getLowerBound())
1546
    PrintExpr(Node->getLowerBound());
1547
  if (Node->getColonLocFirst().isValid()) {
1548
    OS << ":";
1549
    if (Node->getLength())
1550
      PrintExpr(Node->getLength());
1551
  }
1552
  if (Node->isOMPArraySection() && Node->getColonLocSecond().isValid()) {
1553
    OS << ":";
1554
    if (Node->getStride())
1555
      PrintExpr(Node->getStride());
1556
  }
1557
  OS << "]";
1558
}
1559

1560
void StmtPrinter::VisitOMPArrayShapingExpr(OMPArrayShapingExpr *Node) {
1561
  OS << "(";
1562
  for (Expr *E : Node->getDimensions()) {
1563
    OS << "[";
1564
    PrintExpr(E);
1565
    OS << "]";
1566
  }
1567
  OS << ")";
1568
  PrintExpr(Node->getBase());
1569
}
1570

1571
void StmtPrinter::VisitOMPIteratorExpr(OMPIteratorExpr *Node) {
1572
  OS << "iterator(";
1573
  for (unsigned I = 0, E = Node->numOfIterators(); I < E; ++I) {
1574
    auto *VD = cast<ValueDecl>(Node->getIteratorDecl(I));
1575
    VD->getType().print(OS, Policy);
1576
    const OMPIteratorExpr::IteratorRange Range = Node->getIteratorRange(I);
1577
    OS << " " << VD->getName() << " = ";
1578
    PrintExpr(Range.Begin);
1579
    OS << ":";
1580
    PrintExpr(Range.End);
1581
    if (Range.Step) {
1582
      OS << ":";
1583
      PrintExpr(Range.Step);
1584
    }
1585
    if (I < E - 1)
1586
      OS << ", ";
1587
  }
1588
  OS << ")";
1589
}
1590

1591
void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1592
  for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1593
    if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1594
      // Don't print any defaulted arguments
1595
      break;
1596
    }
1597

1598
    if (i) OS << ", ";
1599
    PrintExpr(Call->getArg(i));
1600
  }
1601
}
1602

1603
void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1604
  PrintExpr(Call->getCallee());
1605
  OS << "(";
1606
  PrintCallArgs(Call);
1607
  OS << ")";
1608
}
1609

1610
static bool isImplicitThis(const Expr *E) {
1611
  if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1612
    return TE->isImplicit();
1613
  return false;
1614
}
1615

1616
void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1617
  if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
1618
    PrintExpr(Node->getBase());
1619

1620
    auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1621
    FieldDecl *ParentDecl =
1622
        ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1623
                     : nullptr;
1624

1625
    if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1626
      OS << (Node->isArrow() ? "->" : ".");
1627
  }
1628

1629
  if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1630
    if (FD->isAnonymousStructOrUnion())
1631
      return;
1632

1633
  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1634
    Qualifier->print(OS, Policy);
1635
  if (Node->hasTemplateKeyword())
1636
    OS << "template ";
1637
  OS << Node->getMemberNameInfo();
1638
  const TemplateParameterList *TPL = nullptr;
1639
  if (auto *FD = dyn_cast<FunctionDecl>(Node->getMemberDecl())) {
1640
    if (!Node->hadMultipleCandidates())
1641
      if (auto *FTD = FD->getPrimaryTemplate())
1642
        TPL = FTD->getTemplateParameters();
1643
  } else if (auto *VTSD =
1644
                 dyn_cast<VarTemplateSpecializationDecl>(Node->getMemberDecl()))
1645
    TPL = VTSD->getSpecializedTemplate()->getTemplateParameters();
1646
  if (Node->hasExplicitTemplateArgs())
1647
    printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL);
1648
}
1649

1650
void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1651
  PrintExpr(Node->getBase());
1652
  OS << (Node->isArrow() ? "->isa" : ".isa");
1653
}
1654

1655
void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1656
  PrintExpr(Node->getBase());
1657
  OS << ".";
1658
  OS << Node->getAccessor().getName();
1659
}
1660

1661
void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1662
  OS << '(';
1663
  Node->getTypeAsWritten().print(OS, Policy);
1664
  OS << ')';
1665
  PrintExpr(Node->getSubExpr());
1666
}
1667

1668
void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1669
  OS << '(';
1670
  Node->getType().print(OS, Policy);
1671
  OS << ')';
1672
  PrintExpr(Node->getInitializer());
1673
}
1674

1675
void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1676
  // No need to print anything, simply forward to the subexpression.
1677
  PrintExpr(Node->getSubExpr());
1678
}
1679

1680
void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1681
  PrintExpr(Node->getLHS());
1682
  OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1683
  PrintExpr(Node->getRHS());
1684
}
1685

1686
void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1687
  PrintExpr(Node->getLHS());
1688
  OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1689
  PrintExpr(Node->getRHS());
1690
}
1691

1692
void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1693
  PrintExpr(Node->getCond());
1694
  OS << " ? ";
1695
  PrintExpr(Node->getLHS());
1696
  OS << " : ";
1697
  PrintExpr(Node->getRHS());
1698
}
1699

1700
// GNU extensions.
1701

1702
void
1703
StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1704
  PrintExpr(Node->getCommon());
1705
  OS << " ?: ";
1706
  PrintExpr(Node->getFalseExpr());
1707
}
1708

1709
void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1710
  OS << "&&" << Node->getLabel()->getName();
1711
}
1712

1713
void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1714
  OS << "(";
1715
  PrintRawCompoundStmt(E->getSubStmt());
1716
  OS << ")";
1717
}
1718

1719
void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1720
  OS << "__builtin_choose_expr(";
1721
  PrintExpr(Node->getCond());
1722
  OS << ", ";
1723
  PrintExpr(Node->getLHS());
1724
  OS << ", ";
1725
  PrintExpr(Node->getRHS());
1726
  OS << ")";
1727
}
1728

1729
void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1730
  OS << "__null";
1731
}
1732

1733
void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1734
  OS << "__builtin_shufflevector(";
1735
  for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1736
    if (i) OS << ", ";
1737
    PrintExpr(Node->getExpr(i));
1738
  }
1739
  OS << ")";
1740
}
1741

1742
void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1743
  OS << "__builtin_convertvector(";
1744
  PrintExpr(Node->getSrcExpr());
1745
  OS << ", ";
1746
  Node->getType().print(OS, Policy);
1747
  OS << ")";
1748
}
1749

1750
void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1751
  if (Node->getSyntacticForm()) {
1752
    Visit(Node->getSyntacticForm());
1753
    return;
1754
  }
1755

1756
  OS << "{";
1757
  for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1758
    if (i) OS << ", ";
1759
    if (Node->getInit(i))
1760
      PrintExpr(Node->getInit(i));
1761
    else
1762
      OS << "{}";
1763
  }
1764
  OS << "}";
1765
}
1766

1767
void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1768
  // There's no way to express this expression in any of our supported
1769
  // languages, so just emit something terse and (hopefully) clear.
1770
  OS << "{";
1771
  PrintExpr(Node->getSubExpr());
1772
  OS << "}";
1773
}
1774

1775
void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1776
  OS << "*";
1777
}
1778

1779
void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1780
  OS << "(";
1781
  for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1782
    if (i) OS << ", ";
1783
    PrintExpr(Node->getExpr(i));
1784
  }
1785
  OS << ")";
1786
}
1787

1788
void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1789
  bool NeedsEquals = true;
1790
  for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1791
    if (D.isFieldDesignator()) {
1792
      if (D.getDotLoc().isInvalid()) {
1793
        if (const IdentifierInfo *II = D.getFieldName()) {
1794
          OS << II->getName() << ":";
1795
          NeedsEquals = false;
1796
        }
1797
      } else {
1798
        OS << "." << D.getFieldName()->getName();
1799
      }
1800
    } else {
1801
      OS << "[";
1802
      if (D.isArrayDesignator()) {
1803
        PrintExpr(Node->getArrayIndex(D));
1804
      } else {
1805
        PrintExpr(Node->getArrayRangeStart(D));
1806
        OS << " ... ";
1807
        PrintExpr(Node->getArrayRangeEnd(D));
1808
      }
1809
      OS << "]";
1810
    }
1811
  }
1812

1813
  if (NeedsEquals)
1814
    OS << " = ";
1815
  else
1816
    OS << " ";
1817
  PrintExpr(Node->getInit());
1818
}
1819

1820
void StmtPrinter::VisitDesignatedInitUpdateExpr(
1821
    DesignatedInitUpdateExpr *Node) {
1822
  OS << "{";
1823
  OS << "/*base*/";
1824
  PrintExpr(Node->getBase());
1825
  OS << ", ";
1826

1827
  OS << "/*updater*/";
1828
  PrintExpr(Node->getUpdater());
1829
  OS << "}";
1830
}
1831

1832
void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1833
  OS << "/*no init*/";
1834
}
1835

1836
void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1837
  if (Node->getType()->getAsCXXRecordDecl()) {
1838
    OS << "/*implicit*/";
1839
    Node->getType().print(OS, Policy);
1840
    OS << "()";
1841
  } else {
1842
    OS << "/*implicit*/(";
1843
    Node->getType().print(OS, Policy);
1844
    OS << ')';
1845
    if (Node->getType()->isRecordType())
1846
      OS << "{}";
1847
    else
1848
      OS << 0;
1849
  }
1850
}
1851

1852
void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1853
  OS << "__builtin_va_arg(";
1854
  PrintExpr(Node->getSubExpr());
1855
  OS << ", ";
1856
  Node->getType().print(OS, Policy);
1857
  OS << ")";
1858
}
1859

1860
void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1861
  PrintExpr(Node->getSyntacticForm());
1862
}
1863

1864
void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1865
  const char *Name = nullptr;
1866
  switch (Node->getOp()) {
1867
#define BUILTIN(ID, TYPE, ATTRS)
1868
#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1869
  case AtomicExpr::AO ## ID: \
1870
    Name = #ID "("; \
1871
    break;
1872
#include "clang/Basic/Builtins.inc"
1873
  }
1874
  OS << Name;
1875

1876
  // AtomicExpr stores its subexpressions in a permuted order.
1877
  PrintExpr(Node->getPtr());
1878
  if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1879
      Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1880
      Node->getOp() != AtomicExpr::AO__scoped_atomic_load_n &&
1881
      Node->getOp() != AtomicExpr::AO__opencl_atomic_load &&
1882
      Node->getOp() != AtomicExpr::AO__hip_atomic_load) {
1883
    OS << ", ";
1884
    PrintExpr(Node->getVal1());
1885
  }
1886
  if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1887
      Node->isCmpXChg()) {
1888
    OS << ", ";
1889
    PrintExpr(Node->getVal2());
1890
  }
1891
  if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1892
      Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1893
    OS << ", ";
1894
    PrintExpr(Node->getWeak());
1895
  }
1896
  if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1897
      Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
1898
    OS << ", ";
1899
    PrintExpr(Node->getOrder());
1900
  }
1901
  if (Node->isCmpXChg()) {
1902
    OS << ", ";
1903
    PrintExpr(Node->getOrderFail());
1904
  }
1905
  OS << ")";
1906
}
1907

1908
// C++
1909
void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1910
  OverloadedOperatorKind Kind = Node->getOperator();
1911
  if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1912
    if (Node->getNumArgs() == 1) {
1913
      OS << getOperatorSpelling(Kind) << ' ';
1914
      PrintExpr(Node->getArg(0));
1915
    } else {
1916
      PrintExpr(Node->getArg(0));
1917
      OS << ' ' << getOperatorSpelling(Kind);
1918
    }
1919
  } else if (Kind == OO_Arrow) {
1920
    PrintExpr(Node->getArg(0));
1921
  } else if (Kind == OO_Call || Kind == OO_Subscript) {
1922
    PrintExpr(Node->getArg(0));
1923
    OS << (Kind == OO_Call ? '(' : '[');
1924
    for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1925
      if (ArgIdx > 1)
1926
        OS << ", ";
1927
      if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1928
        PrintExpr(Node->getArg(ArgIdx));
1929
    }
1930
    OS << (Kind == OO_Call ? ')' : ']');
1931
  } else if (Node->getNumArgs() == 1) {
1932
    OS << getOperatorSpelling(Kind) << ' ';
1933
    PrintExpr(Node->getArg(0));
1934
  } else if (Node->getNumArgs() == 2) {
1935
    PrintExpr(Node->getArg(0));
1936
    OS << ' ' << getOperatorSpelling(Kind) << ' ';
1937
    PrintExpr(Node->getArg(1));
1938
  } else {
1939
    llvm_unreachable("unknown overloaded operator");
1940
  }
1941
}
1942

1943
void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1944
  // If we have a conversion operator call only print the argument.
1945
  CXXMethodDecl *MD = Node->getMethodDecl();
1946
  if (isa_and_nonnull<CXXConversionDecl>(MD)) {
1947
    PrintExpr(Node->getImplicitObjectArgument());
1948
    return;
1949
  }
1950
  VisitCallExpr(cast<CallExpr>(Node));
1951
}
1952

1953
void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1954
  PrintExpr(Node->getCallee());
1955
  OS << "<<<";
1956
  PrintCallArgs(Node->getConfig());
1957
  OS << ">>>(";
1958
  PrintCallArgs(Node);
1959
  OS << ")";
1960
}
1961

1962
void StmtPrinter::VisitCXXRewrittenBinaryOperator(
1963
    CXXRewrittenBinaryOperator *Node) {
1964
  CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
1965
      Node->getDecomposedForm();
1966
  PrintExpr(const_cast<Expr*>(Decomposed.LHS));
1967
  OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' ';
1968
  PrintExpr(const_cast<Expr*>(Decomposed.RHS));
1969
}
1970

1971
void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1972
  OS << Node->getCastName() << '<';
1973
  Node->getTypeAsWritten().print(OS, Policy);
1974
  OS << ">(";
1975
  PrintExpr(Node->getSubExpr());
1976
  OS << ")";
1977
}
1978

1979
void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1980
  VisitCXXNamedCastExpr(Node);
1981
}
1982

1983
void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1984
  VisitCXXNamedCastExpr(Node);
1985
}
1986

1987
void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1988
  VisitCXXNamedCastExpr(Node);
1989
}
1990

1991
void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1992
  VisitCXXNamedCastExpr(Node);
1993
}
1994

1995
void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
1996
  OS << "__builtin_bit_cast(";
1997
  Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
1998
  OS << ", ";
1999
  PrintExpr(Node->getSubExpr());
2000
  OS << ")";
2001
}
2002

2003
void StmtPrinter::VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr *Node) {
2004
  VisitCXXNamedCastExpr(Node);
2005
}
2006

2007
void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
2008
  OS << "typeid(";
2009
  if (Node->isTypeOperand()) {
2010
    Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
2011
  } else {
2012
    PrintExpr(Node->getExprOperand());
2013
  }
2014
  OS << ")";
2015
}
2016

2017
void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
2018
  OS << "__uuidof(";
2019
  if (Node->isTypeOperand()) {
2020
    Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
2021
  } else {
2022
    PrintExpr(Node->getExprOperand());
2023
  }
2024
  OS << ")";
2025
}
2026

2027
void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
2028
  PrintExpr(Node->getBaseExpr());
2029
  if (Node->isArrow())
2030
    OS << "->";
2031
  else
2032
    OS << ".";
2033
  if (NestedNameSpecifier *Qualifier =
2034
      Node->getQualifierLoc().getNestedNameSpecifier())
2035
    Qualifier->print(OS, Policy);
2036
  OS << Node->getPropertyDecl()->getDeclName();
2037
}
2038

2039
void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
2040
  PrintExpr(Node->getBase());
2041
  OS << "[";
2042
  PrintExpr(Node->getIdx());
2043
  OS << "]";
2044
}
2045

2046
void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
2047
  switch (Node->getLiteralOperatorKind()) {
2048
  case UserDefinedLiteral::LOK_Raw:
2049
    OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
2050
    break;
2051
  case UserDefinedLiteral::LOK_Template: {
2052
    const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
2053
    const TemplateArgumentList *Args =
2054
      cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
2055
    assert(Args);
2056

2057
    if (Args->size() != 1 || Args->get(0).getKind() != TemplateArgument::Pack) {
2058
      const TemplateParameterList *TPL = nullptr;
2059
      if (!DRE->hadMultipleCandidates())
2060
        if (const auto *TD = dyn_cast<TemplateDecl>(DRE->getDecl()))
2061
          TPL = TD->getTemplateParameters();
2062
      OS << "operator\"\"" << Node->getUDSuffix()->getName();
2063
      printTemplateArgumentList(OS, Args->asArray(), Policy, TPL);
2064
      OS << "()";
2065
      return;
2066
    }
2067

2068
    const TemplateArgument &Pack = Args->get(0);
2069
    for (const auto &P : Pack.pack_elements()) {
2070
      char C = (char)P.getAsIntegral().getZExtValue();
2071
      OS << C;
2072
    }
2073
    break;
2074
  }
2075
  case UserDefinedLiteral::LOK_Integer: {
2076
    // Print integer literal without suffix.
2077
    const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
2078
    OS << toString(Int->getValue(), 10, /*isSigned*/false);
2079
    break;
2080
  }
2081
  case UserDefinedLiteral::LOK_Floating: {
2082
    // Print floating literal without suffix.
2083
    auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
2084
    PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
2085
    break;
2086
  }
2087
  case UserDefinedLiteral::LOK_String:
2088
  case UserDefinedLiteral::LOK_Character:
2089
    PrintExpr(Node->getCookedLiteral());
2090
    break;
2091
  }
2092
  OS << Node->getUDSuffix()->getName();
2093
}
2094

2095
void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
2096
  OS << (Node->getValue() ? "true" : "false");
2097
}
2098

2099
void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
2100
  OS << "nullptr";
2101
}
2102

2103
void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
2104
  OS << "this";
2105
}
2106

2107
void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
2108
  if (!Node->getSubExpr())
2109
    OS << "throw";
2110
  else {
2111
    OS << "throw ";
2112
    PrintExpr(Node->getSubExpr());
2113
  }
2114
}
2115

2116
void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
2117
  // Nothing to print: we picked up the default argument.
2118
}
2119

2120
void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
2121
  // Nothing to print: we picked up the default initializer.
2122
}
2123

2124
void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
2125
  auto TargetType = Node->getType();
2126
  auto *Auto = TargetType->getContainedDeducedType();
2127
  bool Bare = Auto && Auto->isDeduced();
2128

2129
  // Parenthesize deduced casts.
2130
  if (Bare)
2131
    OS << '(';
2132
  TargetType.print(OS, Policy);
2133
  if (Bare)
2134
    OS << ')';
2135

2136
  // No extra braces surrounding the inner construct.
2137
  if (!Node->isListInitialization())
2138
    OS << '(';
2139
  PrintExpr(Node->getSubExpr());
2140
  if (!Node->isListInitialization())
2141
    OS << ')';
2142
}
2143

2144
void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
2145
  PrintExpr(Node->getSubExpr());
2146
}
2147

2148
void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
2149
  Node->getType().print(OS, Policy);
2150
  if (Node->isStdInitListInitialization())
2151
    /* Nothing to do; braces are part of creating the std::initializer_list. */;
2152
  else if (Node->isListInitialization())
2153
    OS << "{";
2154
  else
2155
    OS << "(";
2156
  for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
2157
                                         ArgEnd = Node->arg_end();
2158
       Arg != ArgEnd; ++Arg) {
2159
    if ((*Arg)->isDefaultArgument())
2160
      break;
2161
    if (Arg != Node->arg_begin())
2162
      OS << ", ";
2163
    PrintExpr(*Arg);
2164
  }
2165
  if (Node->isStdInitListInitialization())
2166
    /* See above. */;
2167
  else if (Node->isListInitialization())
2168
    OS << "}";
2169
  else
2170
    OS << ")";
2171
}
2172

2173
void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
2174
  OS << '[';
2175
  bool NeedComma = false;
2176
  switch (Node->getCaptureDefault()) {
2177
  case LCD_None:
2178
    break;
2179

2180
  case LCD_ByCopy:
2181
    OS << '=';
2182
    NeedComma = true;
2183
    break;
2184

2185
  case LCD_ByRef:
2186
    OS << '&';
2187
    NeedComma = true;
2188
    break;
2189
  }
2190
  for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2191
                                 CEnd = Node->explicit_capture_end();
2192
       C != CEnd;
2193
       ++C) {
2194
    if (C->capturesVLAType())
2195
      continue;
2196

2197
    if (NeedComma)
2198
      OS << ", ";
2199
    NeedComma = true;
2200

2201
    switch (C->getCaptureKind()) {
2202
    case LCK_This:
2203
      OS << "this";
2204
      break;
2205

2206
    case LCK_StarThis:
2207
      OS << "*this";
2208
      break;
2209

2210
    case LCK_ByRef:
2211
      if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
2212
        OS << '&';
2213
      OS << C->getCapturedVar()->getName();
2214
      break;
2215

2216
    case LCK_ByCopy:
2217
      OS << C->getCapturedVar()->getName();
2218
      break;
2219

2220
    case LCK_VLAType:
2221
      llvm_unreachable("VLA type in explicit captures.");
2222
    }
2223

2224
    if (C->isPackExpansion())
2225
      OS << "...";
2226

2227
    if (Node->isInitCapture(C)) {
2228
      // Init captures are always VarDecl.
2229
      auto *D = cast<VarDecl>(C->getCapturedVar());
2230

2231
      llvm::StringRef Pre;
2232
      llvm::StringRef Post;
2233
      if (D->getInitStyle() == VarDecl::CallInit &&
2234
          !isa<ParenListExpr>(D->getInit())) {
2235
        Pre = "(";
2236
        Post = ")";
2237
      } else if (D->getInitStyle() == VarDecl::CInit) {
2238
        Pre = " = ";
2239
      }
2240

2241
      OS << Pre;
2242
      PrintExpr(D->getInit());
2243
      OS << Post;
2244
    }
2245
  }
2246
  OS << ']';
2247

2248
  if (!Node->getExplicitTemplateParameters().empty()) {
2249
    Node->getTemplateParameterList()->print(
2250
        OS, Node->getLambdaClass()->getASTContext(),
2251
        /*OmitTemplateKW*/true);
2252
  }
2253

2254
  if (Node->hasExplicitParameters()) {
2255
    OS << '(';
2256
    CXXMethodDecl *Method = Node->getCallOperator();
2257
    NeedComma = false;
2258
    for (const auto *P : Method->parameters()) {
2259
      if (NeedComma) {
2260
        OS << ", ";
2261
      } else {
2262
        NeedComma = true;
2263
      }
2264
      std::string ParamStr =
2265
          (Policy.CleanUglifiedParameters && P->getIdentifier())
2266
              ? P->getIdentifier()->deuglifiedName().str()
2267
              : P->getNameAsString();
2268
      P->getOriginalType().print(OS, Policy, ParamStr);
2269
    }
2270
    if (Method->isVariadic()) {
2271
      if (NeedComma)
2272
        OS << ", ";
2273
      OS << "...";
2274
    }
2275
    OS << ')';
2276

2277
    if (Node->isMutable())
2278
      OS << " mutable";
2279

2280
    auto *Proto = Method->getType()->castAs<FunctionProtoType>();
2281
    Proto->printExceptionSpecification(OS, Policy);
2282

2283
    // FIXME: Attributes
2284

2285
    // Print the trailing return type if it was specified in the source.
2286
    if (Node->hasExplicitResultType()) {
2287
      OS << " -> ";
2288
      Proto->getReturnType().print(OS, Policy);
2289
    }
2290
  }
2291

2292
  // Print the body.
2293
  OS << ' ';
2294
  if (Policy.TerseOutput)
2295
    OS << "{}";
2296
  else
2297
    PrintRawCompoundStmt(Node->getCompoundStmtBody());
2298
}
2299

2300
void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2301
  if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2302
    TSInfo->getType().print(OS, Policy);
2303
  else
2304
    Node->getType().print(OS, Policy);
2305
  OS << "()";
2306
}
2307

2308
void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2309
  if (E->isGlobalNew())
2310
    OS << "::";
2311
  OS << "new ";
2312
  unsigned NumPlace = E->getNumPlacementArgs();
2313
  if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
2314
    OS << "(";
2315
    PrintExpr(E->getPlacementArg(0));
2316
    for (unsigned i = 1; i < NumPlace; ++i) {
2317
      if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2318
        break;
2319
      OS << ", ";
2320
      PrintExpr(E->getPlacementArg(i));
2321
    }
2322
    OS << ") ";
2323
  }
2324
  if (E->isParenTypeId())
2325
    OS << "(";
2326
  std::string TypeS;
2327
  if (E->isArray()) {
2328
    llvm::raw_string_ostream s(TypeS);
2329
    s << '[';
2330
    if (std::optional<Expr *> Size = E->getArraySize())
2331
      (*Size)->printPretty(s, Helper, Policy);
2332
    s << ']';
2333
  }
2334
  E->getAllocatedType().print(OS, Policy, TypeS);
2335
  if (E->isParenTypeId())
2336
    OS << ")";
2337

2338
  CXXNewInitializationStyle InitStyle = E->getInitializationStyle();
2339
  if (InitStyle != CXXNewInitializationStyle::None) {
2340
    bool Bare = InitStyle == CXXNewInitializationStyle::Parens &&
2341
                !isa<ParenListExpr>(E->getInitializer());
2342
    if (Bare)
2343
      OS << "(";
2344
    PrintExpr(E->getInitializer());
2345
    if (Bare)
2346
      OS << ")";
2347
  }
2348
}
2349

2350
void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2351
  if (E->isGlobalDelete())
2352
    OS << "::";
2353
  OS << "delete ";
2354
  if (E->isArrayForm())
2355
    OS << "[] ";
2356
  PrintExpr(E->getArgument());
2357
}
2358

2359
void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2360
  PrintExpr(E->getBase());
2361
  if (E->isArrow())
2362
    OS << "->";
2363
  else
2364
    OS << '.';
2365
  if (E->getQualifier())
2366
    E->getQualifier()->print(OS, Policy);
2367
  OS << "~";
2368

2369
  if (const IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2370
    OS << II->getName();
2371
  else
2372
    E->getDestroyedType().print(OS, Policy);
2373
}
2374

2375
void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2376
  if (E->isListInitialization() && !E->isStdInitListInitialization())
2377
    OS << "{";
2378

2379
  for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2380
    if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2381
      // Don't print any defaulted arguments
2382
      break;
2383
    }
2384

2385
    if (i) OS << ", ";
2386
    PrintExpr(E->getArg(i));
2387
  }
2388

2389
  if (E->isListInitialization() && !E->isStdInitListInitialization())
2390
    OS << "}";
2391
}
2392

2393
void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2394
  // Parens are printed by the surrounding context.
2395
  OS << "<forwarded>";
2396
}
2397

2398
void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2399
  PrintExpr(E->getSubExpr());
2400
}
2401

2402
void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2403
  // Just forward to the subexpression.
2404
  PrintExpr(E->getSubExpr());
2405
}
2406

2407
void StmtPrinter::VisitCXXUnresolvedConstructExpr(
2408
    CXXUnresolvedConstructExpr *Node) {
2409
  Node->getTypeAsWritten().print(OS, Policy);
2410
  if (!Node->isListInitialization())
2411
    OS << '(';
2412
  for (auto Arg = Node->arg_begin(), ArgEnd = Node->arg_end(); Arg != ArgEnd;
2413
       ++Arg) {
2414
    if (Arg != Node->arg_begin())
2415
      OS << ", ";
2416
    PrintExpr(*Arg);
2417
  }
2418
  if (!Node->isListInitialization())
2419
    OS << ')';
2420
}
2421

2422
void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2423
                                         CXXDependentScopeMemberExpr *Node) {
2424
  if (!Node->isImplicitAccess()) {
2425
    PrintExpr(Node->getBase());
2426
    OS << (Node->isArrow() ? "->" : ".");
2427
  }
2428
  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2429
    Qualifier->print(OS, Policy);
2430
  if (Node->hasTemplateKeyword())
2431
    OS << "template ";
2432
  OS << Node->getMemberNameInfo();
2433
  if (Node->hasExplicitTemplateArgs())
2434
    printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2435
}
2436

2437
void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2438
  if (!Node->isImplicitAccess()) {
2439
    PrintExpr(Node->getBase());
2440
    OS << (Node->isArrow() ? "->" : ".");
2441
  }
2442
  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2443
    Qualifier->print(OS, Policy);
2444
  if (Node->hasTemplateKeyword())
2445
    OS << "template ";
2446
  OS << Node->getMemberNameInfo();
2447
  if (Node->hasExplicitTemplateArgs())
2448
    printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2449
}
2450

2451
void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2452
  OS << getTraitSpelling(E->getTrait()) << "(";
2453
  for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2454
    if (I > 0)
2455
      OS << ", ";
2456
    E->getArg(I)->getType().print(OS, Policy);
2457
  }
2458
  OS << ")";
2459
}
2460

2461
void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2462
  OS << getTraitSpelling(E->getTrait()) << '(';
2463
  E->getQueriedType().print(OS, Policy);
2464
  OS << ')';
2465
}
2466

2467
void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2468
  OS << getTraitSpelling(E->getTrait()) << '(';
2469
  PrintExpr(E->getQueriedExpression());
2470
  OS << ')';
2471
}
2472

2473
void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2474
  OS << "noexcept(";
2475
  PrintExpr(E->getOperand());
2476
  OS << ")";
2477
}
2478

2479
void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2480
  PrintExpr(E->getPattern());
2481
  OS << "...";
2482
}
2483

2484
void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2485
  OS << "sizeof...(" << *E->getPack() << ")";
2486
}
2487

2488
void StmtPrinter::VisitPackIndexingExpr(PackIndexingExpr *E) {
2489
  OS << E->getPackIdExpression() << "...[" << E->getIndexExpr() << "]";
2490
}
2491

2492
void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2493
                                       SubstNonTypeTemplateParmPackExpr *Node) {
2494
  OS << *Node->getParameterPack();
2495
}
2496

2497
void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2498
                                       SubstNonTypeTemplateParmExpr *Node) {
2499
  Visit(Node->getReplacement());
2500
}
2501

2502
void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2503
  OS << *E->getParameterPack();
2504
}
2505

2506
void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2507
  PrintExpr(Node->getSubExpr());
2508
}
2509

2510
void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2511
  OS << "(";
2512
  if (E->getLHS()) {
2513
    PrintExpr(E->getLHS());
2514
    OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2515
  }
2516
  OS << "...";
2517
  if (E->getRHS()) {
2518
    OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2519
    PrintExpr(E->getRHS());
2520
  }
2521
  OS << ")";
2522
}
2523

2524
void StmtPrinter::VisitCXXParenListInitExpr(CXXParenListInitExpr *Node) {
2525
  OS << "(";
2526
  llvm::interleaveComma(Node->getInitExprs(), OS,
2527
                        [&](Expr *E) { PrintExpr(E); });
2528
  OS << ")";
2529
}
2530

2531
void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
2532
  NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc();
2533
  if (NNS)
2534
    NNS.getNestedNameSpecifier()->print(OS, Policy);
2535
  if (E->getTemplateKWLoc().isValid())
2536
    OS << "template ";
2537
  OS << E->getFoundDecl()->getName();
2538
  printTemplateArgumentList(OS, E->getTemplateArgsAsWritten()->arguments(),
2539
                            Policy,
2540
                            E->getNamedConcept()->getTemplateParameters());
2541
}
2542

2543
void StmtPrinter::VisitRequiresExpr(RequiresExpr *E) {
2544
  OS << "requires ";
2545
  auto LocalParameters = E->getLocalParameters();
2546
  if (!LocalParameters.empty()) {
2547
    OS << "(";
2548
    for (ParmVarDecl *LocalParam : LocalParameters) {
2549
      PrintRawDecl(LocalParam);
2550
      if (LocalParam != LocalParameters.back())
2551
        OS << ", ";
2552
    }
2553

2554
    OS << ") ";
2555
  }
2556
  OS << "{ ";
2557
  auto Requirements = E->getRequirements();
2558
  for (concepts::Requirement *Req : Requirements) {
2559
    if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) {
2560
      if (TypeReq->isSubstitutionFailure())
2561
        OS << "<<error-type>>";
2562
      else
2563
        TypeReq->getType()->getType().print(OS, Policy);
2564
    } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) {
2565
      if (ExprReq->isCompound())
2566
        OS << "{ ";
2567
      if (ExprReq->isExprSubstitutionFailure())
2568
        OS << "<<error-expression>>";
2569
      else
2570
        PrintExpr(ExprReq->getExpr());
2571
      if (ExprReq->isCompound()) {
2572
        OS << " }";
2573
        if (ExprReq->getNoexceptLoc().isValid())
2574
          OS << " noexcept";
2575
        const auto &RetReq = ExprReq->getReturnTypeRequirement();
2576
        if (!RetReq.isEmpty()) {
2577
          OS << " -> ";
2578
          if (RetReq.isSubstitutionFailure())
2579
            OS << "<<error-type>>";
2580
          else if (RetReq.isTypeConstraint())
2581
            RetReq.getTypeConstraint()->print(OS, Policy);
2582
        }
2583
      }
2584
    } else {
2585
      auto *NestedReq = cast<concepts::NestedRequirement>(Req);
2586
      OS << "requires ";
2587
      if (NestedReq->hasInvalidConstraint())
2588
        OS << "<<error-expression>>";
2589
      else
2590
        PrintExpr(NestedReq->getConstraintExpr());
2591
    }
2592
    OS << "; ";
2593
  }
2594
  OS << "}";
2595
}
2596

2597
// C++ Coroutines
2598

2599
void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2600
  Visit(S->getBody());
2601
}
2602

2603
void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2604
  OS << "co_return";
2605
  if (S->getOperand()) {
2606
    OS << " ";
2607
    Visit(S->getOperand());
2608
  }
2609
  OS << ";";
2610
}
2611

2612
void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2613
  OS << "co_await ";
2614
  PrintExpr(S->getOperand());
2615
}
2616

2617
void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2618
  OS << "co_await ";
2619
  PrintExpr(S->getOperand());
2620
}
2621

2622
void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2623
  OS << "co_yield ";
2624
  PrintExpr(S->getOperand());
2625
}
2626

2627
// Obj-C
2628

2629
void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2630
  OS << "@";
2631
  VisitStringLiteral(Node->getString());
2632
}
2633

2634
void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2635
  OS << "@";
2636
  Visit(E->getSubExpr());
2637
}
2638

2639
void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2640
  OS << "@[ ";
2641
  ObjCArrayLiteral::child_range Ch = E->children();
2642
  for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2643
    if (I != Ch.begin())
2644
      OS << ", ";
2645
    Visit(*I);
2646
  }
2647
  OS << " ]";
2648
}
2649

2650
void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2651
  OS << "@{ ";
2652
  for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2653
    if (I > 0)
2654
      OS << ", ";
2655

2656
    ObjCDictionaryElement Element = E->getKeyValueElement(I);
2657
    Visit(Element.Key);
2658
    OS << " : ";
2659
    Visit(Element.Value);
2660
    if (Element.isPackExpansion())
2661
      OS << "...";
2662
  }
2663
  OS << " }";
2664
}
2665

2666
void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2667
  OS << "@encode(";
2668
  Node->getEncodedType().print(OS, Policy);
2669
  OS << ')';
2670
}
2671

2672
void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2673
  OS << "@selector(";
2674
  Node->getSelector().print(OS);
2675
  OS << ')';
2676
}
2677

2678
void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2679
  OS << "@protocol(" << *Node->getProtocol() << ')';
2680
}
2681

2682
void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2683
  OS << "[";
2684
  switch (Mess->getReceiverKind()) {
2685
  case ObjCMessageExpr::Instance:
2686
    PrintExpr(Mess->getInstanceReceiver());
2687
    break;
2688

2689
  case ObjCMessageExpr::Class:
2690
    Mess->getClassReceiver().print(OS, Policy);
2691
    break;
2692

2693
  case ObjCMessageExpr::SuperInstance:
2694
  case ObjCMessageExpr::SuperClass:
2695
    OS << "Super";
2696
    break;
2697
  }
2698

2699
  OS << ' ';
2700
  Selector selector = Mess->getSelector();
2701
  if (selector.isUnarySelector()) {
2702
    OS << selector.getNameForSlot(0);
2703
  } else {
2704
    for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2705
      if (i < selector.getNumArgs()) {
2706
        if (i > 0) OS << ' ';
2707
        if (selector.getIdentifierInfoForSlot(i))
2708
          OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2709
        else
2710
           OS << ":";
2711
      }
2712
      else OS << ", "; // Handle variadic methods.
2713

2714
      PrintExpr(Mess->getArg(i));
2715
    }
2716
  }
2717
  OS << "]";
2718
}
2719

2720
void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2721
  OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2722
}
2723

2724
void
2725
StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2726
  PrintExpr(E->getSubExpr());
2727
}
2728

2729
void
2730
StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2731
  OS << '(' << E->getBridgeKindName();
2732
  E->getType().print(OS, Policy);
2733
  OS << ')';
2734
  PrintExpr(E->getSubExpr());
2735
}
2736

2737
void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2738
  BlockDecl *BD = Node->getBlockDecl();
2739
  OS << "^";
2740

2741
  const FunctionType *AFT = Node->getFunctionType();
2742

2743
  if (isa<FunctionNoProtoType>(AFT)) {
2744
    OS << "()";
2745
  } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2746
    OS << '(';
2747
    for (BlockDecl::param_iterator AI = BD->param_begin(),
2748
         E = BD->param_end(); AI != E; ++AI) {
2749
      if (AI != BD->param_begin()) OS << ", ";
2750
      std::string ParamStr = (*AI)->getNameAsString();
2751
      (*AI)->getType().print(OS, Policy, ParamStr);
2752
    }
2753

2754
    const auto *FT = cast<FunctionProtoType>(AFT);
2755
    if (FT->isVariadic()) {
2756
      if (!BD->param_empty()) OS << ", ";
2757
      OS << "...";
2758
    }
2759
    OS << ')';
2760
  }
2761
  OS << "{ }";
2762
}
2763

2764
void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2765
  PrintExpr(Node->getSourceExpr());
2766
}
2767

2768
void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2769
  // TODO: Print something reasonable for a TypoExpr, if necessary.
2770
  llvm_unreachable("Cannot print TypoExpr nodes");
2771
}
2772

2773
void StmtPrinter::VisitRecoveryExpr(RecoveryExpr *Node) {
2774
  OS << "<recovery-expr>(";
2775
  const char *Sep = "";
2776
  for (Expr *E : Node->subExpressions()) {
2777
    OS << Sep;
2778
    PrintExpr(E);
2779
    Sep = ", ";
2780
  }
2781
  OS << ')';
2782
}
2783

2784
void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2785
  OS << "__builtin_astype(";
2786
  PrintExpr(Node->getSrcExpr());
2787
  OS << ", ";
2788
  Node->getType().print(OS, Policy);
2789
  OS << ")";
2790
}
2791

2792
//===----------------------------------------------------------------------===//
2793
// Stmt method implementations
2794
//===----------------------------------------------------------------------===//
2795

2796
void Stmt::dumpPretty(const ASTContext &Context) const {
2797
  printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2798
}
2799

2800
void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
2801
                       const PrintingPolicy &Policy, unsigned Indentation,
2802
                       StringRef NL, const ASTContext *Context) const {
2803
  StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2804
  P.Visit(const_cast<Stmt *>(this));
2805
}
2806

2807
void Stmt::printPrettyControlled(raw_ostream &Out, PrinterHelper *Helper,
2808
                                 const PrintingPolicy &Policy,
2809
                                 unsigned Indentation, StringRef NL,
2810
                                 const ASTContext *Context) const {
2811
  StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2812
  P.PrintControlledStmt(const_cast<Stmt *>(this));
2813
}
2814

2815
void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
2816
                     const PrintingPolicy &Policy, bool AddQuotes) const {
2817
  std::string Buf;
2818
  llvm::raw_string_ostream TempOut(Buf);
2819

2820
  printPretty(TempOut, Helper, Policy);
2821

2822
  Out << JsonFormat(TempOut.str(), AddQuotes);
2823
}
2824

2825
//===----------------------------------------------------------------------===//
2826
// PrinterHelper
2827
//===----------------------------------------------------------------------===//
2828

2829
// Implement virtual destructor.
2830
PrinterHelper::~PrinterHelper() = default;
2831

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.