1 //===- BuildTree.cpp ------------------------------------------*- C++ -*-=====//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "clang/Tooling/Syntax/BuildTree.h"
9 #include "clang/AST/ASTFwd.h"
10 #include "clang/AST/Decl.h"
11 #include "clang/AST/DeclBase.h"
12 #include "clang/AST/DeclCXX.h"
13 #include "clang/AST/DeclarationName.h"
14 #include "clang/AST/Expr.h"
15 #include "clang/AST/ExprCXX.h"
16 #include "clang/AST/RecursiveASTVisitor.h"
17 #include "clang/AST/Stmt.h"
18 #include "clang/AST/TypeLoc.h"
19 #include "clang/AST/TypeLocVisitor.h"
20 #include "clang/Basic/LLVM.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/Basic/Specifiers.h"
24 #include "clang/Basic/TokenKinds.h"
25 #include "clang/Lex/Lexer.h"
26 #include "clang/Lex/LiteralSupport.h"
27 #include "clang/Tooling/Syntax/Nodes.h"
28 #include "clang/Tooling/Syntax/Tokens.h"
29 #include "clang/Tooling/Syntax/Tree.h"
30 #include "llvm/ADT/ArrayRef.h"
31 #include "llvm/ADT/DenseMap.h"
32 #include "llvm/ADT/PointerUnion.h"
33 #include "llvm/ADT/STLExtras.h"
34 #include "llvm/ADT/ScopeExit.h"
35 #include "llvm/ADT/SmallVector.h"
36 #include "llvm/Support/Allocator.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/Compiler.h"
39 #include "llvm/Support/FormatVariadic.h"
40 #include "llvm/Support/MemoryBuffer.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include <cstddef>
43 #include <map>
44 
45 using namespace clang;
46 
47 LLVM_ATTRIBUTE_UNUSED
48 static bool isImplicitExpr(Expr *E) { return E->IgnoreImplicit() != E; }
49 
50 namespace {
51 /// Get start location of the Declarator from the TypeLoc.
52 /// E.g.:
53 ///   loc of `(` in `int (a)`
54 ///   loc of `*` in `int *(a)`
55 ///   loc of the first `(` in `int (*a)(int)`
56 ///   loc of the `*` in `int *(a)(int)`
57 ///   loc of the first `*` in `const int *const *volatile a;`
58 ///
59 /// It is non-trivial to get the start location because TypeLocs are stored
60 /// inside out. In the example above `*volatile` is the TypeLoc returned
61 /// by `Decl.getTypeSourceInfo()`, and `*const` is what `.getPointeeLoc()`
62 /// returns.
63 struct GetStartLoc : TypeLocVisitor<GetStartLoc, SourceLocation> {
64   SourceLocation VisitParenTypeLoc(ParenTypeLoc T) {
65     auto L = Visit(T.getInnerLoc());
66     if (L.isValid())
67       return L;
68     return T.getLParenLoc();
69   }
70 
71   // Types spelled in the prefix part of the declarator.
72   SourceLocation VisitPointerTypeLoc(PointerTypeLoc T) {
73     return HandlePointer(T);
74   }
75 
76   SourceLocation VisitMemberPointerTypeLoc(MemberPointerTypeLoc T) {
77     return HandlePointer(T);
78   }
79 
80   SourceLocation VisitBlockPointerTypeLoc(BlockPointerTypeLoc T) {
81     return HandlePointer(T);
82   }
83 
84   SourceLocation VisitReferenceTypeLoc(ReferenceTypeLoc T) {
85     return HandlePointer(T);
86   }
87 
88   SourceLocation VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc T) {
89     return HandlePointer(T);
90   }
91 
92   // All other cases are not important, as they are either part of declaration
93   // specifiers (e.g. inheritors of TypeSpecTypeLoc) or introduce modifiers on
94   // existing declarators (e.g. QualifiedTypeLoc). They cannot start the
95   // declarator themselves, but their underlying type can.
96   SourceLocation VisitTypeLoc(TypeLoc T) {
97     auto N = T.getNextTypeLoc();
98     if (!N)
99       return SourceLocation();
100     return Visit(N);
101   }
102 
103   SourceLocation VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc T) {
104     if (T.getTypePtr()->hasTrailingReturn())
105       return SourceLocation(); // avoid recursing into the suffix of declarator.
106     return VisitTypeLoc(T);
107   }
108 
109 private:
110   template <class PtrLoc> SourceLocation HandlePointer(PtrLoc T) {
111     auto L = Visit(T.getPointeeLoc());
112     if (L.isValid())
113       return L;
114     return T.getLocalSourceRange().getBegin();
115   }
116 };
117 } // namespace
118 
119 static syntax::NodeKind getOperatorNodeKind(const CXXOperatorCallExpr &E) {
120   switch (E.getOperator()) {
121   // Comparison
122   case OO_EqualEqual:
123   case OO_ExclaimEqual:
124   case OO_Greater:
125   case OO_GreaterEqual:
126   case OO_Less:
127   case OO_LessEqual:
128   case OO_Spaceship:
129   // Assignment
130   case OO_Equal:
131   case OO_SlashEqual:
132   case OO_PercentEqual:
133   case OO_CaretEqual:
134   case OO_PipeEqual:
135   case OO_LessLessEqual:
136   case OO_GreaterGreaterEqual:
137   case OO_PlusEqual:
138   case OO_MinusEqual:
139   case OO_StarEqual:
140   case OO_AmpEqual:
141   // Binary computation
142   case OO_Slash:
143   case OO_Percent:
144   case OO_Caret:
145   case OO_Pipe:
146   case OO_LessLess:
147   case OO_GreaterGreater:
148   case OO_AmpAmp:
149   case OO_PipePipe:
150   case OO_ArrowStar:
151   case OO_Comma:
152     return syntax::NodeKind::BinaryOperatorExpression;
153   case OO_Tilde:
154   case OO_Exclaim:
155     return syntax::NodeKind::PrefixUnaryOperatorExpression;
156   // Prefix/Postfix increment/decrement
157   case OO_PlusPlus:
158   case OO_MinusMinus:
159     switch (E.getNumArgs()) {
160     case 1:
161       return syntax::NodeKind::PrefixUnaryOperatorExpression;
162     case 2:
163       return syntax::NodeKind::PostfixUnaryOperatorExpression;
164     default:
165       llvm_unreachable("Invalid number of arguments for operator");
166     }
167   // Operators that can be unary or binary
168   case OO_Plus:
169   case OO_Minus:
170   case OO_Star:
171   case OO_Amp:
172     switch (E.getNumArgs()) {
173     case 1:
174       return syntax::NodeKind::PrefixUnaryOperatorExpression;
175     case 2:
176       return syntax::NodeKind::BinaryOperatorExpression;
177     default:
178       llvm_unreachable("Invalid number of arguments for operator");
179     }
180     return syntax::NodeKind::BinaryOperatorExpression;
181   // Not yet supported by SyntaxTree
182   case OO_New:
183   case OO_Delete:
184   case OO_Array_New:
185   case OO_Array_Delete:
186   case OO_Coawait:
187   case OO_Call:
188   case OO_Subscript:
189   case OO_Arrow:
190     return syntax::NodeKind::UnknownExpression;
191   case OO_Conditional: // not overloadable
192   case NUM_OVERLOADED_OPERATORS:
193   case OO_None:
194     llvm_unreachable("Not an overloadable operator");
195   }
196   llvm_unreachable("Unknown OverloadedOperatorKind enum");
197 }
198 
199 /// Gets the range of declarator as defined by the C++ grammar. E.g.
200 ///     `int a;` -> range of `a`,
201 ///     `int *a;` -> range of `*a`,
202 ///     `int a[10];` -> range of `a[10]`,
203 ///     `int a[1][2][3];` -> range of `a[1][2][3]`,
204 ///     `int *a = nullptr` -> range of `*a = nullptr`.
205 /// FIMXE: \p Name must be a source range, e.g. for `operator+`.
206 static SourceRange getDeclaratorRange(const SourceManager &SM, TypeLoc T,
207                                       SourceLocation Name,
208                                       SourceRange Initializer) {
209   SourceLocation Start = GetStartLoc().Visit(T);
210   SourceLocation End = T.getSourceRange().getEnd();
211   assert(End.isValid());
212   if (Name.isValid()) {
213     if (Start.isInvalid())
214       Start = Name;
215     if (SM.isBeforeInTranslationUnit(End, Name))
216       End = Name;
217   }
218   if (Initializer.isValid()) {
219     auto InitializerEnd = Initializer.getEnd();
220     assert(SM.isBeforeInTranslationUnit(End, InitializerEnd) ||
221            End == InitializerEnd);
222     End = InitializerEnd;
223   }
224   return SourceRange(Start, End);
225 }
226 
227 namespace {
228 /// All AST hierarchy roots that can be represented as pointers.
229 using ASTPtr = llvm::PointerUnion<Stmt *, Decl *>;
230 /// Maintains a mapping from AST to syntax tree nodes. This class will get more
231 /// complicated as we support more kinds of AST nodes, e.g. TypeLocs.
232 /// FIXME: expose this as public API.
233 class ASTToSyntaxMapping {
234 public:
235   void add(ASTPtr From, syntax::Tree *To) {
236     assert(To != nullptr);
237     assert(!From.isNull());
238 
239     bool Added = Nodes.insert({From, To}).second;
240     (void)Added;
241     assert(Added && "mapping added twice");
242   }
243 
244   void add(NestedNameSpecifierLoc From, syntax::Tree *To) {
245     assert(To != nullptr);
246     assert(From.hasQualifier());
247 
248     bool Added = NNSNodes.insert({From, To}).second;
249     (void)Added;
250     assert(Added && "mapping added twice");
251   }
252 
253   syntax::Tree *find(ASTPtr P) const { return Nodes.lookup(P); }
254 
255   syntax::Tree *find(NestedNameSpecifierLoc P) const {
256     return NNSNodes.lookup(P);
257   }
258 
259 private:
260   llvm::DenseMap<ASTPtr, syntax::Tree *> Nodes;
261   llvm::DenseMap<NestedNameSpecifierLoc, syntax::Tree *> NNSNodes;
262 };
263 } // namespace
264 
265 /// A helper class for constructing the syntax tree while traversing a clang
266 /// AST.
267 ///
268 /// At each point of the traversal we maintain a list of pending nodes.
269 /// Initially all tokens are added as pending nodes. When processing a clang AST
270 /// node, the clients need to:
271 ///   - create a corresponding syntax node,
272 ///   - assign roles to all pending child nodes with 'markChild' and
273 ///     'markChildToken',
274 ///   - replace the child nodes with the new syntax node in the pending list
275 ///     with 'foldNode'.
276 ///
277 /// Note that all children are expected to be processed when building a node.
278 ///
279 /// Call finalize() to finish building the tree and consume the root node.
280 class syntax::TreeBuilder {
281 public:
282   TreeBuilder(syntax::Arena &Arena) : Arena(Arena), Pending(Arena) {
283     for (const auto &T : Arena.tokenBuffer().expandedTokens())
284       LocationToToken.insert({T.location().getRawEncoding(), &T});
285   }
286 
287   llvm::BumpPtrAllocator &allocator() { return Arena.allocator(); }
288   const SourceManager &sourceManager() const { return Arena.sourceManager(); }
289 
290   /// Populate children for \p New node, assuming it covers tokens from \p
291   /// Range.
292   void foldNode(ArrayRef<syntax::Token> Range, syntax::Tree *New, ASTPtr From) {
293     assert(New);
294     Pending.foldChildren(Arena, Range, New);
295     if (From)
296       Mapping.add(From, New);
297   }
298 
299   void foldNode(ArrayRef<syntax::Token> Range, syntax::Tree *New, TypeLoc L) {
300     // FIXME: add mapping for TypeLocs
301     foldNode(Range, New, nullptr);
302   }
303 
304   void foldNode(llvm::ArrayRef<syntax::Token> Range, syntax::Tree *New,
305                 NestedNameSpecifierLoc From) {
306     assert(New);
307     Pending.foldChildren(Arena, Range, New);
308     if (From)
309       Mapping.add(From, New);
310   }
311 
312   /// Notifies that we should not consume trailing semicolon when computing
313   /// token range of \p D.
314   void noticeDeclWithoutSemicolon(Decl *D);
315 
316   /// Mark the \p Child node with a corresponding \p Role. All marked children
317   /// should be consumed by foldNode.
318   /// When called on expressions (clang::Expr is derived from clang::Stmt),
319   /// wraps expressions into expression statement.
320   void markStmtChild(Stmt *Child, NodeRole Role);
321   /// Should be called for expressions in non-statement position to avoid
322   /// wrapping into expression statement.
323   void markExprChild(Expr *Child, NodeRole Role);
324   /// Set role for a token starting at \p Loc.
325   void markChildToken(SourceLocation Loc, NodeRole R);
326   /// Set role for \p T.
327   void markChildToken(const syntax::Token *T, NodeRole R);
328 
329   /// Set role for \p N.
330   void markChild(syntax::Node *N, NodeRole R);
331   /// Set role for the syntax node matching \p N.
332   void markChild(ASTPtr N, NodeRole R);
333   /// Set role for the syntax node matching \p N.
334   void markChild(NestedNameSpecifierLoc N, NodeRole R);
335 
336   /// Finish building the tree and consume the root node.
337   syntax::TranslationUnit *finalize() && {
338     auto Tokens = Arena.tokenBuffer().expandedTokens();
339     assert(!Tokens.empty());
340     assert(Tokens.back().kind() == tok::eof);
341 
342     // Build the root of the tree, consuming all the children.
343     Pending.foldChildren(Arena, Tokens.drop_back(),
344                          new (Arena.allocator()) syntax::TranslationUnit);
345 
346     auto *TU = cast<syntax::TranslationUnit>(std::move(Pending).finalize());
347     TU->assertInvariantsRecursive();
348     return TU;
349   }
350 
351   /// Finds a token starting at \p L. The token must exist if \p L is valid.
352   const syntax::Token *findToken(SourceLocation L) const;
353 
354   /// Finds the syntax tokens corresponding to the \p SourceRange.
355   ArrayRef<syntax::Token> getRange(SourceRange Range) const {
356     assert(Range.isValid());
357     return getRange(Range.getBegin(), Range.getEnd());
358   }
359 
360   /// Finds the syntax tokens corresponding to the passed source locations.
361   /// \p First is the start position of the first token and \p Last is the start
362   /// position of the last token.
363   ArrayRef<syntax::Token> getRange(SourceLocation First,
364                                    SourceLocation Last) const {
365     assert(First.isValid());
366     assert(Last.isValid());
367     assert(First == Last ||
368            Arena.sourceManager().isBeforeInTranslationUnit(First, Last));
369     return llvm::makeArrayRef(findToken(First), std::next(findToken(Last)));
370   }
371 
372   ArrayRef<syntax::Token>
373   getTemplateRange(const ClassTemplateSpecializationDecl *D) const {
374     auto Tokens = getRange(D->getSourceRange());
375     return maybeAppendSemicolon(Tokens, D);
376   }
377 
378   /// Returns true if \p D is the last declarator in a chain and is thus
379   /// reponsible for creating SimpleDeclaration for the whole chain.
380   template <class T>
381   bool isResponsibleForCreatingDeclaration(const T *D) const {
382     static_assert((std::is_base_of<DeclaratorDecl, T>::value ||
383                    std::is_base_of<TypedefNameDecl, T>::value),
384                   "only DeclaratorDecl and TypedefNameDecl are supported.");
385 
386     const Decl *Next = D->getNextDeclInContext();
387 
388     // There's no next sibling, this one is responsible.
389     if (Next == nullptr) {
390       return true;
391     }
392     const auto *NextT = dyn_cast<T>(Next);
393 
394     // Next sibling is not the same type, this one is responsible.
395     if (NextT == nullptr) {
396       return true;
397     }
398     // Next sibling doesn't begin at the same loc, it must be a different
399     // declaration, so this declarator is responsible.
400     if (NextT->getBeginLoc() != D->getBeginLoc()) {
401       return true;
402     }
403 
404     // NextT is a member of the same declaration, and we need the last member to
405     // create declaration. This one is not responsible.
406     return false;
407   }
408 
409   ArrayRef<syntax::Token> getDeclarationRange(Decl *D) {
410     ArrayRef<syntax::Token> Tokens;
411     // We want to drop the template parameters for specializations.
412     if (const auto *S = dyn_cast<TagDecl>(D))
413       Tokens = getRange(S->TypeDecl::getBeginLoc(), S->getEndLoc());
414     else
415       Tokens = getRange(D->getSourceRange());
416     return maybeAppendSemicolon(Tokens, D);
417   }
418 
419   ArrayRef<syntax::Token> getExprRange(const Expr *E) const {
420     return getRange(E->getSourceRange());
421   }
422 
423   /// Find the adjusted range for the statement, consuming the trailing
424   /// semicolon when needed.
425   ArrayRef<syntax::Token> getStmtRange(const Stmt *S) const {
426     auto Tokens = getRange(S->getSourceRange());
427     if (isa<CompoundStmt>(S))
428       return Tokens;
429 
430     // Some statements miss a trailing semicolon, e.g. 'return', 'continue' and
431     // all statements that end with those. Consume this semicolon here.
432     if (Tokens.back().kind() == tok::semi)
433       return Tokens;
434     return withTrailingSemicolon(Tokens);
435   }
436 
437 private:
438   ArrayRef<syntax::Token> maybeAppendSemicolon(ArrayRef<syntax::Token> Tokens,
439                                                const Decl *D) const {
440     if (isa<NamespaceDecl>(D))
441       return Tokens;
442     if (DeclsWithoutSemicolons.count(D))
443       return Tokens;
444     // FIXME: do not consume trailing semicolon on function definitions.
445     // Most declarations own a semicolon in syntax trees, but not in clang AST.
446     return withTrailingSemicolon(Tokens);
447   }
448 
449   ArrayRef<syntax::Token>
450   withTrailingSemicolon(ArrayRef<syntax::Token> Tokens) const {
451     assert(!Tokens.empty());
452     assert(Tokens.back().kind() != tok::eof);
453     // We never consume 'eof', so looking at the next token is ok.
454     if (Tokens.back().kind() != tok::semi && Tokens.end()->kind() == tok::semi)
455       return llvm::makeArrayRef(Tokens.begin(), Tokens.end() + 1);
456     return Tokens;
457   }
458 
459   void setRole(syntax::Node *N, NodeRole R) {
460     assert(N->role() == NodeRole::Detached);
461     N->setRole(R);
462   }
463 
464   /// A collection of trees covering the input tokens.
465   /// When created, each tree corresponds to a single token in the file.
466   /// Clients call 'foldChildren' to attach one or more subtrees to a parent
467   /// node and update the list of trees accordingly.
468   ///
469   /// Ensures that added nodes properly nest and cover the whole token stream.
470   struct Forest {
471     Forest(syntax::Arena &A) {
472       assert(!A.tokenBuffer().expandedTokens().empty());
473       assert(A.tokenBuffer().expandedTokens().back().kind() == tok::eof);
474       // Create all leaf nodes.
475       // Note that we do not have 'eof' in the tree.
476       for (auto &T : A.tokenBuffer().expandedTokens().drop_back()) {
477         auto *L = new (A.allocator()) syntax::Leaf(&T);
478         L->Original = true;
479         L->CanModify = A.tokenBuffer().spelledForExpanded(T).hasValue();
480         Trees.insert(Trees.end(), {&T, L});
481       }
482     }
483 
484     void assignRole(ArrayRef<syntax::Token> Range, syntax::NodeRole Role) {
485       assert(!Range.empty());
486       auto It = Trees.lower_bound(Range.begin());
487       assert(It != Trees.end() && "no node found");
488       assert(It->first == Range.begin() && "no child with the specified range");
489       assert((std::next(It) == Trees.end() ||
490               std::next(It)->first == Range.end()) &&
491              "no child with the specified range");
492       assert(It->second->role() == NodeRole::Detached &&
493              "re-assigning role for a child");
494       It->second->setRole(Role);
495     }
496 
497     /// Add \p Node to the forest and attach child nodes based on \p Tokens.
498     void foldChildren(const syntax::Arena &A, ArrayRef<syntax::Token> Tokens,
499                       syntax::Tree *Node) {
500       // Attach children to `Node`.
501       assert(Node->firstChild() == nullptr && "node already has children");
502 
503       auto *FirstToken = Tokens.begin();
504       auto BeginChildren = Trees.lower_bound(FirstToken);
505 
506       assert((BeginChildren == Trees.end() ||
507               BeginChildren->first == FirstToken) &&
508              "fold crosses boundaries of existing subtrees");
509       auto EndChildren = Trees.lower_bound(Tokens.end());
510       assert(
511           (EndChildren == Trees.end() || EndChildren->first == Tokens.end()) &&
512           "fold crosses boundaries of existing subtrees");
513 
514       // We need to go in reverse order, because we can only prepend.
515       for (auto It = EndChildren; It != BeginChildren; --It) {
516         auto *C = std::prev(It)->second;
517         if (C->role() == NodeRole::Detached)
518           C->setRole(NodeRole::Unknown);
519         Node->prependChildLowLevel(C);
520       }
521 
522       // Mark that this node came from the AST and is backed by the source code.
523       Node->Original = true;
524       Node->CanModify = A.tokenBuffer().spelledForExpanded(Tokens).hasValue();
525 
526       Trees.erase(BeginChildren, EndChildren);
527       Trees.insert({FirstToken, Node});
528     }
529 
530     // EXPECTS: all tokens were consumed and are owned by a single root node.
531     syntax::Node *finalize() && {
532       assert(Trees.size() == 1);
533       auto *Root = Trees.begin()->second;
534       Trees = {};
535       return Root;
536     }
537 
538     std::string str(const syntax::Arena &A) const {
539       std::string R;
540       for (auto It = Trees.begin(); It != Trees.end(); ++It) {
541         unsigned CoveredTokens =
542             It != Trees.end()
543                 ? (std::next(It)->first - It->first)
544                 : A.tokenBuffer().expandedTokens().end() - It->first;
545 
546         R += std::string(
547             formatv("- '{0}' covers '{1}'+{2} tokens\n", It->second->kind(),
548                     It->first->text(A.sourceManager()), CoveredTokens));
549         R += It->second->dump(A);
550       }
551       return R;
552     }
553 
554   private:
555     /// Maps from the start token to a subtree starting at that token.
556     /// Keys in the map are pointers into the array of expanded tokens, so
557     /// pointer order corresponds to the order of preprocessor tokens.
558     std::map<const syntax::Token *, syntax::Node *> Trees;
559   };
560 
561   /// For debugging purposes.
562   std::string str() { return Pending.str(Arena); }
563 
564   syntax::Arena &Arena;
565   /// To quickly find tokens by their start location.
566   llvm::DenseMap</*SourceLocation*/ unsigned, const syntax::Token *>
567       LocationToToken;
568   Forest Pending;
569   llvm::DenseSet<Decl *> DeclsWithoutSemicolons;
570   ASTToSyntaxMapping Mapping;
571 };
572 
573 namespace {
574 class BuildTreeVisitor : public RecursiveASTVisitor<BuildTreeVisitor> {
575 public:
576   explicit BuildTreeVisitor(ASTContext &Context, syntax::TreeBuilder &Builder)
577       : Builder(Builder), Context(Context) {}
578 
579   bool shouldTraversePostOrder() const { return true; }
580 
581   bool WalkUpFromDeclaratorDecl(DeclaratorDecl *DD) {
582     return processDeclaratorAndDeclaration(DD);
583   }
584 
585   bool WalkUpFromTypedefNameDecl(TypedefNameDecl *TD) {
586     return processDeclaratorAndDeclaration(TD);
587   }
588 
589   bool VisitDecl(Decl *D) {
590     assert(!D->isImplicit());
591     Builder.foldNode(Builder.getDeclarationRange(D),
592                      new (allocator()) syntax::UnknownDeclaration(), D);
593     return true;
594   }
595 
596   // RAV does not call WalkUpFrom* on explicit instantiations, so we have to
597   // override Traverse.
598   // FIXME: make RAV call WalkUpFrom* instead.
599   bool
600   TraverseClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *C) {
601     if (!RecursiveASTVisitor::TraverseClassTemplateSpecializationDecl(C))
602       return false;
603     if (C->isExplicitSpecialization())
604       return true; // we are only interested in explicit instantiations.
605     auto *Declaration =
606         cast<syntax::SimpleDeclaration>(handleFreeStandingTagDecl(C));
607     foldExplicitTemplateInstantiation(
608         Builder.getTemplateRange(C), Builder.findToken(C->getExternLoc()),
609         Builder.findToken(C->getTemplateKeywordLoc()), Declaration, C);
610     return true;
611   }
612 
613   bool WalkUpFromTemplateDecl(TemplateDecl *S) {
614     foldTemplateDeclaration(
615         Builder.getDeclarationRange(S),
616         Builder.findToken(S->getTemplateParameters()->getTemplateLoc()),
617         Builder.getDeclarationRange(S->getTemplatedDecl()), S);
618     return true;
619   }
620 
621   bool WalkUpFromTagDecl(TagDecl *C) {
622     // FIXME: build the ClassSpecifier node.
623     if (!C->isFreeStanding()) {
624       assert(C->getNumTemplateParameterLists() == 0);
625       return true;
626     }
627     handleFreeStandingTagDecl(C);
628     return true;
629   }
630 
631   syntax::Declaration *handleFreeStandingTagDecl(TagDecl *C) {
632     assert(C->isFreeStanding());
633     // Class is a declaration specifier and needs a spanning declaration node.
634     auto DeclarationRange = Builder.getDeclarationRange(C);
635     syntax::Declaration *Result = new (allocator()) syntax::SimpleDeclaration;
636     Builder.foldNode(DeclarationRange, Result, nullptr);
637 
638     // Build TemplateDeclaration nodes if we had template parameters.
639     auto ConsumeTemplateParameters = [&](const TemplateParameterList &L) {
640       const auto *TemplateKW = Builder.findToken(L.getTemplateLoc());
641       auto R = llvm::makeArrayRef(TemplateKW, DeclarationRange.end());
642       Result =
643           foldTemplateDeclaration(R, TemplateKW, DeclarationRange, nullptr);
644       DeclarationRange = R;
645     };
646     if (auto *S = dyn_cast<ClassTemplatePartialSpecializationDecl>(C))
647       ConsumeTemplateParameters(*S->getTemplateParameters());
648     for (unsigned I = C->getNumTemplateParameterLists(); 0 < I; --I)
649       ConsumeTemplateParameters(*C->getTemplateParameterList(I - 1));
650     return Result;
651   }
652 
653   bool WalkUpFromTranslationUnitDecl(TranslationUnitDecl *TU) {
654     // We do not want to call VisitDecl(), the declaration for translation
655     // unit is built by finalize().
656     return true;
657   }
658 
659   bool WalkUpFromCompoundStmt(CompoundStmt *S) {
660     using NodeRole = syntax::NodeRole;
661 
662     Builder.markChildToken(S->getLBracLoc(), NodeRole::OpenParen);
663     for (auto *Child : S->body())
664       Builder.markStmtChild(Child, NodeRole::CompoundStatement_statement);
665     Builder.markChildToken(S->getRBracLoc(), NodeRole::CloseParen);
666 
667     Builder.foldNode(Builder.getStmtRange(S),
668                      new (allocator()) syntax::CompoundStatement, S);
669     return true;
670   }
671 
672   // Some statements are not yet handled by syntax trees.
673   bool WalkUpFromStmt(Stmt *S) {
674     Builder.foldNode(Builder.getStmtRange(S),
675                      new (allocator()) syntax::UnknownStatement, S);
676     return true;
677   }
678 
679   bool TraverseCXXForRangeStmt(CXXForRangeStmt *S) {
680     // We override to traverse range initializer as VarDecl.
681     // RAV traverses it as a statement, we produce invalid node kinds in that
682     // case.
683     // FIXME: should do this in RAV instead?
684     bool Result = [&, this]() {
685       if (S->getInit() && !TraverseStmt(S->getInit()))
686         return false;
687       if (S->getLoopVariable() && !TraverseDecl(S->getLoopVariable()))
688         return false;
689       if (S->getRangeInit() && !TraverseStmt(S->getRangeInit()))
690         return false;
691       if (S->getBody() && !TraverseStmt(S->getBody()))
692         return false;
693       return true;
694     }();
695     WalkUpFromCXXForRangeStmt(S);
696     return Result;
697   }
698 
699   bool TraverseStmt(Stmt *S) {
700     if (auto *DS = dyn_cast_or_null<DeclStmt>(S)) {
701       // We want to consume the semicolon, make sure SimpleDeclaration does not.
702       for (auto *D : DS->decls())
703         Builder.noticeDeclWithoutSemicolon(D);
704     } else if (auto *E = dyn_cast_or_null<Expr>(S)) {
705       return RecursiveASTVisitor::TraverseStmt(E->IgnoreImplicit());
706     }
707     return RecursiveASTVisitor::TraverseStmt(S);
708   }
709 
710   // Some expressions are not yet handled by syntax trees.
711   bool WalkUpFromExpr(Expr *E) {
712     assert(!isImplicitExpr(E) && "should be handled by TraverseStmt");
713     Builder.foldNode(Builder.getExprRange(E),
714                      new (allocator()) syntax::UnknownExpression, E);
715     return true;
716   }
717 
718   bool TraverseUserDefinedLiteral(UserDefinedLiteral *S) {
719     // The semantic AST node `UserDefinedLiteral` (UDL) may have one child node
720     // referencing the location of the UDL suffix (`_w` in `1.2_w`). The
721     // UDL suffix location does not point to the beginning of a token, so we
722     // can't represent the UDL suffix as a separate syntax tree node.
723 
724     return WalkUpFromUserDefinedLiteral(S);
725   }
726 
727   syntax::UserDefinedLiteralExpression *
728   buildUserDefinedLiteral(UserDefinedLiteral *S) {
729     switch (S->getLiteralOperatorKind()) {
730     case UserDefinedLiteral::LOK_Integer:
731       return new (allocator()) syntax::IntegerUserDefinedLiteralExpression;
732     case UserDefinedLiteral::LOK_Floating:
733       return new (allocator()) syntax::FloatUserDefinedLiteralExpression;
734     case UserDefinedLiteral::LOK_Character:
735       return new (allocator()) syntax::CharUserDefinedLiteralExpression;
736     case UserDefinedLiteral::LOK_String:
737       return new (allocator()) syntax::StringUserDefinedLiteralExpression;
738     case UserDefinedLiteral::LOK_Raw:
739     case UserDefinedLiteral::LOK_Template:
740       // For raw literal operator and numeric literal operator template we
741       // cannot get the type of the operand in the semantic AST. We get this
742       // information from the token. As integer and floating point have the same
743       // token kind, we run `NumericLiteralParser` again to distinguish them.
744       auto TokLoc = S->getBeginLoc();
745       auto TokSpelling =
746           Builder.findToken(TokLoc)->text(Context.getSourceManager());
747       auto Literal =
748           NumericLiteralParser(TokSpelling, TokLoc, Context.getSourceManager(),
749                                Context.getLangOpts(), Context.getTargetInfo(),
750                                Context.getDiagnostics());
751       if (Literal.isIntegerLiteral())
752         return new (allocator()) syntax::IntegerUserDefinedLiteralExpression;
753       else {
754         assert(Literal.isFloatingLiteral());
755         return new (allocator()) syntax::FloatUserDefinedLiteralExpression;
756       }
757     }
758     llvm_unreachable("Unknown literal operator kind.");
759   }
760 
761   bool WalkUpFromUserDefinedLiteral(UserDefinedLiteral *S) {
762     Builder.markChildToken(S->getBeginLoc(), syntax::NodeRole::LiteralToken);
763     Builder.foldNode(Builder.getExprRange(S), buildUserDefinedLiteral(S), S);
764     return true;
765   }
766 
767   // FIXME: Fix `NestedNameSpecifierLoc::getLocalSourceRange` for the
768   // `DependentTemplateSpecializationType` case.
769   /// Given a nested-name-specifier return the range for the last name
770   /// specifier.
771   ///
772   /// e.g. `std::T::template X<U>::` => `template X<U>::`
773   SourceRange getLocalSourceRange(const NestedNameSpecifierLoc &NNSLoc) {
774     auto SR = NNSLoc.getLocalSourceRange();
775 
776     // The method `NestedNameSpecifierLoc::getLocalSourceRange` *should*
777     // return the desired `SourceRange`, but there is a corner case. For a
778     // `DependentTemplateSpecializationType` this method returns its
779     // qualifiers as well, in other words in the example above this method
780     // returns `T::template X<U>::` instead of only `template X<U>::`
781     if (auto TL = NNSLoc.getTypeLoc()) {
782       if (auto DependentTL =
783               TL.getAs<DependentTemplateSpecializationTypeLoc>()) {
784         // The 'template' keyword is always present in dependent template
785         // specializations. Except in the case of incorrect code
786         // TODO: Treat the case of incorrect code.
787         SR.setBegin(DependentTL.getTemplateKeywordLoc());
788       }
789     }
790 
791     return SR;
792   }
793 
794   syntax::NodeKind getNameSpecifierKind(const NestedNameSpecifier &NNS) {
795     switch (NNS.getKind()) {
796     case NestedNameSpecifier::Global:
797       return syntax::NodeKind::GlobalNameSpecifier;
798     case NestedNameSpecifier::Namespace:
799     case NestedNameSpecifier::NamespaceAlias:
800     case NestedNameSpecifier::Identifier:
801       return syntax::NodeKind::IdentifierNameSpecifier;
802     case NestedNameSpecifier::TypeSpecWithTemplate:
803       return syntax::NodeKind::SimpleTemplateNameSpecifier;
804     case NestedNameSpecifier::TypeSpec: {
805       const auto *NNSType = NNS.getAsType();
806       assert(NNSType);
807       if (isa<DecltypeType>(NNSType))
808         return syntax::NodeKind::DecltypeNameSpecifier;
809       if (isa<TemplateSpecializationType, DependentTemplateSpecializationType>(
810               NNSType))
811         return syntax::NodeKind::SimpleTemplateNameSpecifier;
812       return syntax::NodeKind::IdentifierNameSpecifier;
813     }
814     default:
815       // FIXME: Support Microsoft's __super
816       llvm::report_fatal_error("We don't yet support the __super specifier",
817                                true);
818     }
819   }
820 
821   syntax::NameSpecifier *
822   BuildNameSpecifier(const NestedNameSpecifierLoc &NNSLoc) {
823     assert(NNSLoc.hasQualifier());
824     auto NameSpecifierTokens =
825         Builder.getRange(getLocalSourceRange(NNSLoc)).drop_back();
826     switch (getNameSpecifierKind(*NNSLoc.getNestedNameSpecifier())) {
827     case syntax::NodeKind::GlobalNameSpecifier:
828       return new (allocator()) syntax::GlobalNameSpecifier;
829     case syntax::NodeKind::IdentifierNameSpecifier: {
830       assert(NameSpecifierTokens.size() == 1);
831       Builder.markChildToken(NameSpecifierTokens.begin(),
832                              syntax::NodeRole::Unknown);
833       auto *NS = new (allocator()) syntax::IdentifierNameSpecifier;
834       Builder.foldNode(NameSpecifierTokens, NS, nullptr);
835       return NS;
836     }
837     case syntax::NodeKind::SimpleTemplateNameSpecifier: {
838       // TODO: Build `SimpleTemplateNameSpecifier` children and implement
839       // accessors to them.
840       // Be aware, we cannot do that simply by calling `TraverseTypeLoc`,
841       // some `TypeLoc`s have inside them the previous name specifier and
842       // we want to treat them independently.
843       auto *NS = new (allocator()) syntax::SimpleTemplateNameSpecifier;
844       Builder.foldNode(NameSpecifierTokens, NS, nullptr);
845       return NS;
846     }
847     case syntax::NodeKind::DecltypeNameSpecifier: {
848       const auto TL = NNSLoc.getTypeLoc().castAs<DecltypeTypeLoc>();
849       if (!RecursiveASTVisitor::TraverseDecltypeTypeLoc(TL))
850         return nullptr;
851       auto *NS = new (allocator()) syntax::DecltypeNameSpecifier;
852       // TODO: Implement accessor to `DecltypeNameSpecifier` inner
853       // `DecltypeTypeLoc`.
854       // For that add mapping from `TypeLoc` to `syntax::Node*` then:
855       // Builder.markChild(TypeLoc, syntax::NodeRole);
856       Builder.foldNode(NameSpecifierTokens, NS, nullptr);
857       return NS;
858     }
859     default:
860       llvm_unreachable("getChildKind() does not return this value");
861     }
862   }
863 
864   // To build syntax tree nodes for NestedNameSpecifierLoc we override
865   // Traverse instead of WalkUpFrom because we want to traverse the children
866   // ourselves and build a list instead of a nested tree of name specifier
867   // prefixes.
868   bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc QualifierLoc) {
869     if (!QualifierLoc)
870       return true;
871     for (auto it = QualifierLoc; it; it = it.getPrefix()) {
872       auto *NS = BuildNameSpecifier(it);
873       if (!NS)
874         return false;
875       Builder.markChild(NS, syntax::NodeRole::List_element);
876       Builder.markChildToken(it.getEndLoc(), syntax::NodeRole::List_delimiter);
877     }
878     Builder.foldNode(Builder.getRange(QualifierLoc.getSourceRange()),
879                      new (allocator()) syntax::NestedNameSpecifier,
880                      QualifierLoc);
881     return true;
882   }
883 
884   bool WalkUpFromDeclRefExpr(DeclRefExpr *S) {
885     if (auto QualifierLoc = S->getQualifierLoc())
886       Builder.markChild(QualifierLoc, syntax::NodeRole::IdExpression_qualifier);
887 
888     auto TemplateKeywordLoc = S->getTemplateKeywordLoc();
889     if (TemplateKeywordLoc.isValid())
890       Builder.markChildToken(TemplateKeywordLoc,
891                              syntax::NodeRole::TemplateKeyword);
892 
893     auto *unqualifiedId = new (allocator()) syntax::UnqualifiedId;
894 
895     Builder.foldNode(Builder.getRange(S->getLocation(), S->getEndLoc()),
896                      unqualifiedId, nullptr);
897 
898     Builder.markChild(unqualifiedId, syntax::NodeRole::IdExpression_id);
899 
900     Builder.foldNode(Builder.getExprRange(S),
901                      new (allocator()) syntax::IdExpression, S);
902     return true;
903   }
904 
905   // Same logic as DeclRefExpr.
906   bool WalkUpFromDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *S) {
907     if (auto QualifierLoc = S->getQualifierLoc())
908       Builder.markChild(QualifierLoc, syntax::NodeRole::IdExpression_qualifier);
909 
910     auto TemplateKeywordLoc = S->getTemplateKeywordLoc();
911     if (TemplateKeywordLoc.isValid())
912       Builder.markChildToken(TemplateKeywordLoc,
913                              syntax::NodeRole::TemplateKeyword);
914 
915     auto *unqualifiedId = new (allocator()) syntax::UnqualifiedId;
916 
917     Builder.foldNode(Builder.getRange(S->getLocation(), S->getEndLoc()),
918                      unqualifiedId, nullptr);
919 
920     Builder.markChild(unqualifiedId, syntax::NodeRole::IdExpression_id);
921 
922     Builder.foldNode(Builder.getExprRange(S),
923                      new (allocator()) syntax::IdExpression, S);
924     return true;
925   }
926 
927   bool WalkUpFromParenExpr(ParenExpr *S) {
928     Builder.markChildToken(S->getLParen(), syntax::NodeRole::OpenParen);
929     Builder.markExprChild(S->getSubExpr(),
930                           syntax::NodeRole::ParenExpression_subExpression);
931     Builder.markChildToken(S->getRParen(), syntax::NodeRole::CloseParen);
932     Builder.foldNode(Builder.getExprRange(S),
933                      new (allocator()) syntax::ParenExpression, S);
934     return true;
935   }
936 
937   bool WalkUpFromIntegerLiteral(IntegerLiteral *S) {
938     Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
939     Builder.foldNode(Builder.getExprRange(S),
940                      new (allocator()) syntax::IntegerLiteralExpression, S);
941     return true;
942   }
943 
944   bool WalkUpFromCharacterLiteral(CharacterLiteral *S) {
945     Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
946     Builder.foldNode(Builder.getExprRange(S),
947                      new (allocator()) syntax::CharacterLiteralExpression, S);
948     return true;
949   }
950 
951   bool WalkUpFromFloatingLiteral(FloatingLiteral *S) {
952     Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
953     Builder.foldNode(Builder.getExprRange(S),
954                      new (allocator()) syntax::FloatingLiteralExpression, S);
955     return true;
956   }
957 
958   bool WalkUpFromStringLiteral(StringLiteral *S) {
959     Builder.markChildToken(S->getBeginLoc(), syntax::NodeRole::LiteralToken);
960     Builder.foldNode(Builder.getExprRange(S),
961                      new (allocator()) syntax::StringLiteralExpression, S);
962     return true;
963   }
964 
965   bool WalkUpFromCXXBoolLiteralExpr(CXXBoolLiteralExpr *S) {
966     Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
967     Builder.foldNode(Builder.getExprRange(S),
968                      new (allocator()) syntax::BoolLiteralExpression, S);
969     return true;
970   }
971 
972   bool WalkUpFromCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *S) {
973     Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
974     Builder.foldNode(Builder.getExprRange(S),
975                      new (allocator()) syntax::CxxNullPtrExpression, S);
976     return true;
977   }
978 
979   bool WalkUpFromUnaryOperator(UnaryOperator *S) {
980     Builder.markChildToken(S->getOperatorLoc(),
981                            syntax::NodeRole::OperatorExpression_operatorToken);
982     Builder.markExprChild(S->getSubExpr(),
983                           syntax::NodeRole::UnaryOperatorExpression_operand);
984 
985     if (S->isPostfix())
986       Builder.foldNode(Builder.getExprRange(S),
987                        new (allocator()) syntax::PostfixUnaryOperatorExpression,
988                        S);
989     else
990       Builder.foldNode(Builder.getExprRange(S),
991                        new (allocator()) syntax::PrefixUnaryOperatorExpression,
992                        S);
993 
994     return true;
995   }
996 
997   bool WalkUpFromBinaryOperator(BinaryOperator *S) {
998     Builder.markExprChild(
999         S->getLHS(), syntax::NodeRole::BinaryOperatorExpression_leftHandSide);
1000     Builder.markChildToken(S->getOperatorLoc(),
1001                            syntax::NodeRole::OperatorExpression_operatorToken);
1002     Builder.markExprChild(
1003         S->getRHS(), syntax::NodeRole::BinaryOperatorExpression_rightHandSide);
1004     Builder.foldNode(Builder.getExprRange(S),
1005                      new (allocator()) syntax::BinaryOperatorExpression, S);
1006     return true;
1007   }
1008 
1009   bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *S) {
1010     // To construct a syntax tree of the same shape for calls to built-in and
1011     // user-defined operators, ignore the `DeclRefExpr` that refers to the
1012     // operator and treat it as a simple token. Do that by traversing
1013     // arguments instead of children.
1014     for (auto *child : S->arguments()) {
1015       // A postfix unary operator is declared as taking two operands. The
1016       // second operand is used to distinguish from its prefix counterpart. In
1017       // the semantic AST this "phantom" operand is represented as a
1018       // `IntegerLiteral` with invalid `SourceLocation`. We skip visiting this
1019       // operand because it does not correspond to anything written in source
1020       // code.
1021       if (child->getSourceRange().isInvalid()) {
1022         assert(getOperatorNodeKind(*S) ==
1023                syntax::NodeKind::PostfixUnaryOperatorExpression);
1024         continue;
1025       }
1026       if (!TraverseStmt(child))
1027         return false;
1028     }
1029     return WalkUpFromCXXOperatorCallExpr(S);
1030   }
1031 
1032   bool WalkUpFromCXXOperatorCallExpr(CXXOperatorCallExpr *S) {
1033     switch (getOperatorNodeKind(*S)) {
1034     case syntax::NodeKind::BinaryOperatorExpression:
1035       Builder.markExprChild(
1036           S->getArg(0),
1037           syntax::NodeRole::BinaryOperatorExpression_leftHandSide);
1038       Builder.markChildToken(
1039           S->getOperatorLoc(),
1040           syntax::NodeRole::OperatorExpression_operatorToken);
1041       Builder.markExprChild(
1042           S->getArg(1),
1043           syntax::NodeRole::BinaryOperatorExpression_rightHandSide);
1044       Builder.foldNode(Builder.getExprRange(S),
1045                        new (allocator()) syntax::BinaryOperatorExpression, S);
1046       return true;
1047     case syntax::NodeKind::PrefixUnaryOperatorExpression:
1048       Builder.markChildToken(
1049           S->getOperatorLoc(),
1050           syntax::NodeRole::OperatorExpression_operatorToken);
1051       Builder.markExprChild(S->getArg(0),
1052                             syntax::NodeRole::UnaryOperatorExpression_operand);
1053       Builder.foldNode(Builder.getExprRange(S),
1054                        new (allocator()) syntax::PrefixUnaryOperatorExpression,
1055                        S);
1056       return true;
1057     case syntax::NodeKind::PostfixUnaryOperatorExpression:
1058       Builder.markChildToken(
1059           S->getOperatorLoc(),
1060           syntax::NodeRole::OperatorExpression_operatorToken);
1061       Builder.markExprChild(S->getArg(0),
1062                             syntax::NodeRole::UnaryOperatorExpression_operand);
1063       Builder.foldNode(Builder.getExprRange(S),
1064                        new (allocator()) syntax::PostfixUnaryOperatorExpression,
1065                        S);
1066       return true;
1067     case syntax::NodeKind::UnknownExpression:
1068       return RecursiveASTVisitor::WalkUpFromCXXOperatorCallExpr(S);
1069     default:
1070       llvm_unreachable("getOperatorNodeKind() does not return this value");
1071     }
1072   }
1073 
1074   bool WalkUpFromNamespaceDecl(NamespaceDecl *S) {
1075     auto Tokens = Builder.getDeclarationRange(S);
1076     if (Tokens.front().kind() == tok::coloncolon) {
1077       // Handle nested namespace definitions. Those start at '::' token, e.g.
1078       // namespace a^::b {}
1079       // FIXME: build corresponding nodes for the name of this namespace.
1080       return true;
1081     }
1082     Builder.foldNode(Tokens, new (allocator()) syntax::NamespaceDefinition, S);
1083     return true;
1084   }
1085 
1086   // FIXME: Deleting the `TraverseParenTypeLoc` override doesn't change test
1087   // results. Find test coverage or remove it.
1088   bool TraverseParenTypeLoc(ParenTypeLoc L) {
1089     // We reverse order of traversal to get the proper syntax structure.
1090     if (!WalkUpFromParenTypeLoc(L))
1091       return false;
1092     return TraverseTypeLoc(L.getInnerLoc());
1093   }
1094 
1095   bool WalkUpFromParenTypeLoc(ParenTypeLoc L) {
1096     Builder.markChildToken(L.getLParenLoc(), syntax::NodeRole::OpenParen);
1097     Builder.markChildToken(L.getRParenLoc(), syntax::NodeRole::CloseParen);
1098     Builder.foldNode(Builder.getRange(L.getLParenLoc(), L.getRParenLoc()),
1099                      new (allocator()) syntax::ParenDeclarator, L);
1100     return true;
1101   }
1102 
1103   // Declarator chunks, they are produced by type locs and some clang::Decls.
1104   bool WalkUpFromArrayTypeLoc(ArrayTypeLoc L) {
1105     Builder.markChildToken(L.getLBracketLoc(), syntax::NodeRole::OpenParen);
1106     Builder.markExprChild(L.getSizeExpr(),
1107                           syntax::NodeRole::ArraySubscript_sizeExpression);
1108     Builder.markChildToken(L.getRBracketLoc(), syntax::NodeRole::CloseParen);
1109     Builder.foldNode(Builder.getRange(L.getLBracketLoc(), L.getRBracketLoc()),
1110                      new (allocator()) syntax::ArraySubscript, L);
1111     return true;
1112   }
1113 
1114   bool WalkUpFromFunctionTypeLoc(FunctionTypeLoc L) {
1115     Builder.markChildToken(L.getLParenLoc(), syntax::NodeRole::OpenParen);
1116     for (auto *P : L.getParams()) {
1117       Builder.markChild(P, syntax::NodeRole::ParametersAndQualifiers_parameter);
1118     }
1119     Builder.markChildToken(L.getRParenLoc(), syntax::NodeRole::CloseParen);
1120     Builder.foldNode(Builder.getRange(L.getLParenLoc(), L.getEndLoc()),
1121                      new (allocator()) syntax::ParametersAndQualifiers, L);
1122     return true;
1123   }
1124 
1125   bool WalkUpFromFunctionProtoTypeLoc(FunctionProtoTypeLoc L) {
1126     if (!L.getTypePtr()->hasTrailingReturn())
1127       return WalkUpFromFunctionTypeLoc(L);
1128 
1129     auto *TrailingReturnTokens = BuildTrailingReturn(L);
1130     // Finish building the node for parameters.
1131     Builder.markChild(TrailingReturnTokens,
1132                       syntax::NodeRole::ParametersAndQualifiers_trailingReturn);
1133     return WalkUpFromFunctionTypeLoc(L);
1134   }
1135 
1136   bool TraverseMemberPointerTypeLoc(MemberPointerTypeLoc L) {
1137     // In the source code "void (Y::*mp)()" `MemberPointerTypeLoc` corresponds
1138     // to "Y::*" but it points to a `ParenTypeLoc` that corresponds to
1139     // "(Y::*mp)" We thus reverse the order of traversal to get the proper
1140     // syntax structure.
1141     if (!WalkUpFromMemberPointerTypeLoc(L))
1142       return false;
1143     return TraverseTypeLoc(L.getPointeeLoc());
1144   }
1145 
1146   bool WalkUpFromMemberPointerTypeLoc(MemberPointerTypeLoc L) {
1147     auto SR = L.getLocalSourceRange();
1148     Builder.foldNode(Builder.getRange(SR),
1149                      new (allocator()) syntax::MemberPointer, L);
1150     return true;
1151   }
1152 
1153   // The code below is very regular, it could even be generated with some
1154   // preprocessor magic. We merely assign roles to the corresponding children
1155   // and fold resulting nodes.
1156   bool WalkUpFromDeclStmt(DeclStmt *S) {
1157     Builder.foldNode(Builder.getStmtRange(S),
1158                      new (allocator()) syntax::DeclarationStatement, S);
1159     return true;
1160   }
1161 
1162   bool WalkUpFromNullStmt(NullStmt *S) {
1163     Builder.foldNode(Builder.getStmtRange(S),
1164                      new (allocator()) syntax::EmptyStatement, S);
1165     return true;
1166   }
1167 
1168   bool WalkUpFromSwitchStmt(SwitchStmt *S) {
1169     Builder.markChildToken(S->getSwitchLoc(),
1170                            syntax::NodeRole::IntroducerKeyword);
1171     Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1172     Builder.foldNode(Builder.getStmtRange(S),
1173                      new (allocator()) syntax::SwitchStatement, S);
1174     return true;
1175   }
1176 
1177   bool WalkUpFromCaseStmt(CaseStmt *S) {
1178     Builder.markChildToken(S->getKeywordLoc(),
1179                            syntax::NodeRole::IntroducerKeyword);
1180     Builder.markExprChild(S->getLHS(), syntax::NodeRole::CaseStatement_value);
1181     Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement);
1182     Builder.foldNode(Builder.getStmtRange(S),
1183                      new (allocator()) syntax::CaseStatement, S);
1184     return true;
1185   }
1186 
1187   bool WalkUpFromDefaultStmt(DefaultStmt *S) {
1188     Builder.markChildToken(S->getKeywordLoc(),
1189                            syntax::NodeRole::IntroducerKeyword);
1190     Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement);
1191     Builder.foldNode(Builder.getStmtRange(S),
1192                      new (allocator()) syntax::DefaultStatement, S);
1193     return true;
1194   }
1195 
1196   bool WalkUpFromIfStmt(IfStmt *S) {
1197     Builder.markChildToken(S->getIfLoc(), syntax::NodeRole::IntroducerKeyword);
1198     Builder.markStmtChild(S->getThen(),
1199                           syntax::NodeRole::IfStatement_thenStatement);
1200     Builder.markChildToken(S->getElseLoc(),
1201                            syntax::NodeRole::IfStatement_elseKeyword);
1202     Builder.markStmtChild(S->getElse(),
1203                           syntax::NodeRole::IfStatement_elseStatement);
1204     Builder.foldNode(Builder.getStmtRange(S),
1205                      new (allocator()) syntax::IfStatement, S);
1206     return true;
1207   }
1208 
1209   bool WalkUpFromForStmt(ForStmt *S) {
1210     Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword);
1211     Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1212     Builder.foldNode(Builder.getStmtRange(S),
1213                      new (allocator()) syntax::ForStatement, S);
1214     return true;
1215   }
1216 
1217   bool WalkUpFromWhileStmt(WhileStmt *S) {
1218     Builder.markChildToken(S->getWhileLoc(),
1219                            syntax::NodeRole::IntroducerKeyword);
1220     Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1221     Builder.foldNode(Builder.getStmtRange(S),
1222                      new (allocator()) syntax::WhileStatement, S);
1223     return true;
1224   }
1225 
1226   bool WalkUpFromContinueStmt(ContinueStmt *S) {
1227     Builder.markChildToken(S->getContinueLoc(),
1228                            syntax::NodeRole::IntroducerKeyword);
1229     Builder.foldNode(Builder.getStmtRange(S),
1230                      new (allocator()) syntax::ContinueStatement, S);
1231     return true;
1232   }
1233 
1234   bool WalkUpFromBreakStmt(BreakStmt *S) {
1235     Builder.markChildToken(S->getBreakLoc(),
1236                            syntax::NodeRole::IntroducerKeyword);
1237     Builder.foldNode(Builder.getStmtRange(S),
1238                      new (allocator()) syntax::BreakStatement, S);
1239     return true;
1240   }
1241 
1242   bool WalkUpFromReturnStmt(ReturnStmt *S) {
1243     Builder.markChildToken(S->getReturnLoc(),
1244                            syntax::NodeRole::IntroducerKeyword);
1245     Builder.markExprChild(S->getRetValue(),
1246                           syntax::NodeRole::ReturnStatement_value);
1247     Builder.foldNode(Builder.getStmtRange(S),
1248                      new (allocator()) syntax::ReturnStatement, S);
1249     return true;
1250   }
1251 
1252   bool WalkUpFromCXXForRangeStmt(CXXForRangeStmt *S) {
1253     Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword);
1254     Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1255     Builder.foldNode(Builder.getStmtRange(S),
1256                      new (allocator()) syntax::RangeBasedForStatement, S);
1257     return true;
1258   }
1259 
1260   bool WalkUpFromEmptyDecl(EmptyDecl *S) {
1261     Builder.foldNode(Builder.getDeclarationRange(S),
1262                      new (allocator()) syntax::EmptyDeclaration, S);
1263     return true;
1264   }
1265 
1266   bool WalkUpFromStaticAssertDecl(StaticAssertDecl *S) {
1267     Builder.markExprChild(S->getAssertExpr(),
1268                           syntax::NodeRole::StaticAssertDeclaration_condition);
1269     Builder.markExprChild(S->getMessage(),
1270                           syntax::NodeRole::StaticAssertDeclaration_message);
1271     Builder.foldNode(Builder.getDeclarationRange(S),
1272                      new (allocator()) syntax::StaticAssertDeclaration, S);
1273     return true;
1274   }
1275 
1276   bool WalkUpFromLinkageSpecDecl(LinkageSpecDecl *S) {
1277     Builder.foldNode(Builder.getDeclarationRange(S),
1278                      new (allocator()) syntax::LinkageSpecificationDeclaration,
1279                      S);
1280     return true;
1281   }
1282 
1283   bool WalkUpFromNamespaceAliasDecl(NamespaceAliasDecl *S) {
1284     Builder.foldNode(Builder.getDeclarationRange(S),
1285                      new (allocator()) syntax::NamespaceAliasDefinition, S);
1286     return true;
1287   }
1288 
1289   bool WalkUpFromUsingDirectiveDecl(UsingDirectiveDecl *S) {
1290     Builder.foldNode(Builder.getDeclarationRange(S),
1291                      new (allocator()) syntax::UsingNamespaceDirective, S);
1292     return true;
1293   }
1294 
1295   bool WalkUpFromUsingDecl(UsingDecl *S) {
1296     Builder.foldNode(Builder.getDeclarationRange(S),
1297                      new (allocator()) syntax::UsingDeclaration, S);
1298     return true;
1299   }
1300 
1301   bool WalkUpFromUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *S) {
1302     Builder.foldNode(Builder.getDeclarationRange(S),
1303                      new (allocator()) syntax::UsingDeclaration, S);
1304     return true;
1305   }
1306 
1307   bool WalkUpFromUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *S) {
1308     Builder.foldNode(Builder.getDeclarationRange(S),
1309                      new (allocator()) syntax::UsingDeclaration, S);
1310     return true;
1311   }
1312 
1313   bool WalkUpFromTypeAliasDecl(TypeAliasDecl *S) {
1314     Builder.foldNode(Builder.getDeclarationRange(S),
1315                      new (allocator()) syntax::TypeAliasDeclaration, S);
1316     return true;
1317   }
1318 
1319 private:
1320   template <class T> SourceLocation getQualifiedNameStart(T *D) {
1321     static_assert((std::is_base_of<DeclaratorDecl, T>::value ||
1322                    std::is_base_of<TypedefNameDecl, T>::value),
1323                   "only DeclaratorDecl and TypedefNameDecl are supported.");
1324 
1325     auto DN = D->getDeclName();
1326     bool IsAnonymous = DN.isIdentifier() && !DN.getAsIdentifierInfo();
1327     if (IsAnonymous)
1328       return SourceLocation();
1329 
1330     if (const auto *DD = dyn_cast<DeclaratorDecl>(D)) {
1331       if (DD->getQualifierLoc()) {
1332         return DD->getQualifierLoc().getBeginLoc();
1333       }
1334     }
1335 
1336     return D->getLocation();
1337   }
1338 
1339   SourceRange getInitializerRange(Decl *D) {
1340     if (auto *V = dyn_cast<VarDecl>(D)) {
1341       auto *I = V->getInit();
1342       // Initializers in range-based-for are not part of the declarator
1343       if (I && !V->isCXXForRangeDecl())
1344         return I->getSourceRange();
1345     }
1346 
1347     return SourceRange();
1348   }
1349 
1350   /// Folds SimpleDeclarator node (if present) and in case this is the last
1351   /// declarator in the chain it also folds SimpleDeclaration node.
1352   template <class T> bool processDeclaratorAndDeclaration(T *D) {
1353     SourceRange Initializer = getInitializerRange(D);
1354     auto Range = getDeclaratorRange(Builder.sourceManager(),
1355                                     D->getTypeSourceInfo()->getTypeLoc(),
1356                                     getQualifiedNameStart(D), Initializer);
1357 
1358     // There doesn't have to be a declarator (e.g. `void foo(int)` only has
1359     // declaration, but no declarator).
1360     if (Range.getBegin().isValid()) {
1361       auto *N = new (allocator()) syntax::SimpleDeclarator;
1362       Builder.foldNode(Builder.getRange(Range), N, nullptr);
1363       Builder.markChild(N, syntax::NodeRole::SimpleDeclaration_declarator);
1364     }
1365 
1366     if (Builder.isResponsibleForCreatingDeclaration(D)) {
1367       Builder.foldNode(Builder.getDeclarationRange(D),
1368                        new (allocator()) syntax::SimpleDeclaration, D);
1369     }
1370     return true;
1371   }
1372 
1373   /// Returns the range of the built node.
1374   syntax::TrailingReturnType *BuildTrailingReturn(FunctionProtoTypeLoc L) {
1375     assert(L.getTypePtr()->hasTrailingReturn());
1376 
1377     auto ReturnedType = L.getReturnLoc();
1378     // Build node for the declarator, if any.
1379     auto ReturnDeclaratorRange =
1380         getDeclaratorRange(this->Builder.sourceManager(), ReturnedType,
1381                            /*Name=*/SourceLocation(),
1382                            /*Initializer=*/SourceLocation());
1383     syntax::SimpleDeclarator *ReturnDeclarator = nullptr;
1384     if (ReturnDeclaratorRange.isValid()) {
1385       ReturnDeclarator = new (allocator()) syntax::SimpleDeclarator;
1386       Builder.foldNode(Builder.getRange(ReturnDeclaratorRange),
1387                        ReturnDeclarator, nullptr);
1388     }
1389 
1390     // Build node for trailing return type.
1391     auto Return = Builder.getRange(ReturnedType.getSourceRange());
1392     const auto *Arrow = Return.begin() - 1;
1393     assert(Arrow->kind() == tok::arrow);
1394     auto Tokens = llvm::makeArrayRef(Arrow, Return.end());
1395     Builder.markChildToken(Arrow, syntax::NodeRole::ArrowToken);
1396     if (ReturnDeclarator)
1397       Builder.markChild(ReturnDeclarator,
1398                         syntax::NodeRole::TrailingReturnType_declarator);
1399     auto *R = new (allocator()) syntax::TrailingReturnType;
1400     Builder.foldNode(Tokens, R, L);
1401     return R;
1402   }
1403 
1404   void foldExplicitTemplateInstantiation(
1405       ArrayRef<syntax::Token> Range, const syntax::Token *ExternKW,
1406       const syntax::Token *TemplateKW,
1407       syntax::SimpleDeclaration *InnerDeclaration, Decl *From) {
1408     assert(!ExternKW || ExternKW->kind() == tok::kw_extern);
1409     assert(TemplateKW && TemplateKW->kind() == tok::kw_template);
1410     Builder.markChildToken(ExternKW, syntax::NodeRole::ExternKeyword);
1411     Builder.markChildToken(TemplateKW, syntax::NodeRole::IntroducerKeyword);
1412     Builder.markChild(
1413         InnerDeclaration,
1414         syntax::NodeRole::ExplicitTemplateInstantiation_declaration);
1415     Builder.foldNode(
1416         Range, new (allocator()) syntax::ExplicitTemplateInstantiation, From);
1417   }
1418 
1419   syntax::TemplateDeclaration *foldTemplateDeclaration(
1420       ArrayRef<syntax::Token> Range, const syntax::Token *TemplateKW,
1421       ArrayRef<syntax::Token> TemplatedDeclaration, Decl *From) {
1422     assert(TemplateKW && TemplateKW->kind() == tok::kw_template);
1423     Builder.markChildToken(TemplateKW, syntax::NodeRole::IntroducerKeyword);
1424 
1425     auto *N = new (allocator()) syntax::TemplateDeclaration;
1426     Builder.foldNode(Range, N, From);
1427     Builder.markChild(N, syntax::NodeRole::TemplateDeclaration_declaration);
1428     return N;
1429   }
1430 
1431   /// A small helper to save some typing.
1432   llvm::BumpPtrAllocator &allocator() { return Builder.allocator(); }
1433 
1434   syntax::TreeBuilder &Builder;
1435   const ASTContext &Context;
1436 };
1437 } // namespace
1438 
1439 void syntax::TreeBuilder::noticeDeclWithoutSemicolon(Decl *D) {
1440   DeclsWithoutSemicolons.insert(D);
1441 }
1442 
1443 void syntax::TreeBuilder::markChildToken(SourceLocation Loc, NodeRole Role) {
1444   if (Loc.isInvalid())
1445     return;
1446   Pending.assignRole(*findToken(Loc), Role);
1447 }
1448 
1449 void syntax::TreeBuilder::markChildToken(const syntax::Token *T, NodeRole R) {
1450   if (!T)
1451     return;
1452   Pending.assignRole(*T, R);
1453 }
1454 
1455 void syntax::TreeBuilder::markChild(syntax::Node *N, NodeRole R) {
1456   assert(N);
1457   setRole(N, R);
1458 }
1459 
1460 void syntax::TreeBuilder::markChild(ASTPtr N, NodeRole R) {
1461   auto *SN = Mapping.find(N);
1462   assert(SN != nullptr);
1463   setRole(SN, R);
1464 }
1465 void syntax::TreeBuilder::markChild(NestedNameSpecifierLoc NNSLoc, NodeRole R) {
1466   auto *SN = Mapping.find(NNSLoc);
1467   assert(SN != nullptr);
1468   setRole(SN, R);
1469 }
1470 
1471 void syntax::TreeBuilder::markStmtChild(Stmt *Child, NodeRole Role) {
1472   if (!Child)
1473     return;
1474 
1475   syntax::Tree *ChildNode;
1476   if (Expr *ChildExpr = dyn_cast<Expr>(Child)) {
1477     // This is an expression in a statement position, consume the trailing
1478     // semicolon and form an 'ExpressionStatement' node.
1479     markExprChild(ChildExpr, NodeRole::ExpressionStatement_expression);
1480     ChildNode = new (allocator()) syntax::ExpressionStatement;
1481     // (!) 'getStmtRange()' ensures this covers a trailing semicolon.
1482     Pending.foldChildren(Arena, getStmtRange(Child), ChildNode);
1483   } else {
1484     ChildNode = Mapping.find(Child);
1485   }
1486   assert(ChildNode != nullptr);
1487   setRole(ChildNode, Role);
1488 }
1489 
1490 void syntax::TreeBuilder::markExprChild(Expr *Child, NodeRole Role) {
1491   if (!Child)
1492     return;
1493   Child = Child->IgnoreImplicit();
1494 
1495   syntax::Tree *ChildNode = Mapping.find(Child);
1496   assert(ChildNode != nullptr);
1497   setRole(ChildNode, Role);
1498 }
1499 
1500 const syntax::Token *syntax::TreeBuilder::findToken(SourceLocation L) const {
1501   if (L.isInvalid())
1502     return nullptr;
1503   auto It = LocationToToken.find(L.getRawEncoding());
1504   assert(It != LocationToToken.end());
1505   return It->second;
1506 }
1507 
1508 syntax::TranslationUnit *
1509 syntax::buildSyntaxTree(Arena &A, const TranslationUnitDecl &TU) {
1510   TreeBuilder Builder(A);
1511   BuildTreeVisitor(TU.getASTContext(), Builder).TraverseAST(TU.getASTContext());
1512   return std::move(Builder).finalize();
1513 }
1514