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     if (getOperatorNodeKind(*S) ==
1011         syntax::NodeKind::PostfixUnaryOperatorExpression) {
1012       // A postfix unary operator is declared as taking two operands. The
1013       // second operand is used to distinguish from its prefix counterpart. In
1014       // the semantic AST this "phantom" operand is represented as a
1015       // `IntegerLiteral` with invalid `SourceLocation`. We skip visiting this
1016       // operand because it does not correspond to anything written in source
1017       // code
1018       for (auto *child : S->children()) {
1019         if (child->getSourceRange().isInvalid())
1020           continue;
1021         if (!TraverseStmt(child))
1022           return false;
1023       }
1024       return WalkUpFromCXXOperatorCallExpr(S);
1025     } else
1026       return RecursiveASTVisitor::TraverseCXXOperatorCallExpr(S);
1027   }
1028 
1029   bool WalkUpFromCXXOperatorCallExpr(CXXOperatorCallExpr *S) {
1030     switch (getOperatorNodeKind(*S)) {
1031     case syntax::NodeKind::BinaryOperatorExpression:
1032       Builder.markExprChild(
1033           S->getArg(0),
1034           syntax::NodeRole::BinaryOperatorExpression_leftHandSide);
1035       Builder.markChildToken(
1036           S->getOperatorLoc(),
1037           syntax::NodeRole::OperatorExpression_operatorToken);
1038       Builder.markExprChild(
1039           S->getArg(1),
1040           syntax::NodeRole::BinaryOperatorExpression_rightHandSide);
1041       Builder.foldNode(Builder.getExprRange(S),
1042                        new (allocator()) syntax::BinaryOperatorExpression, S);
1043       return true;
1044     case syntax::NodeKind::PrefixUnaryOperatorExpression:
1045       Builder.markChildToken(
1046           S->getOperatorLoc(),
1047           syntax::NodeRole::OperatorExpression_operatorToken);
1048       Builder.markExprChild(S->getArg(0),
1049                             syntax::NodeRole::UnaryOperatorExpression_operand);
1050       Builder.foldNode(Builder.getExprRange(S),
1051                        new (allocator()) syntax::PrefixUnaryOperatorExpression,
1052                        S);
1053       return true;
1054     case syntax::NodeKind::PostfixUnaryOperatorExpression:
1055       Builder.markChildToken(
1056           S->getOperatorLoc(),
1057           syntax::NodeRole::OperatorExpression_operatorToken);
1058       Builder.markExprChild(S->getArg(0),
1059                             syntax::NodeRole::UnaryOperatorExpression_operand);
1060       Builder.foldNode(Builder.getExprRange(S),
1061                        new (allocator()) syntax::PostfixUnaryOperatorExpression,
1062                        S);
1063       return true;
1064     case syntax::NodeKind::UnknownExpression:
1065       return RecursiveASTVisitor::WalkUpFromCXXOperatorCallExpr(S);
1066     default:
1067       llvm_unreachable("getOperatorNodeKind() does not return this value");
1068     }
1069   }
1070 
1071   bool WalkUpFromNamespaceDecl(NamespaceDecl *S) {
1072     auto Tokens = Builder.getDeclarationRange(S);
1073     if (Tokens.front().kind() == tok::coloncolon) {
1074       // Handle nested namespace definitions. Those start at '::' token, e.g.
1075       // namespace a^::b {}
1076       // FIXME: build corresponding nodes for the name of this namespace.
1077       return true;
1078     }
1079     Builder.foldNode(Tokens, new (allocator()) syntax::NamespaceDefinition, S);
1080     return true;
1081   }
1082 
1083   // FIXME: Deleting the `TraverseParenTypeLoc` override doesn't change test
1084   // results. Find test coverage or remove it.
1085   bool TraverseParenTypeLoc(ParenTypeLoc L) {
1086     // We reverse order of traversal to get the proper syntax structure.
1087     if (!WalkUpFromParenTypeLoc(L))
1088       return false;
1089     return TraverseTypeLoc(L.getInnerLoc());
1090   }
1091 
1092   bool WalkUpFromParenTypeLoc(ParenTypeLoc L) {
1093     Builder.markChildToken(L.getLParenLoc(), syntax::NodeRole::OpenParen);
1094     Builder.markChildToken(L.getRParenLoc(), syntax::NodeRole::CloseParen);
1095     Builder.foldNode(Builder.getRange(L.getLParenLoc(), L.getRParenLoc()),
1096                      new (allocator()) syntax::ParenDeclarator, L);
1097     return true;
1098   }
1099 
1100   // Declarator chunks, they are produced by type locs and some clang::Decls.
1101   bool WalkUpFromArrayTypeLoc(ArrayTypeLoc L) {
1102     Builder.markChildToken(L.getLBracketLoc(), syntax::NodeRole::OpenParen);
1103     Builder.markExprChild(L.getSizeExpr(),
1104                           syntax::NodeRole::ArraySubscript_sizeExpression);
1105     Builder.markChildToken(L.getRBracketLoc(), syntax::NodeRole::CloseParen);
1106     Builder.foldNode(Builder.getRange(L.getLBracketLoc(), L.getRBracketLoc()),
1107                      new (allocator()) syntax::ArraySubscript, L);
1108     return true;
1109   }
1110 
1111   bool WalkUpFromFunctionTypeLoc(FunctionTypeLoc L) {
1112     Builder.markChildToken(L.getLParenLoc(), syntax::NodeRole::OpenParen);
1113     for (auto *P : L.getParams()) {
1114       Builder.markChild(P, syntax::NodeRole::ParametersAndQualifiers_parameter);
1115     }
1116     Builder.markChildToken(L.getRParenLoc(), syntax::NodeRole::CloseParen);
1117     Builder.foldNode(Builder.getRange(L.getLParenLoc(), L.getEndLoc()),
1118                      new (allocator()) syntax::ParametersAndQualifiers, L);
1119     return true;
1120   }
1121 
1122   bool WalkUpFromFunctionProtoTypeLoc(FunctionProtoTypeLoc L) {
1123     if (!L.getTypePtr()->hasTrailingReturn())
1124       return WalkUpFromFunctionTypeLoc(L);
1125 
1126     auto *TrailingReturnTokens = BuildTrailingReturn(L);
1127     // Finish building the node for parameters.
1128     Builder.markChild(TrailingReturnTokens,
1129                       syntax::NodeRole::ParametersAndQualifiers_trailingReturn);
1130     return WalkUpFromFunctionTypeLoc(L);
1131   }
1132 
1133   bool TraverseMemberPointerTypeLoc(MemberPointerTypeLoc L) {
1134     // In the source code "void (Y::*mp)()" `MemberPointerTypeLoc` corresponds
1135     // to "Y::*" but it points to a `ParenTypeLoc` that corresponds to
1136     // "(Y::*mp)" We thus reverse the order of traversal to get the proper
1137     // syntax structure.
1138     if (!WalkUpFromMemberPointerTypeLoc(L))
1139       return false;
1140     return TraverseTypeLoc(L.getPointeeLoc());
1141   }
1142 
1143   bool WalkUpFromMemberPointerTypeLoc(MemberPointerTypeLoc L) {
1144     auto SR = L.getLocalSourceRange();
1145     Builder.foldNode(Builder.getRange(SR),
1146                      new (allocator()) syntax::MemberPointer, L);
1147     return true;
1148   }
1149 
1150   // The code below is very regular, it could even be generated with some
1151   // preprocessor magic. We merely assign roles to the corresponding children
1152   // and fold resulting nodes.
1153   bool WalkUpFromDeclStmt(DeclStmt *S) {
1154     Builder.foldNode(Builder.getStmtRange(S),
1155                      new (allocator()) syntax::DeclarationStatement, S);
1156     return true;
1157   }
1158 
1159   bool WalkUpFromNullStmt(NullStmt *S) {
1160     Builder.foldNode(Builder.getStmtRange(S),
1161                      new (allocator()) syntax::EmptyStatement, S);
1162     return true;
1163   }
1164 
1165   bool WalkUpFromSwitchStmt(SwitchStmt *S) {
1166     Builder.markChildToken(S->getSwitchLoc(),
1167                            syntax::NodeRole::IntroducerKeyword);
1168     Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1169     Builder.foldNode(Builder.getStmtRange(S),
1170                      new (allocator()) syntax::SwitchStatement, S);
1171     return true;
1172   }
1173 
1174   bool WalkUpFromCaseStmt(CaseStmt *S) {
1175     Builder.markChildToken(S->getKeywordLoc(),
1176                            syntax::NodeRole::IntroducerKeyword);
1177     Builder.markExprChild(S->getLHS(), syntax::NodeRole::CaseStatement_value);
1178     Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement);
1179     Builder.foldNode(Builder.getStmtRange(S),
1180                      new (allocator()) syntax::CaseStatement, S);
1181     return true;
1182   }
1183 
1184   bool WalkUpFromDefaultStmt(DefaultStmt *S) {
1185     Builder.markChildToken(S->getKeywordLoc(),
1186                            syntax::NodeRole::IntroducerKeyword);
1187     Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement);
1188     Builder.foldNode(Builder.getStmtRange(S),
1189                      new (allocator()) syntax::DefaultStatement, S);
1190     return true;
1191   }
1192 
1193   bool WalkUpFromIfStmt(IfStmt *S) {
1194     Builder.markChildToken(S->getIfLoc(), syntax::NodeRole::IntroducerKeyword);
1195     Builder.markStmtChild(S->getThen(),
1196                           syntax::NodeRole::IfStatement_thenStatement);
1197     Builder.markChildToken(S->getElseLoc(),
1198                            syntax::NodeRole::IfStatement_elseKeyword);
1199     Builder.markStmtChild(S->getElse(),
1200                           syntax::NodeRole::IfStatement_elseStatement);
1201     Builder.foldNode(Builder.getStmtRange(S),
1202                      new (allocator()) syntax::IfStatement, S);
1203     return true;
1204   }
1205 
1206   bool WalkUpFromForStmt(ForStmt *S) {
1207     Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword);
1208     Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1209     Builder.foldNode(Builder.getStmtRange(S),
1210                      new (allocator()) syntax::ForStatement, S);
1211     return true;
1212   }
1213 
1214   bool WalkUpFromWhileStmt(WhileStmt *S) {
1215     Builder.markChildToken(S->getWhileLoc(),
1216                            syntax::NodeRole::IntroducerKeyword);
1217     Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1218     Builder.foldNode(Builder.getStmtRange(S),
1219                      new (allocator()) syntax::WhileStatement, S);
1220     return true;
1221   }
1222 
1223   bool WalkUpFromContinueStmt(ContinueStmt *S) {
1224     Builder.markChildToken(S->getContinueLoc(),
1225                            syntax::NodeRole::IntroducerKeyword);
1226     Builder.foldNode(Builder.getStmtRange(S),
1227                      new (allocator()) syntax::ContinueStatement, S);
1228     return true;
1229   }
1230 
1231   bool WalkUpFromBreakStmt(BreakStmt *S) {
1232     Builder.markChildToken(S->getBreakLoc(),
1233                            syntax::NodeRole::IntroducerKeyword);
1234     Builder.foldNode(Builder.getStmtRange(S),
1235                      new (allocator()) syntax::BreakStatement, S);
1236     return true;
1237   }
1238 
1239   bool WalkUpFromReturnStmt(ReturnStmt *S) {
1240     Builder.markChildToken(S->getReturnLoc(),
1241                            syntax::NodeRole::IntroducerKeyword);
1242     Builder.markExprChild(S->getRetValue(),
1243                           syntax::NodeRole::ReturnStatement_value);
1244     Builder.foldNode(Builder.getStmtRange(S),
1245                      new (allocator()) syntax::ReturnStatement, S);
1246     return true;
1247   }
1248 
1249   bool WalkUpFromCXXForRangeStmt(CXXForRangeStmt *S) {
1250     Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword);
1251     Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1252     Builder.foldNode(Builder.getStmtRange(S),
1253                      new (allocator()) syntax::RangeBasedForStatement, S);
1254     return true;
1255   }
1256 
1257   bool WalkUpFromEmptyDecl(EmptyDecl *S) {
1258     Builder.foldNode(Builder.getDeclarationRange(S),
1259                      new (allocator()) syntax::EmptyDeclaration, S);
1260     return true;
1261   }
1262 
1263   bool WalkUpFromStaticAssertDecl(StaticAssertDecl *S) {
1264     Builder.markExprChild(S->getAssertExpr(),
1265                           syntax::NodeRole::StaticAssertDeclaration_condition);
1266     Builder.markExprChild(S->getMessage(),
1267                           syntax::NodeRole::StaticAssertDeclaration_message);
1268     Builder.foldNode(Builder.getDeclarationRange(S),
1269                      new (allocator()) syntax::StaticAssertDeclaration, S);
1270     return true;
1271   }
1272 
1273   bool WalkUpFromLinkageSpecDecl(LinkageSpecDecl *S) {
1274     Builder.foldNode(Builder.getDeclarationRange(S),
1275                      new (allocator()) syntax::LinkageSpecificationDeclaration,
1276                      S);
1277     return true;
1278   }
1279 
1280   bool WalkUpFromNamespaceAliasDecl(NamespaceAliasDecl *S) {
1281     Builder.foldNode(Builder.getDeclarationRange(S),
1282                      new (allocator()) syntax::NamespaceAliasDefinition, S);
1283     return true;
1284   }
1285 
1286   bool WalkUpFromUsingDirectiveDecl(UsingDirectiveDecl *S) {
1287     Builder.foldNode(Builder.getDeclarationRange(S),
1288                      new (allocator()) syntax::UsingNamespaceDirective, S);
1289     return true;
1290   }
1291 
1292   bool WalkUpFromUsingDecl(UsingDecl *S) {
1293     Builder.foldNode(Builder.getDeclarationRange(S),
1294                      new (allocator()) syntax::UsingDeclaration, S);
1295     return true;
1296   }
1297 
1298   bool WalkUpFromUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *S) {
1299     Builder.foldNode(Builder.getDeclarationRange(S),
1300                      new (allocator()) syntax::UsingDeclaration, S);
1301     return true;
1302   }
1303 
1304   bool WalkUpFromUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *S) {
1305     Builder.foldNode(Builder.getDeclarationRange(S),
1306                      new (allocator()) syntax::UsingDeclaration, S);
1307     return true;
1308   }
1309 
1310   bool WalkUpFromTypeAliasDecl(TypeAliasDecl *S) {
1311     Builder.foldNode(Builder.getDeclarationRange(S),
1312                      new (allocator()) syntax::TypeAliasDeclaration, S);
1313     return true;
1314   }
1315 
1316 private:
1317   template <class T> SourceLocation getQualifiedNameStart(T *D) {
1318     static_assert((std::is_base_of<DeclaratorDecl, T>::value ||
1319                    std::is_base_of<TypedefNameDecl, T>::value),
1320                   "only DeclaratorDecl and TypedefNameDecl are supported.");
1321 
1322     auto DN = D->getDeclName();
1323     bool IsAnonymous = DN.isIdentifier() && !DN.getAsIdentifierInfo();
1324     if (IsAnonymous)
1325       return SourceLocation();
1326 
1327     if (const auto *DD = dyn_cast<DeclaratorDecl>(D)) {
1328       if (DD->getQualifierLoc()) {
1329         return DD->getQualifierLoc().getBeginLoc();
1330       }
1331     }
1332 
1333     return D->getLocation();
1334   }
1335 
1336   SourceRange getInitializerRange(Decl *D) {
1337     if (auto *V = dyn_cast<VarDecl>(D)) {
1338       auto *I = V->getInit();
1339       // Initializers in range-based-for are not part of the declarator
1340       if (I && !V->isCXXForRangeDecl())
1341         return I->getSourceRange();
1342     }
1343 
1344     return SourceRange();
1345   }
1346 
1347   /// Folds SimpleDeclarator node (if present) and in case this is the last
1348   /// declarator in the chain it also folds SimpleDeclaration node.
1349   template <class T> bool processDeclaratorAndDeclaration(T *D) {
1350     SourceRange Initializer = getInitializerRange(D);
1351     auto Range = getDeclaratorRange(Builder.sourceManager(),
1352                                     D->getTypeSourceInfo()->getTypeLoc(),
1353                                     getQualifiedNameStart(D), Initializer);
1354 
1355     // There doesn't have to be a declarator (e.g. `void foo(int)` only has
1356     // declaration, but no declarator).
1357     if (Range.getBegin().isValid()) {
1358       auto *N = new (allocator()) syntax::SimpleDeclarator;
1359       Builder.foldNode(Builder.getRange(Range), N, nullptr);
1360       Builder.markChild(N, syntax::NodeRole::SimpleDeclaration_declarator);
1361     }
1362 
1363     if (Builder.isResponsibleForCreatingDeclaration(D)) {
1364       Builder.foldNode(Builder.getDeclarationRange(D),
1365                        new (allocator()) syntax::SimpleDeclaration, D);
1366     }
1367     return true;
1368   }
1369 
1370   /// Returns the range of the built node.
1371   syntax::TrailingReturnType *BuildTrailingReturn(FunctionProtoTypeLoc L) {
1372     assert(L.getTypePtr()->hasTrailingReturn());
1373 
1374     auto ReturnedType = L.getReturnLoc();
1375     // Build node for the declarator, if any.
1376     auto ReturnDeclaratorRange =
1377         getDeclaratorRange(this->Builder.sourceManager(), ReturnedType,
1378                            /*Name=*/SourceLocation(),
1379                            /*Initializer=*/SourceLocation());
1380     syntax::SimpleDeclarator *ReturnDeclarator = nullptr;
1381     if (ReturnDeclaratorRange.isValid()) {
1382       ReturnDeclarator = new (allocator()) syntax::SimpleDeclarator;
1383       Builder.foldNode(Builder.getRange(ReturnDeclaratorRange),
1384                        ReturnDeclarator, nullptr);
1385     }
1386 
1387     // Build node for trailing return type.
1388     auto Return = Builder.getRange(ReturnedType.getSourceRange());
1389     const auto *Arrow = Return.begin() - 1;
1390     assert(Arrow->kind() == tok::arrow);
1391     auto Tokens = llvm::makeArrayRef(Arrow, Return.end());
1392     Builder.markChildToken(Arrow, syntax::NodeRole::ArrowToken);
1393     if (ReturnDeclarator)
1394       Builder.markChild(ReturnDeclarator,
1395                         syntax::NodeRole::TrailingReturnType_declarator);
1396     auto *R = new (allocator()) syntax::TrailingReturnType;
1397     Builder.foldNode(Tokens, R, L);
1398     return R;
1399   }
1400 
1401   void foldExplicitTemplateInstantiation(
1402       ArrayRef<syntax::Token> Range, const syntax::Token *ExternKW,
1403       const syntax::Token *TemplateKW,
1404       syntax::SimpleDeclaration *InnerDeclaration, Decl *From) {
1405     assert(!ExternKW || ExternKW->kind() == tok::kw_extern);
1406     assert(TemplateKW && TemplateKW->kind() == tok::kw_template);
1407     Builder.markChildToken(ExternKW, syntax::NodeRole::ExternKeyword);
1408     Builder.markChildToken(TemplateKW, syntax::NodeRole::IntroducerKeyword);
1409     Builder.markChild(
1410         InnerDeclaration,
1411         syntax::NodeRole::ExplicitTemplateInstantiation_declaration);
1412     Builder.foldNode(
1413         Range, new (allocator()) syntax::ExplicitTemplateInstantiation, From);
1414   }
1415 
1416   syntax::TemplateDeclaration *foldTemplateDeclaration(
1417       ArrayRef<syntax::Token> Range, const syntax::Token *TemplateKW,
1418       ArrayRef<syntax::Token> TemplatedDeclaration, Decl *From) {
1419     assert(TemplateKW && TemplateKW->kind() == tok::kw_template);
1420     Builder.markChildToken(TemplateKW, syntax::NodeRole::IntroducerKeyword);
1421 
1422     auto *N = new (allocator()) syntax::TemplateDeclaration;
1423     Builder.foldNode(Range, N, From);
1424     Builder.markChild(N, syntax::NodeRole::TemplateDeclaration_declaration);
1425     return N;
1426   }
1427 
1428   /// A small helper to save some typing.
1429   llvm::BumpPtrAllocator &allocator() { return Builder.allocator(); }
1430 
1431   syntax::TreeBuilder &Builder;
1432   const ASTContext &Context;
1433 };
1434 } // namespace
1435 
1436 void syntax::TreeBuilder::noticeDeclWithoutSemicolon(Decl *D) {
1437   DeclsWithoutSemicolons.insert(D);
1438 }
1439 
1440 void syntax::TreeBuilder::markChildToken(SourceLocation Loc, NodeRole Role) {
1441   if (Loc.isInvalid())
1442     return;
1443   Pending.assignRole(*findToken(Loc), Role);
1444 }
1445 
1446 void syntax::TreeBuilder::markChildToken(const syntax::Token *T, NodeRole R) {
1447   if (!T)
1448     return;
1449   Pending.assignRole(*T, R);
1450 }
1451 
1452 void syntax::TreeBuilder::markChild(syntax::Node *N, NodeRole R) {
1453   assert(N);
1454   setRole(N, R);
1455 }
1456 
1457 void syntax::TreeBuilder::markChild(ASTPtr N, NodeRole R) {
1458   auto *SN = Mapping.find(N);
1459   assert(SN != nullptr);
1460   setRole(SN, R);
1461 }
1462 void syntax::TreeBuilder::markChild(NestedNameSpecifierLoc NNSLoc, NodeRole R) {
1463   auto *SN = Mapping.find(NNSLoc);
1464   assert(SN != nullptr);
1465   setRole(SN, R);
1466 }
1467 
1468 void syntax::TreeBuilder::markStmtChild(Stmt *Child, NodeRole Role) {
1469   if (!Child)
1470     return;
1471 
1472   syntax::Tree *ChildNode;
1473   if (Expr *ChildExpr = dyn_cast<Expr>(Child)) {
1474     // This is an expression in a statement position, consume the trailing
1475     // semicolon and form an 'ExpressionStatement' node.
1476     markExprChild(ChildExpr, NodeRole::ExpressionStatement_expression);
1477     ChildNode = new (allocator()) syntax::ExpressionStatement;
1478     // (!) 'getStmtRange()' ensures this covers a trailing semicolon.
1479     Pending.foldChildren(Arena, getStmtRange(Child), ChildNode);
1480   } else {
1481     ChildNode = Mapping.find(Child);
1482   }
1483   assert(ChildNode != nullptr);
1484   setRole(ChildNode, Role);
1485 }
1486 
1487 void syntax::TreeBuilder::markExprChild(Expr *Child, NodeRole Role) {
1488   if (!Child)
1489     return;
1490   Child = Child->IgnoreImplicit();
1491 
1492   syntax::Tree *ChildNode = Mapping.find(Child);
1493   assert(ChildNode != nullptr);
1494   setRole(ChildNode, Role);
1495 }
1496 
1497 const syntax::Token *syntax::TreeBuilder::findToken(SourceLocation L) const {
1498   if (L.isInvalid())
1499     return nullptr;
1500   auto It = LocationToToken.find(L.getRawEncoding());
1501   assert(It != LocationToToken.end());
1502   return It->second;
1503 }
1504 
1505 syntax::TranslationUnit *
1506 syntax::buildSyntaxTree(Arena &A, const TranslationUnitDecl &TU) {
1507   TreeBuilder Builder(A);
1508   BuildTreeVisitor(TU.getASTContext(), Builder).TraverseAST(TU.getASTContext());
1509   return std::move(Builder).finalize();
1510 }
1511