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