19b3f38f9SIlya Biryukov //===- BuildTree.cpp ------------------------------------------*- C++ -*-=====// 29b3f38f9SIlya Biryukov // 39b3f38f9SIlya Biryukov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 49b3f38f9SIlya Biryukov // See https://llvm.org/LICENSE.txt for license information. 59b3f38f9SIlya Biryukov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 69b3f38f9SIlya Biryukov // 79b3f38f9SIlya Biryukov //===----------------------------------------------------------------------===// 89b3f38f9SIlya Biryukov #include "clang/Tooling/Syntax/BuildTree.h" 97d382dcdSMarcel Hlopko #include "clang/AST/ASTFwd.h" 10e702bdb8SIlya Biryukov #include "clang/AST/Decl.h" 11e702bdb8SIlya Biryukov #include "clang/AST/DeclBase.h" 127d382dcdSMarcel Hlopko #include "clang/AST/DeclCXX.h" 137d382dcdSMarcel Hlopko #include "clang/AST/DeclarationName.h" 143785eb83SEduardo Caldas #include "clang/AST/Expr.h" 15ea8bba7eSEduardo Caldas #include "clang/AST/ExprCXX.h" 169b3f38f9SIlya Biryukov #include "clang/AST/RecursiveASTVisitor.h" 179b3f38f9SIlya Biryukov #include "clang/AST/Stmt.h" 187d382dcdSMarcel Hlopko #include "clang/AST/TypeLoc.h" 197d382dcdSMarcel Hlopko #include "clang/AST/TypeLocVisitor.h" 209b3f38f9SIlya Biryukov #include "clang/Basic/LLVM.h" 219b3f38f9SIlya Biryukov #include "clang/Basic/SourceLocation.h" 229b3f38f9SIlya Biryukov #include "clang/Basic/SourceManager.h" 2388bf9b3dSMarcel Hlopko #include "clang/Basic/Specifiers.h" 249b3f38f9SIlya Biryukov #include "clang/Basic/TokenKinds.h" 259b3f38f9SIlya Biryukov #include "clang/Lex/Lexer.h" 261db5b348SEduardo Caldas #include "clang/Lex/LiteralSupport.h" 279b3f38f9SIlya Biryukov #include "clang/Tooling/Syntax/Nodes.h" 289b3f38f9SIlya Biryukov #include "clang/Tooling/Syntax/Tokens.h" 299b3f38f9SIlya Biryukov #include "clang/Tooling/Syntax/Tree.h" 309b3f38f9SIlya Biryukov #include "llvm/ADT/ArrayRef.h" 31a711a3a4SMarcel Hlopko #include "llvm/ADT/DenseMap.h" 32a711a3a4SMarcel Hlopko #include "llvm/ADT/PointerUnion.h" 339b3f38f9SIlya Biryukov #include "llvm/ADT/STLExtras.h" 347d382dcdSMarcel Hlopko #include "llvm/ADT/ScopeExit.h" 359b3f38f9SIlya Biryukov #include "llvm/ADT/SmallVector.h" 369b3f38f9SIlya Biryukov #include "llvm/Support/Allocator.h" 379b3f38f9SIlya Biryukov #include "llvm/Support/Casting.h" 3896065cf7SIlya Biryukov #include "llvm/Support/Compiler.h" 399b3f38f9SIlya Biryukov #include "llvm/Support/FormatVariadic.h" 401ad15046SIlya Biryukov #include "llvm/Support/MemoryBuffer.h" 419b3f38f9SIlya Biryukov #include "llvm/Support/raw_ostream.h" 42a711a3a4SMarcel Hlopko #include <cstddef> 439b3f38f9SIlya Biryukov #include <map> 449b3f38f9SIlya Biryukov 459b3f38f9SIlya Biryukov using namespace clang; 469b3f38f9SIlya Biryukov 4796065cf7SIlya Biryukov LLVM_ATTRIBUTE_UNUSED 48ba41a0f7SEduardo Caldas static bool isImplicitExpr(Expr *E) { return E->IgnoreImplicit() != E; } 4958fa50f4SIlya Biryukov 507d382dcdSMarcel Hlopko namespace { 517d382dcdSMarcel Hlopko /// Get start location of the Declarator from the TypeLoc. 527d382dcdSMarcel Hlopko /// E.g.: 537d382dcdSMarcel Hlopko /// loc of `(` in `int (a)` 547d382dcdSMarcel Hlopko /// loc of `*` in `int *(a)` 557d382dcdSMarcel Hlopko /// loc of the first `(` in `int (*a)(int)` 567d382dcdSMarcel Hlopko /// loc of the `*` in `int *(a)(int)` 577d382dcdSMarcel Hlopko /// loc of the first `*` in `const int *const *volatile a;` 587d382dcdSMarcel Hlopko /// 597d382dcdSMarcel Hlopko /// It is non-trivial to get the start location because TypeLocs are stored 607d382dcdSMarcel Hlopko /// inside out. In the example above `*volatile` is the TypeLoc returned 617d382dcdSMarcel Hlopko /// by `Decl.getTypeSourceInfo()`, and `*const` is what `.getPointeeLoc()` 627d382dcdSMarcel Hlopko /// returns. 637d382dcdSMarcel Hlopko struct GetStartLoc : TypeLocVisitor<GetStartLoc, SourceLocation> { 647d382dcdSMarcel Hlopko SourceLocation VisitParenTypeLoc(ParenTypeLoc T) { 657d382dcdSMarcel Hlopko auto L = Visit(T.getInnerLoc()); 667d382dcdSMarcel Hlopko if (L.isValid()) 677d382dcdSMarcel Hlopko return L; 687d382dcdSMarcel Hlopko return T.getLParenLoc(); 697d382dcdSMarcel Hlopko } 707d382dcdSMarcel Hlopko 717d382dcdSMarcel Hlopko // Types spelled in the prefix part of the declarator. 727d382dcdSMarcel Hlopko SourceLocation VisitPointerTypeLoc(PointerTypeLoc T) { 737d382dcdSMarcel Hlopko return HandlePointer(T); 747d382dcdSMarcel Hlopko } 757d382dcdSMarcel Hlopko 767d382dcdSMarcel Hlopko SourceLocation VisitMemberPointerTypeLoc(MemberPointerTypeLoc T) { 777d382dcdSMarcel Hlopko return HandlePointer(T); 787d382dcdSMarcel Hlopko } 797d382dcdSMarcel Hlopko 807d382dcdSMarcel Hlopko SourceLocation VisitBlockPointerTypeLoc(BlockPointerTypeLoc T) { 817d382dcdSMarcel Hlopko return HandlePointer(T); 827d382dcdSMarcel Hlopko } 837d382dcdSMarcel Hlopko 847d382dcdSMarcel Hlopko SourceLocation VisitReferenceTypeLoc(ReferenceTypeLoc T) { 857d382dcdSMarcel Hlopko return HandlePointer(T); 867d382dcdSMarcel Hlopko } 877d382dcdSMarcel Hlopko 887d382dcdSMarcel Hlopko SourceLocation VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc T) { 897d382dcdSMarcel Hlopko return HandlePointer(T); 907d382dcdSMarcel Hlopko } 917d382dcdSMarcel Hlopko 927d382dcdSMarcel Hlopko // All other cases are not important, as they are either part of declaration 937d382dcdSMarcel Hlopko // specifiers (e.g. inheritors of TypeSpecTypeLoc) or introduce modifiers on 947d382dcdSMarcel Hlopko // existing declarators (e.g. QualifiedTypeLoc). They cannot start the 957d382dcdSMarcel Hlopko // declarator themselves, but their underlying type can. 967d382dcdSMarcel Hlopko SourceLocation VisitTypeLoc(TypeLoc T) { 977d382dcdSMarcel Hlopko auto N = T.getNextTypeLoc(); 987d382dcdSMarcel Hlopko if (!N) 997d382dcdSMarcel Hlopko return SourceLocation(); 1007d382dcdSMarcel Hlopko return Visit(N); 1017d382dcdSMarcel Hlopko } 1027d382dcdSMarcel Hlopko 1037d382dcdSMarcel Hlopko SourceLocation VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc T) { 1047d382dcdSMarcel Hlopko if (T.getTypePtr()->hasTrailingReturn()) 1057d382dcdSMarcel Hlopko return SourceLocation(); // avoid recursing into the suffix of declarator. 1067d382dcdSMarcel Hlopko return VisitTypeLoc(T); 1077d382dcdSMarcel Hlopko } 1087d382dcdSMarcel Hlopko 1097d382dcdSMarcel Hlopko private: 1107d382dcdSMarcel Hlopko template <class PtrLoc> SourceLocation HandlePointer(PtrLoc T) { 1117d382dcdSMarcel Hlopko auto L = Visit(T.getPointeeLoc()); 1127d382dcdSMarcel Hlopko if (L.isValid()) 1137d382dcdSMarcel Hlopko return L; 1147d382dcdSMarcel Hlopko return T.getLocalSourceRange().getBegin(); 1157d382dcdSMarcel Hlopko } 1167d382dcdSMarcel Hlopko }; 1177d382dcdSMarcel Hlopko } // namespace 1187d382dcdSMarcel Hlopko 119ea8bba7eSEduardo Caldas static syntax::NodeKind getOperatorNodeKind(const CXXOperatorCallExpr &E) { 120ea8bba7eSEduardo Caldas switch (E.getOperator()) { 121ea8bba7eSEduardo Caldas // Comparison 122ea8bba7eSEduardo Caldas case OO_EqualEqual: 123ea8bba7eSEduardo Caldas case OO_ExclaimEqual: 124ea8bba7eSEduardo Caldas case OO_Greater: 125ea8bba7eSEduardo Caldas case OO_GreaterEqual: 126ea8bba7eSEduardo Caldas case OO_Less: 127ea8bba7eSEduardo Caldas case OO_LessEqual: 128ea8bba7eSEduardo Caldas case OO_Spaceship: 129ea8bba7eSEduardo Caldas // Assignment 130ea8bba7eSEduardo Caldas case OO_Equal: 131ea8bba7eSEduardo Caldas case OO_SlashEqual: 132ea8bba7eSEduardo Caldas case OO_PercentEqual: 133ea8bba7eSEduardo Caldas case OO_CaretEqual: 134ea8bba7eSEduardo Caldas case OO_PipeEqual: 135ea8bba7eSEduardo Caldas case OO_LessLessEqual: 136ea8bba7eSEduardo Caldas case OO_GreaterGreaterEqual: 137ea8bba7eSEduardo Caldas case OO_PlusEqual: 138ea8bba7eSEduardo Caldas case OO_MinusEqual: 139ea8bba7eSEduardo Caldas case OO_StarEqual: 140ea8bba7eSEduardo Caldas case OO_AmpEqual: 141ea8bba7eSEduardo Caldas // Binary computation 142ea8bba7eSEduardo Caldas case OO_Slash: 143ea8bba7eSEduardo Caldas case OO_Percent: 144ea8bba7eSEduardo Caldas case OO_Caret: 145ea8bba7eSEduardo Caldas case OO_Pipe: 146ea8bba7eSEduardo Caldas case OO_LessLess: 147ea8bba7eSEduardo Caldas case OO_GreaterGreater: 148ea8bba7eSEduardo Caldas case OO_AmpAmp: 149ea8bba7eSEduardo Caldas case OO_PipePipe: 150ea8bba7eSEduardo Caldas case OO_ArrowStar: 151ea8bba7eSEduardo Caldas case OO_Comma: 152ea8bba7eSEduardo Caldas return syntax::NodeKind::BinaryOperatorExpression; 153ea8bba7eSEduardo Caldas case OO_Tilde: 154ea8bba7eSEduardo Caldas case OO_Exclaim: 155ea8bba7eSEduardo Caldas return syntax::NodeKind::PrefixUnaryOperatorExpression; 156ea8bba7eSEduardo Caldas // Prefix/Postfix increment/decrement 157ea8bba7eSEduardo Caldas case OO_PlusPlus: 158ea8bba7eSEduardo Caldas case OO_MinusMinus: 159ea8bba7eSEduardo Caldas switch (E.getNumArgs()) { 160ea8bba7eSEduardo Caldas case 1: 161ea8bba7eSEduardo Caldas return syntax::NodeKind::PrefixUnaryOperatorExpression; 162ea8bba7eSEduardo Caldas case 2: 163ea8bba7eSEduardo Caldas return syntax::NodeKind::PostfixUnaryOperatorExpression; 164ea8bba7eSEduardo Caldas default: 165ea8bba7eSEduardo Caldas llvm_unreachable("Invalid number of arguments for operator"); 166ea8bba7eSEduardo Caldas } 167ea8bba7eSEduardo Caldas // Operators that can be unary or binary 168ea8bba7eSEduardo Caldas case OO_Plus: 169ea8bba7eSEduardo Caldas case OO_Minus: 170ea8bba7eSEduardo Caldas case OO_Star: 171ea8bba7eSEduardo Caldas case OO_Amp: 172ea8bba7eSEduardo Caldas switch (E.getNumArgs()) { 173ea8bba7eSEduardo Caldas case 1: 174ea8bba7eSEduardo Caldas return syntax::NodeKind::PrefixUnaryOperatorExpression; 175ea8bba7eSEduardo Caldas case 2: 176ea8bba7eSEduardo Caldas return syntax::NodeKind::BinaryOperatorExpression; 177ea8bba7eSEduardo Caldas default: 178ea8bba7eSEduardo Caldas llvm_unreachable("Invalid number of arguments for operator"); 179ea8bba7eSEduardo Caldas } 180ea8bba7eSEduardo Caldas return syntax::NodeKind::BinaryOperatorExpression; 181ea8bba7eSEduardo Caldas // Not yet supported by SyntaxTree 182ea8bba7eSEduardo Caldas case OO_New: 183ea8bba7eSEduardo Caldas case OO_Delete: 184ea8bba7eSEduardo Caldas case OO_Array_New: 185ea8bba7eSEduardo Caldas case OO_Array_Delete: 186ea8bba7eSEduardo Caldas case OO_Coawait: 187ea8bba7eSEduardo Caldas case OO_Subscript: 188ea8bba7eSEduardo Caldas case OO_Arrow: 189ea8bba7eSEduardo Caldas return syntax::NodeKind::UnknownExpression; 190*2de2ca34SEduardo Caldas case OO_Call: 191*2de2ca34SEduardo Caldas return syntax::NodeKind::CallExpression; 192ea8bba7eSEduardo Caldas case OO_Conditional: // not overloadable 193ea8bba7eSEduardo Caldas case NUM_OVERLOADED_OPERATORS: 194ea8bba7eSEduardo Caldas case OO_None: 195ea8bba7eSEduardo Caldas llvm_unreachable("Not an overloadable operator"); 196ea8bba7eSEduardo Caldas } 197397c6820SSimon Pilgrim llvm_unreachable("Unknown OverloadedOperatorKind enum"); 198ea8bba7eSEduardo Caldas } 199ea8bba7eSEduardo Caldas 2007d382dcdSMarcel Hlopko /// Gets the range of declarator as defined by the C++ grammar. E.g. 2017d382dcdSMarcel Hlopko /// `int a;` -> range of `a`, 2027d382dcdSMarcel Hlopko /// `int *a;` -> range of `*a`, 2037d382dcdSMarcel Hlopko /// `int a[10];` -> range of `a[10]`, 2047d382dcdSMarcel Hlopko /// `int a[1][2][3];` -> range of `a[1][2][3]`, 2057d382dcdSMarcel Hlopko /// `int *a = nullptr` -> range of `*a = nullptr`. 2067d382dcdSMarcel Hlopko /// FIMXE: \p Name must be a source range, e.g. for `operator+`. 2077d382dcdSMarcel Hlopko static SourceRange getDeclaratorRange(const SourceManager &SM, TypeLoc T, 2087d382dcdSMarcel Hlopko SourceLocation Name, 2097d382dcdSMarcel Hlopko SourceRange Initializer) { 2107d382dcdSMarcel Hlopko SourceLocation Start = GetStartLoc().Visit(T); 2117d382dcdSMarcel Hlopko SourceLocation End = T.getSourceRange().getEnd(); 2127d382dcdSMarcel Hlopko assert(End.isValid()); 2137d382dcdSMarcel Hlopko if (Name.isValid()) { 2147d382dcdSMarcel Hlopko if (Start.isInvalid()) 2157d382dcdSMarcel Hlopko Start = Name; 2167d382dcdSMarcel Hlopko if (SM.isBeforeInTranslationUnit(End, Name)) 2177d382dcdSMarcel Hlopko End = Name; 2187d382dcdSMarcel Hlopko } 2197d382dcdSMarcel Hlopko if (Initializer.isValid()) { 220cdce2fe5SMarcel Hlopko auto InitializerEnd = Initializer.getEnd(); 221f33c2c27SEduardo Caldas assert(SM.isBeforeInTranslationUnit(End, InitializerEnd) || 222f33c2c27SEduardo Caldas End == InitializerEnd); 223cdce2fe5SMarcel Hlopko End = InitializerEnd; 2247d382dcdSMarcel Hlopko } 2257d382dcdSMarcel Hlopko return SourceRange(Start, End); 2267d382dcdSMarcel Hlopko } 2277d382dcdSMarcel Hlopko 228a711a3a4SMarcel Hlopko namespace { 229a711a3a4SMarcel Hlopko /// All AST hierarchy roots that can be represented as pointers. 230a711a3a4SMarcel Hlopko using ASTPtr = llvm::PointerUnion<Stmt *, Decl *>; 231a711a3a4SMarcel Hlopko /// Maintains a mapping from AST to syntax tree nodes. This class will get more 232a711a3a4SMarcel Hlopko /// complicated as we support more kinds of AST nodes, e.g. TypeLocs. 233a711a3a4SMarcel Hlopko /// FIXME: expose this as public API. 234a711a3a4SMarcel Hlopko class ASTToSyntaxMapping { 235a711a3a4SMarcel Hlopko public: 236a711a3a4SMarcel Hlopko void add(ASTPtr From, syntax::Tree *To) { 237a711a3a4SMarcel Hlopko assert(To != nullptr); 238a711a3a4SMarcel Hlopko assert(!From.isNull()); 239a711a3a4SMarcel Hlopko 240a711a3a4SMarcel Hlopko bool Added = Nodes.insert({From, To}).second; 241a711a3a4SMarcel Hlopko (void)Added; 242a711a3a4SMarcel Hlopko assert(Added && "mapping added twice"); 243a711a3a4SMarcel Hlopko } 244a711a3a4SMarcel Hlopko 245f9500cc4SEduardo Caldas void add(NestedNameSpecifierLoc From, syntax::Tree *To) { 246f9500cc4SEduardo Caldas assert(To != nullptr); 247f9500cc4SEduardo Caldas assert(From.hasQualifier()); 248f9500cc4SEduardo Caldas 249f9500cc4SEduardo Caldas bool Added = NNSNodes.insert({From, To}).second; 250f9500cc4SEduardo Caldas (void)Added; 251f9500cc4SEduardo Caldas assert(Added && "mapping added twice"); 252f9500cc4SEduardo Caldas } 253f9500cc4SEduardo Caldas 254a711a3a4SMarcel Hlopko syntax::Tree *find(ASTPtr P) const { return Nodes.lookup(P); } 255a711a3a4SMarcel Hlopko 256f9500cc4SEduardo Caldas syntax::Tree *find(NestedNameSpecifierLoc P) const { 257f9500cc4SEduardo Caldas return NNSNodes.lookup(P); 258f9500cc4SEduardo Caldas } 259f9500cc4SEduardo Caldas 260a711a3a4SMarcel Hlopko private: 261a711a3a4SMarcel Hlopko llvm::DenseMap<ASTPtr, syntax::Tree *> Nodes; 262f9500cc4SEduardo Caldas llvm::DenseMap<NestedNameSpecifierLoc, syntax::Tree *> NNSNodes; 263a711a3a4SMarcel Hlopko }; 264a711a3a4SMarcel Hlopko } // namespace 265a711a3a4SMarcel Hlopko 2669b3f38f9SIlya Biryukov /// A helper class for constructing the syntax tree while traversing a clang 2679b3f38f9SIlya Biryukov /// AST. 2689b3f38f9SIlya Biryukov /// 2699b3f38f9SIlya Biryukov /// At each point of the traversal we maintain a list of pending nodes. 2709b3f38f9SIlya Biryukov /// Initially all tokens are added as pending nodes. When processing a clang AST 2719b3f38f9SIlya Biryukov /// node, the clients need to: 2729b3f38f9SIlya Biryukov /// - create a corresponding syntax node, 2739b3f38f9SIlya Biryukov /// - assign roles to all pending child nodes with 'markChild' and 2749b3f38f9SIlya Biryukov /// 'markChildToken', 2759b3f38f9SIlya Biryukov /// - replace the child nodes with the new syntax node in the pending list 2769b3f38f9SIlya Biryukov /// with 'foldNode'. 2779b3f38f9SIlya Biryukov /// 2789b3f38f9SIlya Biryukov /// Note that all children are expected to be processed when building a node. 2799b3f38f9SIlya Biryukov /// 2809b3f38f9SIlya Biryukov /// Call finalize() to finish building the tree and consume the root node. 2819b3f38f9SIlya Biryukov class syntax::TreeBuilder { 2829b3f38f9SIlya Biryukov public: 283c1bbefefSIlya Biryukov TreeBuilder(syntax::Arena &Arena) : Arena(Arena), Pending(Arena) { 284c1bbefefSIlya Biryukov for (const auto &T : Arena.tokenBuffer().expandedTokens()) 285c1bbefefSIlya Biryukov LocationToToken.insert({T.location().getRawEncoding(), &T}); 286c1bbefefSIlya Biryukov } 2879b3f38f9SIlya Biryukov 2889b3f38f9SIlya Biryukov llvm::BumpPtrAllocator &allocator() { return Arena.allocator(); } 2897d382dcdSMarcel Hlopko const SourceManager &sourceManager() const { return Arena.sourceManager(); } 2909b3f38f9SIlya Biryukov 2919b3f38f9SIlya Biryukov /// Populate children for \p New node, assuming it covers tokens from \p 2929b3f38f9SIlya Biryukov /// Range. 293ba41a0f7SEduardo Caldas void foldNode(ArrayRef<syntax::Token> Range, syntax::Tree *New, ASTPtr From) { 294a711a3a4SMarcel Hlopko assert(New); 295a711a3a4SMarcel Hlopko Pending.foldChildren(Arena, Range, New); 296a711a3a4SMarcel Hlopko if (From) 297a711a3a4SMarcel Hlopko Mapping.add(From, New); 298a711a3a4SMarcel Hlopko } 299f9500cc4SEduardo Caldas 300ba41a0f7SEduardo Caldas void foldNode(ArrayRef<syntax::Token> Range, syntax::Tree *New, TypeLoc L) { 301a711a3a4SMarcel Hlopko // FIXME: add mapping for TypeLocs 302a711a3a4SMarcel Hlopko foldNode(Range, New, nullptr); 303a711a3a4SMarcel Hlopko } 3049b3f38f9SIlya Biryukov 305f9500cc4SEduardo Caldas void foldNode(llvm::ArrayRef<syntax::Token> Range, syntax::Tree *New, 306f9500cc4SEduardo Caldas NestedNameSpecifierLoc From) { 307f9500cc4SEduardo Caldas assert(New); 308f9500cc4SEduardo Caldas Pending.foldChildren(Arena, Range, New); 309f9500cc4SEduardo Caldas if (From) 310f9500cc4SEduardo Caldas Mapping.add(From, New); 3118abb5fb6SEduardo Caldas } 312f9500cc4SEduardo Caldas 313e702bdb8SIlya Biryukov /// Notifies that we should not consume trailing semicolon when computing 314e702bdb8SIlya Biryukov /// token range of \p D. 3157d382dcdSMarcel Hlopko void noticeDeclWithoutSemicolon(Decl *D); 316e702bdb8SIlya Biryukov 31758fa50f4SIlya Biryukov /// Mark the \p Child node with a corresponding \p Role. All marked children 31858fa50f4SIlya Biryukov /// should be consumed by foldNode. 3197d382dcdSMarcel Hlopko /// When called on expressions (clang::Expr is derived from clang::Stmt), 32058fa50f4SIlya Biryukov /// wraps expressions into expression statement. 32158fa50f4SIlya Biryukov void markStmtChild(Stmt *Child, NodeRole Role); 32258fa50f4SIlya Biryukov /// Should be called for expressions in non-statement position to avoid 32358fa50f4SIlya Biryukov /// wrapping into expression statement. 32458fa50f4SIlya Biryukov void markExprChild(Expr *Child, NodeRole Role); 3259b3f38f9SIlya Biryukov /// Set role for a token starting at \p Loc. 326def65bb4SIlya Biryukov void markChildToken(SourceLocation Loc, NodeRole R); 3277d382dcdSMarcel Hlopko /// Set role for \p T. 3287d382dcdSMarcel Hlopko void markChildToken(const syntax::Token *T, NodeRole R); 3297d382dcdSMarcel Hlopko 330a711a3a4SMarcel Hlopko /// Set role for \p N. 331a711a3a4SMarcel Hlopko void markChild(syntax::Node *N, NodeRole R); 332a711a3a4SMarcel Hlopko /// Set role for the syntax node matching \p N. 333a711a3a4SMarcel Hlopko void markChild(ASTPtr N, NodeRole R); 334f9500cc4SEduardo Caldas /// Set role for the syntax node matching \p N. 335f9500cc4SEduardo Caldas void markChild(NestedNameSpecifierLoc N, NodeRole R); 3369b3f38f9SIlya Biryukov 3379b3f38f9SIlya Biryukov /// Finish building the tree and consume the root node. 3389b3f38f9SIlya Biryukov syntax::TranslationUnit *finalize() && { 3399b3f38f9SIlya Biryukov auto Tokens = Arena.tokenBuffer().expandedTokens(); 340bfbf6b6cSIlya Biryukov assert(!Tokens.empty()); 341bfbf6b6cSIlya Biryukov assert(Tokens.back().kind() == tok::eof); 342bfbf6b6cSIlya Biryukov 3439b3f38f9SIlya Biryukov // Build the root of the tree, consuming all the children. 3441ad15046SIlya Biryukov Pending.foldChildren(Arena, Tokens.drop_back(), 3459b3f38f9SIlya Biryukov new (Arena.allocator()) syntax::TranslationUnit); 3469b3f38f9SIlya Biryukov 3473b929fe7SIlya Biryukov auto *TU = cast<syntax::TranslationUnit>(std::move(Pending).finalize()); 3483b929fe7SIlya Biryukov TU->assertInvariantsRecursive(); 3493b929fe7SIlya Biryukov return TU; 3509b3f38f9SIlya Biryukov } 3519b3f38f9SIlya Biryukov 35288bf9b3dSMarcel Hlopko /// Finds a token starting at \p L. The token must exist if \p L is valid. 35388bf9b3dSMarcel Hlopko const syntax::Token *findToken(SourceLocation L) const; 35488bf9b3dSMarcel Hlopko 355a711a3a4SMarcel Hlopko /// Finds the syntax tokens corresponding to the \p SourceRange. 356ba41a0f7SEduardo Caldas ArrayRef<syntax::Token> getRange(SourceRange Range) const { 357a711a3a4SMarcel Hlopko assert(Range.isValid()); 358a711a3a4SMarcel Hlopko return getRange(Range.getBegin(), Range.getEnd()); 359a711a3a4SMarcel Hlopko } 360a711a3a4SMarcel Hlopko 361a711a3a4SMarcel Hlopko /// Finds the syntax tokens corresponding to the passed source locations. 3629b3f38f9SIlya Biryukov /// \p First is the start position of the first token and \p Last is the start 3639b3f38f9SIlya Biryukov /// position of the last token. 364ba41a0f7SEduardo Caldas ArrayRef<syntax::Token> getRange(SourceLocation First, 3659b3f38f9SIlya Biryukov SourceLocation Last) const { 3669b3f38f9SIlya Biryukov assert(First.isValid()); 3679b3f38f9SIlya Biryukov assert(Last.isValid()); 3689b3f38f9SIlya Biryukov assert(First == Last || 3699b3f38f9SIlya Biryukov Arena.sourceManager().isBeforeInTranslationUnit(First, Last)); 3709b3f38f9SIlya Biryukov return llvm::makeArrayRef(findToken(First), std::next(findToken(Last))); 3719b3f38f9SIlya Biryukov } 37288bf9b3dSMarcel Hlopko 373ba41a0f7SEduardo Caldas ArrayRef<syntax::Token> 37488bf9b3dSMarcel Hlopko getTemplateRange(const ClassTemplateSpecializationDecl *D) const { 375a711a3a4SMarcel Hlopko auto Tokens = getRange(D->getSourceRange()); 37688bf9b3dSMarcel Hlopko return maybeAppendSemicolon(Tokens, D); 37788bf9b3dSMarcel Hlopko } 37888bf9b3dSMarcel Hlopko 379cdce2fe5SMarcel Hlopko /// Returns true if \p D is the last declarator in a chain and is thus 380cdce2fe5SMarcel Hlopko /// reponsible for creating SimpleDeclaration for the whole chain. 381cdce2fe5SMarcel Hlopko template <class T> 382cdce2fe5SMarcel Hlopko bool isResponsibleForCreatingDeclaration(const T *D) const { 383cdce2fe5SMarcel Hlopko static_assert((std::is_base_of<DeclaratorDecl, T>::value || 384cdce2fe5SMarcel Hlopko std::is_base_of<TypedefNameDecl, T>::value), 385cdce2fe5SMarcel Hlopko "only DeclaratorDecl and TypedefNameDecl are supported."); 386cdce2fe5SMarcel Hlopko 387cdce2fe5SMarcel Hlopko const Decl *Next = D->getNextDeclInContext(); 388cdce2fe5SMarcel Hlopko 389cdce2fe5SMarcel Hlopko // There's no next sibling, this one is responsible. 390cdce2fe5SMarcel Hlopko if (Next == nullptr) { 391cdce2fe5SMarcel Hlopko return true; 392cdce2fe5SMarcel Hlopko } 393ba41a0f7SEduardo Caldas const auto *NextT = dyn_cast<T>(Next); 394cdce2fe5SMarcel Hlopko 395cdce2fe5SMarcel Hlopko // Next sibling is not the same type, this one is responsible. 396cdce2fe5SMarcel Hlopko if (NextT == nullptr) { 397cdce2fe5SMarcel Hlopko return true; 398cdce2fe5SMarcel Hlopko } 399cdce2fe5SMarcel Hlopko // Next sibling doesn't begin at the same loc, it must be a different 400cdce2fe5SMarcel Hlopko // declaration, so this declarator is responsible. 401cdce2fe5SMarcel Hlopko if (NextT->getBeginLoc() != D->getBeginLoc()) { 402cdce2fe5SMarcel Hlopko return true; 403cdce2fe5SMarcel Hlopko } 404cdce2fe5SMarcel Hlopko 405cdce2fe5SMarcel Hlopko // NextT is a member of the same declaration, and we need the last member to 406cdce2fe5SMarcel Hlopko // create declaration. This one is not responsible. 407cdce2fe5SMarcel Hlopko return false; 408cdce2fe5SMarcel Hlopko } 409cdce2fe5SMarcel Hlopko 410ba41a0f7SEduardo Caldas ArrayRef<syntax::Token> getDeclarationRange(Decl *D) { 411ba41a0f7SEduardo Caldas ArrayRef<syntax::Token> Tokens; 41288bf9b3dSMarcel Hlopko // We want to drop the template parameters for specializations. 413ba41a0f7SEduardo Caldas if (const auto *S = dyn_cast<TagDecl>(D)) 41488bf9b3dSMarcel Hlopko Tokens = getRange(S->TypeDecl::getBeginLoc(), S->getEndLoc()); 41588bf9b3dSMarcel Hlopko else 416a711a3a4SMarcel Hlopko Tokens = getRange(D->getSourceRange()); 41788bf9b3dSMarcel Hlopko return maybeAppendSemicolon(Tokens, D); 4189b3f38f9SIlya Biryukov } 419cdce2fe5SMarcel Hlopko 420ba41a0f7SEduardo Caldas ArrayRef<syntax::Token> getExprRange(const Expr *E) const { 421a711a3a4SMarcel Hlopko return getRange(E->getSourceRange()); 42258fa50f4SIlya Biryukov } 423cdce2fe5SMarcel Hlopko 42458fa50f4SIlya Biryukov /// Find the adjusted range for the statement, consuming the trailing 42558fa50f4SIlya Biryukov /// semicolon when needed. 426ba41a0f7SEduardo Caldas ArrayRef<syntax::Token> getStmtRange(const Stmt *S) const { 427a711a3a4SMarcel Hlopko auto Tokens = getRange(S->getSourceRange()); 42858fa50f4SIlya Biryukov if (isa<CompoundStmt>(S)) 42958fa50f4SIlya Biryukov return Tokens; 43058fa50f4SIlya Biryukov 43158fa50f4SIlya Biryukov // Some statements miss a trailing semicolon, e.g. 'return', 'continue' and 43258fa50f4SIlya Biryukov // all statements that end with those. Consume this semicolon here. 433e702bdb8SIlya Biryukov if (Tokens.back().kind() == tok::semi) 434e702bdb8SIlya Biryukov return Tokens; 435e702bdb8SIlya Biryukov return withTrailingSemicolon(Tokens); 436e702bdb8SIlya Biryukov } 437e702bdb8SIlya Biryukov 438e702bdb8SIlya Biryukov private: 439ba41a0f7SEduardo Caldas ArrayRef<syntax::Token> maybeAppendSemicolon(ArrayRef<syntax::Token> Tokens, 44088bf9b3dSMarcel Hlopko const Decl *D) const { 441ba41a0f7SEduardo Caldas if (isa<NamespaceDecl>(D)) 44288bf9b3dSMarcel Hlopko return Tokens; 44388bf9b3dSMarcel Hlopko if (DeclsWithoutSemicolons.count(D)) 44488bf9b3dSMarcel Hlopko return Tokens; 44588bf9b3dSMarcel Hlopko // FIXME: do not consume trailing semicolon on function definitions. 44688bf9b3dSMarcel Hlopko // Most declarations own a semicolon in syntax trees, but not in clang AST. 44788bf9b3dSMarcel Hlopko return withTrailingSemicolon(Tokens); 44888bf9b3dSMarcel Hlopko } 44988bf9b3dSMarcel Hlopko 450ba41a0f7SEduardo Caldas ArrayRef<syntax::Token> 451ba41a0f7SEduardo Caldas withTrailingSemicolon(ArrayRef<syntax::Token> Tokens) const { 452e702bdb8SIlya Biryukov assert(!Tokens.empty()); 453e702bdb8SIlya Biryukov assert(Tokens.back().kind() != tok::eof); 4547d382dcdSMarcel Hlopko // We never consume 'eof', so looking at the next token is ok. 45558fa50f4SIlya Biryukov if (Tokens.back().kind() != tok::semi && Tokens.end()->kind() == tok::semi) 45658fa50f4SIlya Biryukov return llvm::makeArrayRef(Tokens.begin(), Tokens.end() + 1); 45758fa50f4SIlya Biryukov return Tokens; 4589b3f38f9SIlya Biryukov } 4599b3f38f9SIlya Biryukov 460a711a3a4SMarcel Hlopko void setRole(syntax::Node *N, NodeRole R) { 461a711a3a4SMarcel Hlopko assert(N->role() == NodeRole::Detached); 462a711a3a4SMarcel Hlopko N->setRole(R); 463a711a3a4SMarcel Hlopko } 464a711a3a4SMarcel Hlopko 4659b3f38f9SIlya Biryukov /// A collection of trees covering the input tokens. 4669b3f38f9SIlya Biryukov /// When created, each tree corresponds to a single token in the file. 4679b3f38f9SIlya Biryukov /// Clients call 'foldChildren' to attach one or more subtrees to a parent 4689b3f38f9SIlya Biryukov /// node and update the list of trees accordingly. 4699b3f38f9SIlya Biryukov /// 4709b3f38f9SIlya Biryukov /// Ensures that added nodes properly nest and cover the whole token stream. 4719b3f38f9SIlya Biryukov struct Forest { 4729b3f38f9SIlya Biryukov Forest(syntax::Arena &A) { 473bfbf6b6cSIlya Biryukov assert(!A.tokenBuffer().expandedTokens().empty()); 474bfbf6b6cSIlya Biryukov assert(A.tokenBuffer().expandedTokens().back().kind() == tok::eof); 4759b3f38f9SIlya Biryukov // Create all leaf nodes. 476bfbf6b6cSIlya Biryukov // Note that we do not have 'eof' in the tree. 4771ad15046SIlya Biryukov for (auto &T : A.tokenBuffer().expandedTokens().drop_back()) { 4781ad15046SIlya Biryukov auto *L = new (A.allocator()) syntax::Leaf(&T); 4791ad15046SIlya Biryukov L->Original = true; 4801ad15046SIlya Biryukov L->CanModify = A.tokenBuffer().spelledForExpanded(T).hasValue(); 481a711a3a4SMarcel Hlopko Trees.insert(Trees.end(), {&T, L}); 4821ad15046SIlya Biryukov } 4839b3f38f9SIlya Biryukov } 4849b3f38f9SIlya Biryukov 485ba41a0f7SEduardo Caldas void assignRole(ArrayRef<syntax::Token> Range, syntax::NodeRole Role) { 4869b3f38f9SIlya Biryukov assert(!Range.empty()); 4879b3f38f9SIlya Biryukov auto It = Trees.lower_bound(Range.begin()); 4889b3f38f9SIlya Biryukov assert(It != Trees.end() && "no node found"); 4899b3f38f9SIlya Biryukov assert(It->first == Range.begin() && "no child with the specified range"); 4909b3f38f9SIlya Biryukov assert((std::next(It) == Trees.end() || 4919b3f38f9SIlya Biryukov std::next(It)->first == Range.end()) && 4929b3f38f9SIlya Biryukov "no child with the specified range"); 493a711a3a4SMarcel Hlopko assert(It->second->role() == NodeRole::Detached && 494a711a3a4SMarcel Hlopko "re-assigning role for a child"); 495a711a3a4SMarcel Hlopko It->second->setRole(Role); 4969b3f38f9SIlya Biryukov } 4979b3f38f9SIlya Biryukov 498e702bdb8SIlya Biryukov /// Add \p Node to the forest and attach child nodes based on \p Tokens. 499ba41a0f7SEduardo Caldas void foldChildren(const syntax::Arena &A, ArrayRef<syntax::Token> Tokens, 5009b3f38f9SIlya Biryukov syntax::Tree *Node) { 501e702bdb8SIlya Biryukov // Attach children to `Node`. 502cdce2fe5SMarcel Hlopko assert(Node->firstChild() == nullptr && "node already has children"); 503cdce2fe5SMarcel Hlopko 504cdce2fe5SMarcel Hlopko auto *FirstToken = Tokens.begin(); 505cdce2fe5SMarcel Hlopko auto BeginChildren = Trees.lower_bound(FirstToken); 506cdce2fe5SMarcel Hlopko 507cdce2fe5SMarcel Hlopko assert((BeginChildren == Trees.end() || 508cdce2fe5SMarcel Hlopko BeginChildren->first == FirstToken) && 509cdce2fe5SMarcel Hlopko "fold crosses boundaries of existing subtrees"); 510cdce2fe5SMarcel Hlopko auto EndChildren = Trees.lower_bound(Tokens.end()); 511cdce2fe5SMarcel Hlopko assert( 512cdce2fe5SMarcel Hlopko (EndChildren == Trees.end() || EndChildren->first == Tokens.end()) && 513cdce2fe5SMarcel Hlopko "fold crosses boundaries of existing subtrees"); 514cdce2fe5SMarcel Hlopko 515cdce2fe5SMarcel Hlopko // We need to go in reverse order, because we can only prepend. 516cdce2fe5SMarcel Hlopko for (auto It = EndChildren; It != BeginChildren; --It) { 517cdce2fe5SMarcel Hlopko auto *C = std::prev(It)->second; 518cdce2fe5SMarcel Hlopko if (C->role() == NodeRole::Detached) 519cdce2fe5SMarcel Hlopko C->setRole(NodeRole::Unknown); 520cdce2fe5SMarcel Hlopko Node->prependChildLowLevel(C); 521e702bdb8SIlya Biryukov } 5229b3f38f9SIlya Biryukov 523cdce2fe5SMarcel Hlopko // Mark that this node came from the AST and is backed by the source code. 524cdce2fe5SMarcel Hlopko Node->Original = true; 525cdce2fe5SMarcel Hlopko Node->CanModify = A.tokenBuffer().spelledForExpanded(Tokens).hasValue(); 5269b3f38f9SIlya Biryukov 527cdce2fe5SMarcel Hlopko Trees.erase(BeginChildren, EndChildren); 528cdce2fe5SMarcel Hlopko Trees.insert({FirstToken, Node}); 5299b3f38f9SIlya Biryukov } 5309b3f38f9SIlya Biryukov 5319b3f38f9SIlya Biryukov // EXPECTS: all tokens were consumed and are owned by a single root node. 5329b3f38f9SIlya Biryukov syntax::Node *finalize() && { 5339b3f38f9SIlya Biryukov assert(Trees.size() == 1); 534a711a3a4SMarcel Hlopko auto *Root = Trees.begin()->second; 5359b3f38f9SIlya Biryukov Trees = {}; 5369b3f38f9SIlya Biryukov return Root; 5379b3f38f9SIlya Biryukov } 5389b3f38f9SIlya Biryukov 5399b3f38f9SIlya Biryukov std::string str(const syntax::Arena &A) const { 5409b3f38f9SIlya Biryukov std::string R; 5419b3f38f9SIlya Biryukov for (auto It = Trees.begin(); It != Trees.end(); ++It) { 5429b3f38f9SIlya Biryukov unsigned CoveredTokens = 5439b3f38f9SIlya Biryukov It != Trees.end() 5449b3f38f9SIlya Biryukov ? (std::next(It)->first - It->first) 5459b3f38f9SIlya Biryukov : A.tokenBuffer().expandedTokens().end() - It->first; 5469b3f38f9SIlya Biryukov 547ba41a0f7SEduardo Caldas R += std::string( 548ba41a0f7SEduardo Caldas formatv("- '{0}' covers '{1}'+{2} tokens\n", It->second->kind(), 549adcd0268SBenjamin Kramer It->first->text(A.sourceManager()), CoveredTokens)); 550c655d808SEduardo Caldas R += It->second->dump(A.sourceManager()); 5519b3f38f9SIlya Biryukov } 5529b3f38f9SIlya Biryukov return R; 5539b3f38f9SIlya Biryukov } 5549b3f38f9SIlya Biryukov 5559b3f38f9SIlya Biryukov private: 5569b3f38f9SIlya Biryukov /// Maps from the start token to a subtree starting at that token. 557302cb3bcSIlya Biryukov /// Keys in the map are pointers into the array of expanded tokens, so 558302cb3bcSIlya Biryukov /// pointer order corresponds to the order of preprocessor tokens. 559a711a3a4SMarcel Hlopko std::map<const syntax::Token *, syntax::Node *> Trees; 5609b3f38f9SIlya Biryukov }; 5619b3f38f9SIlya Biryukov 5629b3f38f9SIlya Biryukov /// For debugging purposes. 5639b3f38f9SIlya Biryukov std::string str() { return Pending.str(Arena); } 5649b3f38f9SIlya Biryukov 5659b3f38f9SIlya Biryukov syntax::Arena &Arena; 566c1bbefefSIlya Biryukov /// To quickly find tokens by their start location. 567c1bbefefSIlya Biryukov llvm::DenseMap</*SourceLocation*/ unsigned, const syntax::Token *> 568c1bbefefSIlya Biryukov LocationToToken; 5699b3f38f9SIlya Biryukov Forest Pending; 570e702bdb8SIlya Biryukov llvm::DenseSet<Decl *> DeclsWithoutSemicolons; 571a711a3a4SMarcel Hlopko ASTToSyntaxMapping Mapping; 5729b3f38f9SIlya Biryukov }; 5739b3f38f9SIlya Biryukov 5749b3f38f9SIlya Biryukov namespace { 5759b3f38f9SIlya Biryukov class BuildTreeVisitor : public RecursiveASTVisitor<BuildTreeVisitor> { 5769b3f38f9SIlya Biryukov public: 5771db5b348SEduardo Caldas explicit BuildTreeVisitor(ASTContext &Context, syntax::TreeBuilder &Builder) 5781db5b348SEduardo Caldas : Builder(Builder), Context(Context) {} 5799b3f38f9SIlya Biryukov 5809b3f38f9SIlya Biryukov bool shouldTraversePostOrder() const { return true; } 5819b3f38f9SIlya Biryukov 5827d382dcdSMarcel Hlopko bool WalkUpFromDeclaratorDecl(DeclaratorDecl *DD) { 583cdce2fe5SMarcel Hlopko return processDeclaratorAndDeclaration(DD); 5847d382dcdSMarcel Hlopko } 5857d382dcdSMarcel Hlopko 586cdce2fe5SMarcel Hlopko bool WalkUpFromTypedefNameDecl(TypedefNameDecl *TD) { 587cdce2fe5SMarcel Hlopko return processDeclaratorAndDeclaration(TD); 5889b3f38f9SIlya Biryukov } 5899b3f38f9SIlya Biryukov 5909b3f38f9SIlya Biryukov bool VisitDecl(Decl *D) { 5919b3f38f9SIlya Biryukov assert(!D->isImplicit()); 592cdce2fe5SMarcel Hlopko Builder.foldNode(Builder.getDeclarationRange(D), 593a711a3a4SMarcel Hlopko new (allocator()) syntax::UnknownDeclaration(), D); 594e702bdb8SIlya Biryukov return true; 595e702bdb8SIlya Biryukov } 596e702bdb8SIlya Biryukov 59788bf9b3dSMarcel Hlopko // RAV does not call WalkUpFrom* on explicit instantiations, so we have to 59888bf9b3dSMarcel Hlopko // override Traverse. 59988bf9b3dSMarcel Hlopko // FIXME: make RAV call WalkUpFrom* instead. 60088bf9b3dSMarcel Hlopko bool 60188bf9b3dSMarcel Hlopko TraverseClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *C) { 60288bf9b3dSMarcel Hlopko if (!RecursiveASTVisitor::TraverseClassTemplateSpecializationDecl(C)) 60388bf9b3dSMarcel Hlopko return false; 60488bf9b3dSMarcel Hlopko if (C->isExplicitSpecialization()) 60588bf9b3dSMarcel Hlopko return true; // we are only interested in explicit instantiations. 606a711a3a4SMarcel Hlopko auto *Declaration = 607a711a3a4SMarcel Hlopko cast<syntax::SimpleDeclaration>(handleFreeStandingTagDecl(C)); 60888bf9b3dSMarcel Hlopko foldExplicitTemplateInstantiation( 60988bf9b3dSMarcel Hlopko Builder.getTemplateRange(C), Builder.findToken(C->getExternLoc()), 610a711a3a4SMarcel Hlopko Builder.findToken(C->getTemplateKeywordLoc()), Declaration, C); 61188bf9b3dSMarcel Hlopko return true; 61288bf9b3dSMarcel Hlopko } 61388bf9b3dSMarcel Hlopko 61488bf9b3dSMarcel Hlopko bool WalkUpFromTemplateDecl(TemplateDecl *S) { 61588bf9b3dSMarcel Hlopko foldTemplateDeclaration( 616cdce2fe5SMarcel Hlopko Builder.getDeclarationRange(S), 61788bf9b3dSMarcel Hlopko Builder.findToken(S->getTemplateParameters()->getTemplateLoc()), 618cdce2fe5SMarcel Hlopko Builder.getDeclarationRange(S->getTemplatedDecl()), S); 61988bf9b3dSMarcel Hlopko return true; 62088bf9b3dSMarcel Hlopko } 62188bf9b3dSMarcel Hlopko 622e702bdb8SIlya Biryukov bool WalkUpFromTagDecl(TagDecl *C) { 62304f627f6SIlya Biryukov // FIXME: build the ClassSpecifier node. 62488bf9b3dSMarcel Hlopko if (!C->isFreeStanding()) { 62588bf9b3dSMarcel Hlopko assert(C->getNumTemplateParameterLists() == 0); 62604f627f6SIlya Biryukov return true; 62704f627f6SIlya Biryukov } 628a711a3a4SMarcel Hlopko handleFreeStandingTagDecl(C); 629a711a3a4SMarcel Hlopko return true; 630a711a3a4SMarcel Hlopko } 631a711a3a4SMarcel Hlopko 632a711a3a4SMarcel Hlopko syntax::Declaration *handleFreeStandingTagDecl(TagDecl *C) { 633a711a3a4SMarcel Hlopko assert(C->isFreeStanding()); 63488bf9b3dSMarcel Hlopko // Class is a declaration specifier and needs a spanning declaration node. 635cdce2fe5SMarcel Hlopko auto DeclarationRange = Builder.getDeclarationRange(C); 636a711a3a4SMarcel Hlopko syntax::Declaration *Result = new (allocator()) syntax::SimpleDeclaration; 637a711a3a4SMarcel Hlopko Builder.foldNode(DeclarationRange, Result, nullptr); 63888bf9b3dSMarcel Hlopko 63988bf9b3dSMarcel Hlopko // Build TemplateDeclaration nodes if we had template parameters. 64088bf9b3dSMarcel Hlopko auto ConsumeTemplateParameters = [&](const TemplateParameterList &L) { 64188bf9b3dSMarcel Hlopko const auto *TemplateKW = Builder.findToken(L.getTemplateLoc()); 64288bf9b3dSMarcel Hlopko auto R = llvm::makeArrayRef(TemplateKW, DeclarationRange.end()); 643a711a3a4SMarcel Hlopko Result = 644a711a3a4SMarcel Hlopko foldTemplateDeclaration(R, TemplateKW, DeclarationRange, nullptr); 64588bf9b3dSMarcel Hlopko DeclarationRange = R; 64688bf9b3dSMarcel Hlopko }; 647ba41a0f7SEduardo Caldas if (auto *S = dyn_cast<ClassTemplatePartialSpecializationDecl>(C)) 64888bf9b3dSMarcel Hlopko ConsumeTemplateParameters(*S->getTemplateParameters()); 64988bf9b3dSMarcel Hlopko for (unsigned I = C->getNumTemplateParameterLists(); 0 < I; --I) 65088bf9b3dSMarcel Hlopko ConsumeTemplateParameters(*C->getTemplateParameterList(I - 1)); 651a711a3a4SMarcel Hlopko return Result; 6529b3f38f9SIlya Biryukov } 6539b3f38f9SIlya Biryukov 6549b3f38f9SIlya Biryukov bool WalkUpFromTranslationUnitDecl(TranslationUnitDecl *TU) { 6557d382dcdSMarcel Hlopko // We do not want to call VisitDecl(), the declaration for translation 6569b3f38f9SIlya Biryukov // unit is built by finalize(). 6579b3f38f9SIlya Biryukov return true; 6589b3f38f9SIlya Biryukov } 6599b3f38f9SIlya Biryukov 6609b3f38f9SIlya Biryukov bool WalkUpFromCompoundStmt(CompoundStmt *S) { 66151dad419SIlya Biryukov using NodeRole = syntax::NodeRole; 6629b3f38f9SIlya Biryukov 663def65bb4SIlya Biryukov Builder.markChildToken(S->getLBracLoc(), NodeRole::OpenParen); 66458fa50f4SIlya Biryukov for (auto *Child : S->body()) 66558fa50f4SIlya Biryukov Builder.markStmtChild(Child, NodeRole::CompoundStatement_statement); 666def65bb4SIlya Biryukov Builder.markChildToken(S->getRBracLoc(), NodeRole::CloseParen); 6679b3f38f9SIlya Biryukov 66858fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 669a711a3a4SMarcel Hlopko new (allocator()) syntax::CompoundStatement, S); 6709b3f38f9SIlya Biryukov return true; 6719b3f38f9SIlya Biryukov } 6729b3f38f9SIlya Biryukov 67358fa50f4SIlya Biryukov // Some statements are not yet handled by syntax trees. 67458fa50f4SIlya Biryukov bool WalkUpFromStmt(Stmt *S) { 67558fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 676a711a3a4SMarcel Hlopko new (allocator()) syntax::UnknownStatement, S); 67758fa50f4SIlya Biryukov return true; 67858fa50f4SIlya Biryukov } 67958fa50f4SIlya Biryukov 68058fa50f4SIlya Biryukov bool TraverseCXXForRangeStmt(CXXForRangeStmt *S) { 68158fa50f4SIlya Biryukov // We override to traverse range initializer as VarDecl. 68258fa50f4SIlya Biryukov // RAV traverses it as a statement, we produce invalid node kinds in that 68358fa50f4SIlya Biryukov // case. 68458fa50f4SIlya Biryukov // FIXME: should do this in RAV instead? 6857349479fSDmitri Gribenko bool Result = [&, this]() { 68658fa50f4SIlya Biryukov if (S->getInit() && !TraverseStmt(S->getInit())) 68758fa50f4SIlya Biryukov return false; 68858fa50f4SIlya Biryukov if (S->getLoopVariable() && !TraverseDecl(S->getLoopVariable())) 68958fa50f4SIlya Biryukov return false; 69058fa50f4SIlya Biryukov if (S->getRangeInit() && !TraverseStmt(S->getRangeInit())) 69158fa50f4SIlya Biryukov return false; 69258fa50f4SIlya Biryukov if (S->getBody() && !TraverseStmt(S->getBody())) 69358fa50f4SIlya Biryukov return false; 69458fa50f4SIlya Biryukov return true; 6957349479fSDmitri Gribenko }(); 6967349479fSDmitri Gribenko WalkUpFromCXXForRangeStmt(S); 6977349479fSDmitri Gribenko return Result; 69858fa50f4SIlya Biryukov } 69958fa50f4SIlya Biryukov 70058fa50f4SIlya Biryukov bool TraverseStmt(Stmt *S) { 701ba41a0f7SEduardo Caldas if (auto *DS = dyn_cast_or_null<DeclStmt>(S)) { 702e702bdb8SIlya Biryukov // We want to consume the semicolon, make sure SimpleDeclaration does not. 703e702bdb8SIlya Biryukov for (auto *D : DS->decls()) 7047d382dcdSMarcel Hlopko Builder.noticeDeclWithoutSemicolon(D); 705ba41a0f7SEduardo Caldas } else if (auto *E = dyn_cast_or_null<Expr>(S)) { 7063785eb83SEduardo Caldas return RecursiveASTVisitor::TraverseStmt(E->IgnoreImplicit()); 70758fa50f4SIlya Biryukov } 70858fa50f4SIlya Biryukov return RecursiveASTVisitor::TraverseStmt(S); 70958fa50f4SIlya Biryukov } 71058fa50f4SIlya Biryukov 71158fa50f4SIlya Biryukov // Some expressions are not yet handled by syntax trees. 71258fa50f4SIlya Biryukov bool WalkUpFromExpr(Expr *E) { 71358fa50f4SIlya Biryukov assert(!isImplicitExpr(E) && "should be handled by TraverseStmt"); 71458fa50f4SIlya Biryukov Builder.foldNode(Builder.getExprRange(E), 715a711a3a4SMarcel Hlopko new (allocator()) syntax::UnknownExpression, E); 71658fa50f4SIlya Biryukov return true; 71758fa50f4SIlya Biryukov } 71858fa50f4SIlya Biryukov 719f33c2c27SEduardo Caldas bool TraverseUserDefinedLiteral(UserDefinedLiteral *S) { 720f33c2c27SEduardo Caldas // The semantic AST node `UserDefinedLiteral` (UDL) may have one child node 721f33c2c27SEduardo Caldas // referencing the location of the UDL suffix (`_w` in `1.2_w`). The 722f33c2c27SEduardo Caldas // UDL suffix location does not point to the beginning of a token, so we 723f33c2c27SEduardo Caldas // can't represent the UDL suffix as a separate syntax tree node. 724f33c2c27SEduardo Caldas 725f33c2c27SEduardo Caldas return WalkUpFromUserDefinedLiteral(S); 726f33c2c27SEduardo Caldas } 727f33c2c27SEduardo Caldas 7281db5b348SEduardo Caldas syntax::UserDefinedLiteralExpression * 7291db5b348SEduardo Caldas buildUserDefinedLiteral(UserDefinedLiteral *S) { 730f33c2c27SEduardo Caldas switch (S->getLiteralOperatorKind()) { 731ba41a0f7SEduardo Caldas case UserDefinedLiteral::LOK_Integer: 7321db5b348SEduardo Caldas return new (allocator()) syntax::IntegerUserDefinedLiteralExpression; 733ba41a0f7SEduardo Caldas case UserDefinedLiteral::LOK_Floating: 7341db5b348SEduardo Caldas return new (allocator()) syntax::FloatUserDefinedLiteralExpression; 735ba41a0f7SEduardo Caldas case UserDefinedLiteral::LOK_Character: 7361db5b348SEduardo Caldas return new (allocator()) syntax::CharUserDefinedLiteralExpression; 737ba41a0f7SEduardo Caldas case UserDefinedLiteral::LOK_String: 7381db5b348SEduardo Caldas return new (allocator()) syntax::StringUserDefinedLiteralExpression; 739ba41a0f7SEduardo Caldas case UserDefinedLiteral::LOK_Raw: 740ba41a0f7SEduardo Caldas case UserDefinedLiteral::LOK_Template: 7411db5b348SEduardo Caldas // For raw literal operator and numeric literal operator template we 7421db5b348SEduardo Caldas // cannot get the type of the operand in the semantic AST. We get this 7431db5b348SEduardo Caldas // information from the token. As integer and floating point have the same 7441db5b348SEduardo Caldas // token kind, we run `NumericLiteralParser` again to distinguish them. 7451db5b348SEduardo Caldas auto TokLoc = S->getBeginLoc(); 7461db5b348SEduardo Caldas auto TokSpelling = 747a474d5baSEduardo Caldas Builder.findToken(TokLoc)->text(Context.getSourceManager()); 7481db5b348SEduardo Caldas auto Literal = 7491db5b348SEduardo Caldas NumericLiteralParser(TokSpelling, TokLoc, Context.getSourceManager(), 7501db5b348SEduardo Caldas Context.getLangOpts(), Context.getTargetInfo(), 7511db5b348SEduardo Caldas Context.getDiagnostics()); 7521db5b348SEduardo Caldas if (Literal.isIntegerLiteral()) 7531db5b348SEduardo Caldas return new (allocator()) syntax::IntegerUserDefinedLiteralExpression; 754a474d5baSEduardo Caldas else { 755a474d5baSEduardo Caldas assert(Literal.isFloatingLiteral()); 7561db5b348SEduardo Caldas return new (allocator()) syntax::FloatUserDefinedLiteralExpression; 757f33c2c27SEduardo Caldas } 758f33c2c27SEduardo Caldas } 759b8409c03SMichael Liao llvm_unreachable("Unknown literal operator kind."); 760a474d5baSEduardo Caldas } 761f33c2c27SEduardo Caldas 762f33c2c27SEduardo Caldas bool WalkUpFromUserDefinedLiteral(UserDefinedLiteral *S) { 763f33c2c27SEduardo Caldas Builder.markChildToken(S->getBeginLoc(), syntax::NodeRole::LiteralToken); 7641db5b348SEduardo Caldas Builder.foldNode(Builder.getExprRange(S), buildUserDefinedLiteral(S), S); 765f33c2c27SEduardo Caldas return true; 766f33c2c27SEduardo Caldas } 767f33c2c27SEduardo Caldas 7688abb5fb6SEduardo Caldas // FIXME: Fix `NestedNameSpecifierLoc::getLocalSourceRange` for the 7698abb5fb6SEduardo Caldas // `DependentTemplateSpecializationType` case. 770f9500cc4SEduardo Caldas /// Given a nested-name-specifier return the range for the last name 771f9500cc4SEduardo Caldas /// specifier. 7728abb5fb6SEduardo Caldas /// 7738abb5fb6SEduardo Caldas /// e.g. `std::T::template X<U>::` => `template X<U>::` 7748abb5fb6SEduardo Caldas SourceRange getLocalSourceRange(const NestedNameSpecifierLoc &NNSLoc) { 7758abb5fb6SEduardo Caldas auto SR = NNSLoc.getLocalSourceRange(); 7768abb5fb6SEduardo Caldas 777f9500cc4SEduardo Caldas // The method `NestedNameSpecifierLoc::getLocalSourceRange` *should* 778f9500cc4SEduardo Caldas // return the desired `SourceRange`, but there is a corner case. For a 779f9500cc4SEduardo Caldas // `DependentTemplateSpecializationType` this method returns its 7808abb5fb6SEduardo Caldas // qualifiers as well, in other words in the example above this method 7818abb5fb6SEduardo Caldas // returns `T::template X<U>::` instead of only `template X<U>::` 7828abb5fb6SEduardo Caldas if (auto TL = NNSLoc.getTypeLoc()) { 7838abb5fb6SEduardo Caldas if (auto DependentTL = 7848abb5fb6SEduardo Caldas TL.getAs<DependentTemplateSpecializationTypeLoc>()) { 7858abb5fb6SEduardo Caldas // The 'template' keyword is always present in dependent template 7868abb5fb6SEduardo Caldas // specializations. Except in the case of incorrect code 7878abb5fb6SEduardo Caldas // TODO: Treat the case of incorrect code. 7888abb5fb6SEduardo Caldas SR.setBegin(DependentTL.getTemplateKeywordLoc()); 7898abb5fb6SEduardo Caldas } 7908abb5fb6SEduardo Caldas } 7918abb5fb6SEduardo Caldas 7928abb5fb6SEduardo Caldas return SR; 7938abb5fb6SEduardo Caldas } 7948abb5fb6SEduardo Caldas 795f9500cc4SEduardo Caldas syntax::NodeKind getNameSpecifierKind(const NestedNameSpecifier &NNS) { 796f9500cc4SEduardo Caldas switch (NNS.getKind()) { 797f9500cc4SEduardo Caldas case NestedNameSpecifier::Global: 798f9500cc4SEduardo Caldas return syntax::NodeKind::GlobalNameSpecifier; 799f9500cc4SEduardo Caldas case NestedNameSpecifier::Namespace: 800f9500cc4SEduardo Caldas case NestedNameSpecifier::NamespaceAlias: 801f9500cc4SEduardo Caldas case NestedNameSpecifier::Identifier: 802f9500cc4SEduardo Caldas return syntax::NodeKind::IdentifierNameSpecifier; 803f9500cc4SEduardo Caldas case NestedNameSpecifier::TypeSpecWithTemplate: 804f9500cc4SEduardo Caldas return syntax::NodeKind::SimpleTemplateNameSpecifier; 805f9500cc4SEduardo Caldas case NestedNameSpecifier::TypeSpec: { 806f9500cc4SEduardo Caldas const auto *NNSType = NNS.getAsType(); 807f9500cc4SEduardo Caldas assert(NNSType); 808f9500cc4SEduardo Caldas if (isa<DecltypeType>(NNSType)) 809f9500cc4SEduardo Caldas return syntax::NodeKind::DecltypeNameSpecifier; 810f9500cc4SEduardo Caldas if (isa<TemplateSpecializationType, DependentTemplateSpecializationType>( 811f9500cc4SEduardo Caldas NNSType)) 812f9500cc4SEduardo Caldas return syntax::NodeKind::SimpleTemplateNameSpecifier; 813f9500cc4SEduardo Caldas return syntax::NodeKind::IdentifierNameSpecifier; 814f9500cc4SEduardo Caldas } 815f9500cc4SEduardo Caldas default: 816f9500cc4SEduardo Caldas // FIXME: Support Microsoft's __super 817f9500cc4SEduardo Caldas llvm::report_fatal_error("We don't yet support the __super specifier", 818f9500cc4SEduardo Caldas true); 819f9500cc4SEduardo Caldas } 820f9500cc4SEduardo Caldas } 821f9500cc4SEduardo Caldas 822f9500cc4SEduardo Caldas syntax::NameSpecifier * 823f9500cc4SEduardo Caldas BuildNameSpecifier(const NestedNameSpecifierLoc &NNSLoc) { 824f9500cc4SEduardo Caldas assert(NNSLoc.hasQualifier()); 825f9500cc4SEduardo Caldas auto NameSpecifierTokens = 826f9500cc4SEduardo Caldas Builder.getRange(getLocalSourceRange(NNSLoc)).drop_back(); 827f9500cc4SEduardo Caldas switch (getNameSpecifierKind(*NNSLoc.getNestedNameSpecifier())) { 828f9500cc4SEduardo Caldas case syntax::NodeKind::GlobalNameSpecifier: 829f9500cc4SEduardo Caldas return new (allocator()) syntax::GlobalNameSpecifier; 830f9500cc4SEduardo Caldas case syntax::NodeKind::IdentifierNameSpecifier: { 831f9500cc4SEduardo Caldas assert(NameSpecifierTokens.size() == 1); 832f9500cc4SEduardo Caldas Builder.markChildToken(NameSpecifierTokens.begin(), 833f9500cc4SEduardo Caldas syntax::NodeRole::Unknown); 834f9500cc4SEduardo Caldas auto *NS = new (allocator()) syntax::IdentifierNameSpecifier; 835f9500cc4SEduardo Caldas Builder.foldNode(NameSpecifierTokens, NS, nullptr); 836f9500cc4SEduardo Caldas return NS; 837f9500cc4SEduardo Caldas } 838f9500cc4SEduardo Caldas case syntax::NodeKind::SimpleTemplateNameSpecifier: { 839f9500cc4SEduardo Caldas // TODO: Build `SimpleTemplateNameSpecifier` children and implement 840f9500cc4SEduardo Caldas // accessors to them. 841f9500cc4SEduardo Caldas // Be aware, we cannot do that simply by calling `TraverseTypeLoc`, 842f9500cc4SEduardo Caldas // some `TypeLoc`s have inside them the previous name specifier and 843f9500cc4SEduardo Caldas // we want to treat them independently. 844f9500cc4SEduardo Caldas auto *NS = new (allocator()) syntax::SimpleTemplateNameSpecifier; 845f9500cc4SEduardo Caldas Builder.foldNode(NameSpecifierTokens, NS, nullptr); 846f9500cc4SEduardo Caldas return NS; 847f9500cc4SEduardo Caldas } 848f9500cc4SEduardo Caldas case syntax::NodeKind::DecltypeNameSpecifier: { 849f9500cc4SEduardo Caldas const auto TL = NNSLoc.getTypeLoc().castAs<DecltypeTypeLoc>(); 850f9500cc4SEduardo Caldas if (!RecursiveASTVisitor::TraverseDecltypeTypeLoc(TL)) 8518abb5fb6SEduardo Caldas return nullptr; 852f9500cc4SEduardo Caldas auto *NS = new (allocator()) syntax::DecltypeNameSpecifier; 853f9500cc4SEduardo Caldas // TODO: Implement accessor to `DecltypeNameSpecifier` inner 854f9500cc4SEduardo Caldas // `DecltypeTypeLoc`. 855f9500cc4SEduardo Caldas // For that add mapping from `TypeLoc` to `syntax::Node*` then: 856f9500cc4SEduardo Caldas // Builder.markChild(TypeLoc, syntax::NodeRole); 857f9500cc4SEduardo Caldas Builder.foldNode(NameSpecifierTokens, NS, nullptr); 858f9500cc4SEduardo Caldas return NS; 859f9500cc4SEduardo Caldas } 860f9500cc4SEduardo Caldas default: 861f9500cc4SEduardo Caldas llvm_unreachable("getChildKind() does not return this value"); 862f9500cc4SEduardo Caldas } 863f9500cc4SEduardo Caldas } 864f9500cc4SEduardo Caldas 865f9500cc4SEduardo Caldas // To build syntax tree nodes for NestedNameSpecifierLoc we override 866f9500cc4SEduardo Caldas // Traverse instead of WalkUpFrom because we want to traverse the children 867f9500cc4SEduardo Caldas // ourselves and build a list instead of a nested tree of name specifier 868f9500cc4SEduardo Caldas // prefixes. 869f9500cc4SEduardo Caldas bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc QualifierLoc) { 870f9500cc4SEduardo Caldas if (!QualifierLoc) 871f9500cc4SEduardo Caldas return true; 8728abb5fb6SEduardo Caldas for (auto it = QualifierLoc; it; it = it.getPrefix()) { 873f9500cc4SEduardo Caldas auto *NS = BuildNameSpecifier(it); 874f9500cc4SEduardo Caldas if (!NS) 875f9500cc4SEduardo Caldas return false; 876fdbd5996SEduardo Caldas Builder.markChild(NS, syntax::NodeRole::List_element); 877fdbd5996SEduardo Caldas Builder.markChildToken(it.getEndLoc(), syntax::NodeRole::List_delimiter); 8788abb5fb6SEduardo Caldas } 879f9500cc4SEduardo Caldas Builder.foldNode(Builder.getRange(QualifierLoc.getSourceRange()), 880f9500cc4SEduardo Caldas new (allocator()) syntax::NestedNameSpecifier, 8818abb5fb6SEduardo Caldas QualifierLoc); 882f9500cc4SEduardo Caldas return true; 8838abb5fb6SEduardo Caldas } 8848abb5fb6SEduardo Caldas 885a4ef9e86SEduardo Caldas syntax::IdExpression *buildIdExpression(NestedNameSpecifierLoc QualifierLoc, 886a4ef9e86SEduardo Caldas SourceLocation TemplateKeywordLoc, 887a4ef9e86SEduardo Caldas SourceRange UnqualifiedIdLoc, 888a4ef9e86SEduardo Caldas ASTPtr From) { 889a4ef9e86SEduardo Caldas if (QualifierLoc) { 890ba32915dSEduardo Caldas Builder.markChild(QualifierLoc, syntax::NodeRole::IdExpression_qualifier); 891ba32915dSEduardo Caldas if (TemplateKeywordLoc.isValid()) 892ba32915dSEduardo Caldas Builder.markChildToken(TemplateKeywordLoc, 893ba32915dSEduardo Caldas syntax::NodeRole::TemplateKeyword); 894a4ef9e86SEduardo Caldas } 895ba32915dSEduardo Caldas 896ba32915dSEduardo Caldas auto *TheUnqualifiedId = new (allocator()) syntax::UnqualifiedId; 897a4ef9e86SEduardo Caldas Builder.foldNode(Builder.getRange(UnqualifiedIdLoc), TheUnqualifiedId, 898a4ef9e86SEduardo Caldas nullptr); 899ba32915dSEduardo Caldas Builder.markChild(TheUnqualifiedId, syntax::NodeRole::IdExpression_id); 900ba32915dSEduardo Caldas 901a4ef9e86SEduardo Caldas auto IdExpressionBeginLoc = 902a4ef9e86SEduardo Caldas QualifierLoc ? QualifierLoc.getBeginLoc() : UnqualifiedIdLoc.getBegin(); 903ba32915dSEduardo Caldas 904a4ef9e86SEduardo Caldas auto *TheIdExpression = new (allocator()) syntax::IdExpression; 905a4ef9e86SEduardo Caldas Builder.foldNode( 906a4ef9e86SEduardo Caldas Builder.getRange(IdExpressionBeginLoc, UnqualifiedIdLoc.getEnd()), 907a4ef9e86SEduardo Caldas TheIdExpression, From); 908a4ef9e86SEduardo Caldas 909a4ef9e86SEduardo Caldas return TheIdExpression; 910a4ef9e86SEduardo Caldas } 911a4ef9e86SEduardo Caldas 912a4ef9e86SEduardo Caldas bool WalkUpFromMemberExpr(MemberExpr *S) { 913ba32915dSEduardo Caldas // For `MemberExpr` with implicit `this->` we generate a simple 914ba32915dSEduardo Caldas // `id-expression` syntax node, beacuse an implicit `member-expression` is 915ba32915dSEduardo Caldas // syntactically undistinguishable from an `id-expression` 916ba32915dSEduardo Caldas if (S->isImplicitAccess()) { 917a4ef9e86SEduardo Caldas buildIdExpression(S->getQualifierLoc(), S->getTemplateKeywordLoc(), 918a4ef9e86SEduardo Caldas SourceRange(S->getMemberLoc(), S->getEndLoc()), S); 919ba32915dSEduardo Caldas return true; 920ba32915dSEduardo Caldas } 921a4ef9e86SEduardo Caldas 922a4ef9e86SEduardo Caldas auto *TheIdExpression = buildIdExpression( 923a4ef9e86SEduardo Caldas S->getQualifierLoc(), S->getTemplateKeywordLoc(), 924a4ef9e86SEduardo Caldas SourceRange(S->getMemberLoc(), S->getEndLoc()), nullptr); 925ba32915dSEduardo Caldas 926ba32915dSEduardo Caldas Builder.markChild(TheIdExpression, 927ba32915dSEduardo Caldas syntax::NodeRole::MemberExpression_member); 928ba32915dSEduardo Caldas 929ba32915dSEduardo Caldas Builder.markExprChild(S->getBase(), 930ba32915dSEduardo Caldas syntax::NodeRole::MemberExpression_object); 931ba32915dSEduardo Caldas Builder.markChildToken(S->getOperatorLoc(), 932ba32915dSEduardo Caldas syntax::NodeRole::MemberExpression_accessToken); 933ba32915dSEduardo Caldas 934ba32915dSEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 935ba32915dSEduardo Caldas new (allocator()) syntax::MemberExpression, S); 936ba32915dSEduardo Caldas return true; 937ba32915dSEduardo Caldas } 938ba32915dSEduardo Caldas 9391b2f6b4aSEduardo Caldas bool WalkUpFromDeclRefExpr(DeclRefExpr *S) { 940a4ef9e86SEduardo Caldas buildIdExpression(S->getQualifierLoc(), S->getTemplateKeywordLoc(), 941a4ef9e86SEduardo Caldas SourceRange(S->getLocation(), S->getEndLoc()), S); 9428abb5fb6SEduardo Caldas 9438abb5fb6SEduardo Caldas return true; 9441b2f6b4aSEduardo Caldas } 9458abb5fb6SEduardo Caldas 9468abb5fb6SEduardo Caldas // Same logic as DeclRefExpr. 9478abb5fb6SEduardo Caldas bool WalkUpFromDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *S) { 948a4ef9e86SEduardo Caldas buildIdExpression(S->getQualifierLoc(), S->getTemplateKeywordLoc(), 949a4ef9e86SEduardo Caldas SourceRange(S->getLocation(), S->getEndLoc()), S); 9508abb5fb6SEduardo Caldas 9511b2f6b4aSEduardo Caldas return true; 9521b2f6b4aSEduardo Caldas } 9531b2f6b4aSEduardo Caldas 95485c15f17SEduardo Caldas bool WalkUpFromCXXThisExpr(CXXThisExpr *S) { 95585c15f17SEduardo Caldas if (!S->isImplicit()) { 95685c15f17SEduardo Caldas Builder.markChildToken(S->getLocation(), 95785c15f17SEduardo Caldas syntax::NodeRole::IntroducerKeyword); 95885c15f17SEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 95985c15f17SEduardo Caldas new (allocator()) syntax::ThisExpression, S); 96085c15f17SEduardo Caldas } 96185c15f17SEduardo Caldas return true; 96285c15f17SEduardo Caldas } 96385c15f17SEduardo Caldas 964fdbd7833SEduardo Caldas bool WalkUpFromParenExpr(ParenExpr *S) { 965fdbd7833SEduardo Caldas Builder.markChildToken(S->getLParen(), syntax::NodeRole::OpenParen); 966fdbd7833SEduardo Caldas Builder.markExprChild(S->getSubExpr(), 967fdbd7833SEduardo Caldas syntax::NodeRole::ParenExpression_subExpression); 968fdbd7833SEduardo Caldas Builder.markChildToken(S->getRParen(), syntax::NodeRole::CloseParen); 969fdbd7833SEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 970fdbd7833SEduardo Caldas new (allocator()) syntax::ParenExpression, S); 971fdbd7833SEduardo Caldas return true; 972fdbd7833SEduardo Caldas } 973fdbd7833SEduardo Caldas 9743b739690SEduardo Caldas bool WalkUpFromIntegerLiteral(IntegerLiteral *S) { 97542f6fec3SEduardo Caldas Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken); 9763b739690SEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 9773b739690SEduardo Caldas new (allocator()) syntax::IntegerLiteralExpression, S); 9783b739690SEduardo Caldas return true; 9793b739690SEduardo Caldas } 9803b739690SEduardo Caldas 981221d7bbeSEduardo Caldas bool WalkUpFromCharacterLiteral(CharacterLiteral *S) { 982221d7bbeSEduardo Caldas Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken); 983221d7bbeSEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 984221d7bbeSEduardo Caldas new (allocator()) syntax::CharacterLiteralExpression, S); 985221d7bbeSEduardo Caldas return true; 986221d7bbeSEduardo Caldas } 9877b404b6dSEduardo Caldas 9887b404b6dSEduardo Caldas bool WalkUpFromFloatingLiteral(FloatingLiteral *S) { 9897b404b6dSEduardo Caldas Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken); 9907b404b6dSEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 9917b404b6dSEduardo Caldas new (allocator()) syntax::FloatingLiteralExpression, S); 9927b404b6dSEduardo Caldas return true; 9937b404b6dSEduardo Caldas } 9947b404b6dSEduardo Caldas 995466e8b7eSEduardo Caldas bool WalkUpFromStringLiteral(StringLiteral *S) { 996466e8b7eSEduardo Caldas Builder.markChildToken(S->getBeginLoc(), syntax::NodeRole::LiteralToken); 997466e8b7eSEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 998466e8b7eSEduardo Caldas new (allocator()) syntax::StringLiteralExpression, S); 999466e8b7eSEduardo Caldas return true; 1000466e8b7eSEduardo Caldas } 1001221d7bbeSEduardo Caldas 10027b404b6dSEduardo Caldas bool WalkUpFromCXXBoolLiteralExpr(CXXBoolLiteralExpr *S) { 10037b404b6dSEduardo Caldas Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken); 10047b404b6dSEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 10057b404b6dSEduardo Caldas new (allocator()) syntax::BoolLiteralExpression, S); 10067b404b6dSEduardo Caldas return true; 10077b404b6dSEduardo Caldas } 10087b404b6dSEduardo Caldas 1009007098d7SEduardo Caldas bool WalkUpFromCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *S) { 101042f6fec3SEduardo Caldas Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken); 1011007098d7SEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 1012007098d7SEduardo Caldas new (allocator()) syntax::CxxNullPtrExpression, S); 1013007098d7SEduardo Caldas return true; 1014007098d7SEduardo Caldas } 1015007098d7SEduardo Caldas 1016461af57dSEduardo Caldas bool WalkUpFromUnaryOperator(UnaryOperator *S) { 101742f6fec3SEduardo Caldas Builder.markChildToken(S->getOperatorLoc(), 101842f6fec3SEduardo Caldas syntax::NodeRole::OperatorExpression_operatorToken); 1019461af57dSEduardo Caldas Builder.markExprChild(S->getSubExpr(), 1020461af57dSEduardo Caldas syntax::NodeRole::UnaryOperatorExpression_operand); 1021461af57dSEduardo Caldas 1022461af57dSEduardo Caldas if (S->isPostfix()) 1023461af57dSEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 1024461af57dSEduardo Caldas new (allocator()) syntax::PostfixUnaryOperatorExpression, 1025461af57dSEduardo Caldas S); 1026461af57dSEduardo Caldas else 1027461af57dSEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 1028461af57dSEduardo Caldas new (allocator()) syntax::PrefixUnaryOperatorExpression, 1029461af57dSEduardo Caldas S); 1030461af57dSEduardo Caldas 1031461af57dSEduardo Caldas return true; 1032461af57dSEduardo Caldas } 1033461af57dSEduardo Caldas 10343785eb83SEduardo Caldas bool WalkUpFromBinaryOperator(BinaryOperator *S) { 10353785eb83SEduardo Caldas Builder.markExprChild( 10363785eb83SEduardo Caldas S->getLHS(), syntax::NodeRole::BinaryOperatorExpression_leftHandSide); 103742f6fec3SEduardo Caldas Builder.markChildToken(S->getOperatorLoc(), 103842f6fec3SEduardo Caldas syntax::NodeRole::OperatorExpression_operatorToken); 10393785eb83SEduardo Caldas Builder.markExprChild( 10403785eb83SEduardo Caldas S->getRHS(), syntax::NodeRole::BinaryOperatorExpression_rightHandSide); 10413785eb83SEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 10423785eb83SEduardo Caldas new (allocator()) syntax::BinaryOperatorExpression, S); 10433785eb83SEduardo Caldas return true; 10443785eb83SEduardo Caldas } 10453785eb83SEduardo Caldas 1046*2de2ca34SEduardo Caldas syntax::CallArguments *buildCallArguments(CallExpr::arg_range Args) { 1047*2de2ca34SEduardo Caldas for (const auto &Arg : Args) { 1048*2de2ca34SEduardo Caldas Builder.markExprChild(Arg, syntax::NodeRole::List_element); 1049*2de2ca34SEduardo Caldas const auto *DelimiterToken = 1050*2de2ca34SEduardo Caldas std::next(Builder.findToken(Arg->getEndLoc())); 1051*2de2ca34SEduardo Caldas if (DelimiterToken->kind() == clang::tok::TokenKind::comma) 1052*2de2ca34SEduardo Caldas Builder.markChildToken(DelimiterToken, 1053*2de2ca34SEduardo Caldas syntax::NodeRole::List_delimiter); 1054*2de2ca34SEduardo Caldas } 1055*2de2ca34SEduardo Caldas 1056*2de2ca34SEduardo Caldas auto *Arguments = new (allocator()) syntax::CallArguments; 1057*2de2ca34SEduardo Caldas if (!Args.empty()) 1058*2de2ca34SEduardo Caldas Builder.foldNode(Builder.getRange((*Args.begin())->getBeginLoc(), 1059*2de2ca34SEduardo Caldas (*(Args.end() - 1))->getEndLoc()), 1060*2de2ca34SEduardo Caldas Arguments, nullptr); 1061*2de2ca34SEduardo Caldas 1062*2de2ca34SEduardo Caldas return Arguments; 1063*2de2ca34SEduardo Caldas } 1064*2de2ca34SEduardo Caldas 1065*2de2ca34SEduardo Caldas bool WalkUpFromCallExpr(CallExpr *S) { 1066*2de2ca34SEduardo Caldas Builder.markExprChild(S->getCallee(), 1067*2de2ca34SEduardo Caldas syntax::NodeRole::CallExpression_callee); 1068*2de2ca34SEduardo Caldas 1069*2de2ca34SEduardo Caldas const auto *LParenToken = 1070*2de2ca34SEduardo Caldas std::next(Builder.findToken(S->getCallee()->getEndLoc())); 1071*2de2ca34SEduardo Caldas // FIXME: Assert that `LParenToken` is indeed a `l_paren` once we have fixed 1072*2de2ca34SEduardo Caldas // the test on decltype desctructors. 1073*2de2ca34SEduardo Caldas if (LParenToken->kind() == clang::tok::l_paren) 1074*2de2ca34SEduardo Caldas Builder.markChildToken(LParenToken, syntax::NodeRole::OpenParen); 1075*2de2ca34SEduardo Caldas 1076*2de2ca34SEduardo Caldas Builder.markChild(buildCallArguments(S->arguments()), 1077*2de2ca34SEduardo Caldas syntax::NodeRole::CallExpression_arguments); 1078*2de2ca34SEduardo Caldas 1079*2de2ca34SEduardo Caldas Builder.markChildToken(S->getRParenLoc(), syntax::NodeRole::CloseParen); 1080*2de2ca34SEduardo Caldas 1081*2de2ca34SEduardo Caldas Builder.foldNode(Builder.getRange(S->getSourceRange()), 1082*2de2ca34SEduardo Caldas new (allocator()) syntax::CallExpression, S); 1083*2de2ca34SEduardo Caldas return true; 1084*2de2ca34SEduardo Caldas } 1085*2de2ca34SEduardo Caldas 1086ea8bba7eSEduardo Caldas bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *S) { 1087ac37afa6SEduardo Caldas // To construct a syntax tree of the same shape for calls to built-in and 1088ac37afa6SEduardo Caldas // user-defined operators, ignore the `DeclRefExpr` that refers to the 1089ac37afa6SEduardo Caldas // operator and treat it as a simple token. Do that by traversing 1090ac37afa6SEduardo Caldas // arguments instead of children. 1091ac37afa6SEduardo Caldas for (auto *child : S->arguments()) { 1092f33c2c27SEduardo Caldas // A postfix unary operator is declared as taking two operands. The 1093f33c2c27SEduardo Caldas // second operand is used to distinguish from its prefix counterpart. In 1094f33c2c27SEduardo Caldas // the semantic AST this "phantom" operand is represented as a 1095ea8bba7eSEduardo Caldas // `IntegerLiteral` with invalid `SourceLocation`. We skip visiting this 1096ea8bba7eSEduardo Caldas // operand because it does not correspond to anything written in source 1097ac37afa6SEduardo Caldas // code. 1098ac37afa6SEduardo Caldas if (child->getSourceRange().isInvalid()) { 1099ac37afa6SEduardo Caldas assert(getOperatorNodeKind(*S) == 1100ac37afa6SEduardo Caldas syntax::NodeKind::PostfixUnaryOperatorExpression); 1101ea8bba7eSEduardo Caldas continue; 1102ac37afa6SEduardo Caldas } 1103ea8bba7eSEduardo Caldas if (!TraverseStmt(child)) 1104ea8bba7eSEduardo Caldas return false; 1105ea8bba7eSEduardo Caldas } 1106ea8bba7eSEduardo Caldas return WalkUpFromCXXOperatorCallExpr(S); 1107ea8bba7eSEduardo Caldas } 1108ea8bba7eSEduardo Caldas 11093a574a6cSEduardo Caldas bool WalkUpFromCXXOperatorCallExpr(CXXOperatorCallExpr *S) { 1110ea8bba7eSEduardo Caldas switch (getOperatorNodeKind(*S)) { 1111ea8bba7eSEduardo Caldas case syntax::NodeKind::BinaryOperatorExpression: 11123a574a6cSEduardo Caldas Builder.markExprChild( 11133a574a6cSEduardo Caldas S->getArg(0), 11143a574a6cSEduardo Caldas syntax::NodeRole::BinaryOperatorExpression_leftHandSide); 11153a574a6cSEduardo Caldas Builder.markChildToken( 11163a574a6cSEduardo Caldas S->getOperatorLoc(), 111742f6fec3SEduardo Caldas syntax::NodeRole::OperatorExpression_operatorToken); 11183a574a6cSEduardo Caldas Builder.markExprChild( 11193a574a6cSEduardo Caldas S->getArg(1), 11203a574a6cSEduardo Caldas syntax::NodeRole::BinaryOperatorExpression_rightHandSide); 11213a574a6cSEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 11223a574a6cSEduardo Caldas new (allocator()) syntax::BinaryOperatorExpression, S); 11233a574a6cSEduardo Caldas return true; 1124ea8bba7eSEduardo Caldas case syntax::NodeKind::PrefixUnaryOperatorExpression: 1125ea8bba7eSEduardo Caldas Builder.markChildToken( 1126ea8bba7eSEduardo Caldas S->getOperatorLoc(), 1127ea8bba7eSEduardo Caldas syntax::NodeRole::OperatorExpression_operatorToken); 1128ea8bba7eSEduardo Caldas Builder.markExprChild(S->getArg(0), 1129ea8bba7eSEduardo Caldas syntax::NodeRole::UnaryOperatorExpression_operand); 1130ea8bba7eSEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 1131ea8bba7eSEduardo Caldas new (allocator()) syntax::PrefixUnaryOperatorExpression, 1132ea8bba7eSEduardo Caldas S); 1133ea8bba7eSEduardo Caldas return true; 1134ea8bba7eSEduardo Caldas case syntax::NodeKind::PostfixUnaryOperatorExpression: 1135ea8bba7eSEduardo Caldas Builder.markChildToken( 1136ea8bba7eSEduardo Caldas S->getOperatorLoc(), 1137ea8bba7eSEduardo Caldas syntax::NodeRole::OperatorExpression_operatorToken); 1138ea8bba7eSEduardo Caldas Builder.markExprChild(S->getArg(0), 1139ea8bba7eSEduardo Caldas syntax::NodeRole::UnaryOperatorExpression_operand); 1140ea8bba7eSEduardo Caldas Builder.foldNode(Builder.getExprRange(S), 1141ea8bba7eSEduardo Caldas new (allocator()) syntax::PostfixUnaryOperatorExpression, 1142ea8bba7eSEduardo Caldas S); 1143ea8bba7eSEduardo Caldas return true; 1144*2de2ca34SEduardo Caldas case syntax::NodeKind::CallExpression: { 1145*2de2ca34SEduardo Caldas Builder.markExprChild(S->getArg(0), 1146*2de2ca34SEduardo Caldas syntax::NodeRole::CallExpression_callee); 1147*2de2ca34SEduardo Caldas 1148*2de2ca34SEduardo Caldas const auto *LParenToken = 1149*2de2ca34SEduardo Caldas std::next(Builder.findToken(S->getArg(0)->getEndLoc())); 1150*2de2ca34SEduardo Caldas // FIXME: Assert that `LParenToken` is indeed a `l_paren` once we have 1151*2de2ca34SEduardo Caldas // fixed the test on decltype desctructors. 1152*2de2ca34SEduardo Caldas if (LParenToken->kind() == clang::tok::l_paren) 1153*2de2ca34SEduardo Caldas Builder.markChildToken(LParenToken, syntax::NodeRole::OpenParen); 1154*2de2ca34SEduardo Caldas 1155*2de2ca34SEduardo Caldas Builder.markChild(buildCallArguments(CallExpr::arg_range( 1156*2de2ca34SEduardo Caldas S->arg_begin() + 1, S->arg_end())), 1157*2de2ca34SEduardo Caldas syntax::NodeRole::CallExpression_arguments); 1158*2de2ca34SEduardo Caldas 1159*2de2ca34SEduardo Caldas Builder.markChildToken(S->getRParenLoc(), syntax::NodeRole::CloseParen); 1160*2de2ca34SEduardo Caldas 1161*2de2ca34SEduardo Caldas Builder.foldNode(Builder.getRange(S->getSourceRange()), 1162*2de2ca34SEduardo Caldas new (allocator()) syntax::CallExpression, S); 1163*2de2ca34SEduardo Caldas return true; 1164*2de2ca34SEduardo Caldas } 1165ea8bba7eSEduardo Caldas case syntax::NodeKind::UnknownExpression: 1166*2de2ca34SEduardo Caldas return WalkUpFromExpr(S); 1167ea8bba7eSEduardo Caldas default: 1168ea8bba7eSEduardo Caldas llvm_unreachable("getOperatorNodeKind() does not return this value"); 1169ea8bba7eSEduardo Caldas } 11703a574a6cSEduardo Caldas } 11713a574a6cSEduardo Caldas 1172be14a22bSIlya Biryukov bool WalkUpFromNamespaceDecl(NamespaceDecl *S) { 1173cdce2fe5SMarcel Hlopko auto Tokens = Builder.getDeclarationRange(S); 1174be14a22bSIlya Biryukov if (Tokens.front().kind() == tok::coloncolon) { 1175be14a22bSIlya Biryukov // Handle nested namespace definitions. Those start at '::' token, e.g. 1176be14a22bSIlya Biryukov // namespace a^::b {} 1177be14a22bSIlya Biryukov // FIXME: build corresponding nodes for the name of this namespace. 1178be14a22bSIlya Biryukov return true; 1179be14a22bSIlya Biryukov } 1180a711a3a4SMarcel Hlopko Builder.foldNode(Tokens, new (allocator()) syntax::NamespaceDefinition, S); 1181be14a22bSIlya Biryukov return true; 1182be14a22bSIlya Biryukov } 1183be14a22bSIlya Biryukov 11848ce15f7eSEduardo Caldas // FIXME: Deleting the `TraverseParenTypeLoc` override doesn't change test 11858ce15f7eSEduardo Caldas // results. Find test coverage or remove it. 11867d382dcdSMarcel Hlopko bool TraverseParenTypeLoc(ParenTypeLoc L) { 11877d382dcdSMarcel Hlopko // We reverse order of traversal to get the proper syntax structure. 11887d382dcdSMarcel Hlopko if (!WalkUpFromParenTypeLoc(L)) 11897d382dcdSMarcel Hlopko return false; 11907d382dcdSMarcel Hlopko return TraverseTypeLoc(L.getInnerLoc()); 11917d382dcdSMarcel Hlopko } 11927d382dcdSMarcel Hlopko 11937d382dcdSMarcel Hlopko bool WalkUpFromParenTypeLoc(ParenTypeLoc L) { 11947d382dcdSMarcel Hlopko Builder.markChildToken(L.getLParenLoc(), syntax::NodeRole::OpenParen); 11957d382dcdSMarcel Hlopko Builder.markChildToken(L.getRParenLoc(), syntax::NodeRole::CloseParen); 11967d382dcdSMarcel Hlopko Builder.foldNode(Builder.getRange(L.getLParenLoc(), L.getRParenLoc()), 1197a711a3a4SMarcel Hlopko new (allocator()) syntax::ParenDeclarator, L); 11987d382dcdSMarcel Hlopko return true; 11997d382dcdSMarcel Hlopko } 12007d382dcdSMarcel Hlopko 12017d382dcdSMarcel Hlopko // Declarator chunks, they are produced by type locs and some clang::Decls. 12027d382dcdSMarcel Hlopko bool WalkUpFromArrayTypeLoc(ArrayTypeLoc L) { 12037d382dcdSMarcel Hlopko Builder.markChildToken(L.getLBracketLoc(), syntax::NodeRole::OpenParen); 12047d382dcdSMarcel Hlopko Builder.markExprChild(L.getSizeExpr(), 12057d382dcdSMarcel Hlopko syntax::NodeRole::ArraySubscript_sizeExpression); 12067d382dcdSMarcel Hlopko Builder.markChildToken(L.getRBracketLoc(), syntax::NodeRole::CloseParen); 12077d382dcdSMarcel Hlopko Builder.foldNode(Builder.getRange(L.getLBracketLoc(), L.getRBracketLoc()), 1208a711a3a4SMarcel Hlopko new (allocator()) syntax::ArraySubscript, L); 12097d382dcdSMarcel Hlopko return true; 12107d382dcdSMarcel Hlopko } 12117d382dcdSMarcel Hlopko 12127d382dcdSMarcel Hlopko bool WalkUpFromFunctionTypeLoc(FunctionTypeLoc L) { 12137d382dcdSMarcel Hlopko Builder.markChildToken(L.getLParenLoc(), syntax::NodeRole::OpenParen); 1214cdce2fe5SMarcel Hlopko for (auto *P : L.getParams()) { 1215cdce2fe5SMarcel Hlopko Builder.markChild(P, syntax::NodeRole::ParametersAndQualifiers_parameter); 1216cdce2fe5SMarcel Hlopko } 12177d382dcdSMarcel Hlopko Builder.markChildToken(L.getRParenLoc(), syntax::NodeRole::CloseParen); 12187d382dcdSMarcel Hlopko Builder.foldNode(Builder.getRange(L.getLParenLoc(), L.getEndLoc()), 1219a711a3a4SMarcel Hlopko new (allocator()) syntax::ParametersAndQualifiers, L); 12207d382dcdSMarcel Hlopko return true; 12217d382dcdSMarcel Hlopko } 12227d382dcdSMarcel Hlopko 12237d382dcdSMarcel Hlopko bool WalkUpFromFunctionProtoTypeLoc(FunctionProtoTypeLoc L) { 12247d382dcdSMarcel Hlopko if (!L.getTypePtr()->hasTrailingReturn()) 12257d382dcdSMarcel Hlopko return WalkUpFromFunctionTypeLoc(L); 12267d382dcdSMarcel Hlopko 1227cdce2fe5SMarcel Hlopko auto *TrailingReturnTokens = BuildTrailingReturn(L); 12287d382dcdSMarcel Hlopko // Finish building the node for parameters. 12297d382dcdSMarcel Hlopko Builder.markChild(TrailingReturnTokens, 12307d382dcdSMarcel Hlopko syntax::NodeRole::ParametersAndQualifiers_trailingReturn); 12317d382dcdSMarcel Hlopko return WalkUpFromFunctionTypeLoc(L); 12327d382dcdSMarcel Hlopko } 12337d382dcdSMarcel Hlopko 12348ce15f7eSEduardo Caldas bool TraverseMemberPointerTypeLoc(MemberPointerTypeLoc L) { 12358ce15f7eSEduardo Caldas // In the source code "void (Y::*mp)()" `MemberPointerTypeLoc` corresponds 12368ce15f7eSEduardo Caldas // to "Y::*" but it points to a `ParenTypeLoc` that corresponds to 12378ce15f7eSEduardo Caldas // "(Y::*mp)" We thus reverse the order of traversal to get the proper 12388ce15f7eSEduardo Caldas // syntax structure. 12398ce15f7eSEduardo Caldas if (!WalkUpFromMemberPointerTypeLoc(L)) 12408ce15f7eSEduardo Caldas return false; 12418ce15f7eSEduardo Caldas return TraverseTypeLoc(L.getPointeeLoc()); 12428ce15f7eSEduardo Caldas } 12438ce15f7eSEduardo Caldas 12447d382dcdSMarcel Hlopko bool WalkUpFromMemberPointerTypeLoc(MemberPointerTypeLoc L) { 12457d382dcdSMarcel Hlopko auto SR = L.getLocalSourceRange(); 1246a711a3a4SMarcel Hlopko Builder.foldNode(Builder.getRange(SR), 1247a711a3a4SMarcel Hlopko new (allocator()) syntax::MemberPointer, L); 12487d382dcdSMarcel Hlopko return true; 12497d382dcdSMarcel Hlopko } 12507d382dcdSMarcel Hlopko 125158fa50f4SIlya Biryukov // The code below is very regular, it could even be generated with some 125258fa50f4SIlya Biryukov // preprocessor magic. We merely assign roles to the corresponding children 125358fa50f4SIlya Biryukov // and fold resulting nodes. 125458fa50f4SIlya Biryukov bool WalkUpFromDeclStmt(DeclStmt *S) { 125558fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 1256a711a3a4SMarcel Hlopko new (allocator()) syntax::DeclarationStatement, S); 125758fa50f4SIlya Biryukov return true; 125858fa50f4SIlya Biryukov } 125958fa50f4SIlya Biryukov 126058fa50f4SIlya Biryukov bool WalkUpFromNullStmt(NullStmt *S) { 126158fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 1262a711a3a4SMarcel Hlopko new (allocator()) syntax::EmptyStatement, S); 126358fa50f4SIlya Biryukov return true; 126458fa50f4SIlya Biryukov } 126558fa50f4SIlya Biryukov 126658fa50f4SIlya Biryukov bool WalkUpFromSwitchStmt(SwitchStmt *S) { 1267def65bb4SIlya Biryukov Builder.markChildToken(S->getSwitchLoc(), 126858fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 126958fa50f4SIlya Biryukov Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 127058fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 1271a711a3a4SMarcel Hlopko new (allocator()) syntax::SwitchStatement, S); 127258fa50f4SIlya Biryukov return true; 127358fa50f4SIlya Biryukov } 127458fa50f4SIlya Biryukov 127558fa50f4SIlya Biryukov bool WalkUpFromCaseStmt(CaseStmt *S) { 1276def65bb4SIlya Biryukov Builder.markChildToken(S->getKeywordLoc(), 127758fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 127858fa50f4SIlya Biryukov Builder.markExprChild(S->getLHS(), syntax::NodeRole::CaseStatement_value); 127958fa50f4SIlya Biryukov Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement); 128058fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 1281a711a3a4SMarcel Hlopko new (allocator()) syntax::CaseStatement, S); 128258fa50f4SIlya Biryukov return true; 128358fa50f4SIlya Biryukov } 128458fa50f4SIlya Biryukov 128558fa50f4SIlya Biryukov bool WalkUpFromDefaultStmt(DefaultStmt *S) { 1286def65bb4SIlya Biryukov Builder.markChildToken(S->getKeywordLoc(), 128758fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 128858fa50f4SIlya Biryukov Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement); 128958fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 1290a711a3a4SMarcel Hlopko new (allocator()) syntax::DefaultStatement, S); 129158fa50f4SIlya Biryukov return true; 129258fa50f4SIlya Biryukov } 129358fa50f4SIlya Biryukov 129458fa50f4SIlya Biryukov bool WalkUpFromIfStmt(IfStmt *S) { 1295def65bb4SIlya Biryukov Builder.markChildToken(S->getIfLoc(), syntax::NodeRole::IntroducerKeyword); 129658fa50f4SIlya Biryukov Builder.markStmtChild(S->getThen(), 129758fa50f4SIlya Biryukov syntax::NodeRole::IfStatement_thenStatement); 1298def65bb4SIlya Biryukov Builder.markChildToken(S->getElseLoc(), 129958fa50f4SIlya Biryukov syntax::NodeRole::IfStatement_elseKeyword); 130058fa50f4SIlya Biryukov Builder.markStmtChild(S->getElse(), 130158fa50f4SIlya Biryukov syntax::NodeRole::IfStatement_elseStatement); 130258fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 1303a711a3a4SMarcel Hlopko new (allocator()) syntax::IfStatement, S); 130458fa50f4SIlya Biryukov return true; 130558fa50f4SIlya Biryukov } 130658fa50f4SIlya Biryukov 130758fa50f4SIlya Biryukov bool WalkUpFromForStmt(ForStmt *S) { 1308def65bb4SIlya Biryukov Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword); 130958fa50f4SIlya Biryukov Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 131058fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 1311a711a3a4SMarcel Hlopko new (allocator()) syntax::ForStatement, S); 131258fa50f4SIlya Biryukov return true; 131358fa50f4SIlya Biryukov } 131458fa50f4SIlya Biryukov 131558fa50f4SIlya Biryukov bool WalkUpFromWhileStmt(WhileStmt *S) { 1316def65bb4SIlya Biryukov Builder.markChildToken(S->getWhileLoc(), 131758fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 131858fa50f4SIlya Biryukov Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 131958fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 1320a711a3a4SMarcel Hlopko new (allocator()) syntax::WhileStatement, S); 132158fa50f4SIlya Biryukov return true; 132258fa50f4SIlya Biryukov } 132358fa50f4SIlya Biryukov 132458fa50f4SIlya Biryukov bool WalkUpFromContinueStmt(ContinueStmt *S) { 1325def65bb4SIlya Biryukov Builder.markChildToken(S->getContinueLoc(), 132658fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 132758fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 1328a711a3a4SMarcel Hlopko new (allocator()) syntax::ContinueStatement, S); 132958fa50f4SIlya Biryukov return true; 133058fa50f4SIlya Biryukov } 133158fa50f4SIlya Biryukov 133258fa50f4SIlya Biryukov bool WalkUpFromBreakStmt(BreakStmt *S) { 1333def65bb4SIlya Biryukov Builder.markChildToken(S->getBreakLoc(), 133458fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 133558fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 1336a711a3a4SMarcel Hlopko new (allocator()) syntax::BreakStatement, S); 133758fa50f4SIlya Biryukov return true; 133858fa50f4SIlya Biryukov } 133958fa50f4SIlya Biryukov 134058fa50f4SIlya Biryukov bool WalkUpFromReturnStmt(ReturnStmt *S) { 1341def65bb4SIlya Biryukov Builder.markChildToken(S->getReturnLoc(), 134258fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 134358fa50f4SIlya Biryukov Builder.markExprChild(S->getRetValue(), 134458fa50f4SIlya Biryukov syntax::NodeRole::ReturnStatement_value); 134558fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 1346a711a3a4SMarcel Hlopko new (allocator()) syntax::ReturnStatement, S); 134758fa50f4SIlya Biryukov return true; 134858fa50f4SIlya Biryukov } 134958fa50f4SIlya Biryukov 135058fa50f4SIlya Biryukov bool WalkUpFromCXXForRangeStmt(CXXForRangeStmt *S) { 1351def65bb4SIlya Biryukov Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword); 135258fa50f4SIlya Biryukov Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 135358fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 1354a711a3a4SMarcel Hlopko new (allocator()) syntax::RangeBasedForStatement, S); 135558fa50f4SIlya Biryukov return true; 135658fa50f4SIlya Biryukov } 135758fa50f4SIlya Biryukov 1358be14a22bSIlya Biryukov bool WalkUpFromEmptyDecl(EmptyDecl *S) { 1359cdce2fe5SMarcel Hlopko Builder.foldNode(Builder.getDeclarationRange(S), 1360a711a3a4SMarcel Hlopko new (allocator()) syntax::EmptyDeclaration, S); 1361be14a22bSIlya Biryukov return true; 1362be14a22bSIlya Biryukov } 1363be14a22bSIlya Biryukov 1364be14a22bSIlya Biryukov bool WalkUpFromStaticAssertDecl(StaticAssertDecl *S) { 1365be14a22bSIlya Biryukov Builder.markExprChild(S->getAssertExpr(), 1366be14a22bSIlya Biryukov syntax::NodeRole::StaticAssertDeclaration_condition); 1367be14a22bSIlya Biryukov Builder.markExprChild(S->getMessage(), 1368be14a22bSIlya Biryukov syntax::NodeRole::StaticAssertDeclaration_message); 1369cdce2fe5SMarcel Hlopko Builder.foldNode(Builder.getDeclarationRange(S), 1370a711a3a4SMarcel Hlopko new (allocator()) syntax::StaticAssertDeclaration, S); 1371be14a22bSIlya Biryukov return true; 1372be14a22bSIlya Biryukov } 1373be14a22bSIlya Biryukov 1374be14a22bSIlya Biryukov bool WalkUpFromLinkageSpecDecl(LinkageSpecDecl *S) { 1375cdce2fe5SMarcel Hlopko Builder.foldNode(Builder.getDeclarationRange(S), 1376a711a3a4SMarcel Hlopko new (allocator()) syntax::LinkageSpecificationDeclaration, 1377a711a3a4SMarcel Hlopko S); 1378be14a22bSIlya Biryukov return true; 1379be14a22bSIlya Biryukov } 1380be14a22bSIlya Biryukov 1381be14a22bSIlya Biryukov bool WalkUpFromNamespaceAliasDecl(NamespaceAliasDecl *S) { 1382cdce2fe5SMarcel Hlopko Builder.foldNode(Builder.getDeclarationRange(S), 1383a711a3a4SMarcel Hlopko new (allocator()) syntax::NamespaceAliasDefinition, S); 1384be14a22bSIlya Biryukov return true; 1385be14a22bSIlya Biryukov } 1386be14a22bSIlya Biryukov 1387be14a22bSIlya Biryukov bool WalkUpFromUsingDirectiveDecl(UsingDirectiveDecl *S) { 1388cdce2fe5SMarcel Hlopko Builder.foldNode(Builder.getDeclarationRange(S), 1389a711a3a4SMarcel Hlopko new (allocator()) syntax::UsingNamespaceDirective, S); 1390be14a22bSIlya Biryukov return true; 1391be14a22bSIlya Biryukov } 1392be14a22bSIlya Biryukov 1393be14a22bSIlya Biryukov bool WalkUpFromUsingDecl(UsingDecl *S) { 1394cdce2fe5SMarcel Hlopko Builder.foldNode(Builder.getDeclarationRange(S), 1395a711a3a4SMarcel Hlopko new (allocator()) syntax::UsingDeclaration, S); 1396be14a22bSIlya Biryukov return true; 1397be14a22bSIlya Biryukov } 1398be14a22bSIlya Biryukov 1399be14a22bSIlya Biryukov bool WalkUpFromUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *S) { 1400cdce2fe5SMarcel Hlopko Builder.foldNode(Builder.getDeclarationRange(S), 1401a711a3a4SMarcel Hlopko new (allocator()) syntax::UsingDeclaration, S); 1402be14a22bSIlya Biryukov return true; 1403be14a22bSIlya Biryukov } 1404be14a22bSIlya Biryukov 1405be14a22bSIlya Biryukov bool WalkUpFromUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *S) { 1406cdce2fe5SMarcel Hlopko Builder.foldNode(Builder.getDeclarationRange(S), 1407a711a3a4SMarcel Hlopko new (allocator()) syntax::UsingDeclaration, S); 1408be14a22bSIlya Biryukov return true; 1409be14a22bSIlya Biryukov } 1410be14a22bSIlya Biryukov 1411be14a22bSIlya Biryukov bool WalkUpFromTypeAliasDecl(TypeAliasDecl *S) { 1412cdce2fe5SMarcel Hlopko Builder.foldNode(Builder.getDeclarationRange(S), 1413a711a3a4SMarcel Hlopko new (allocator()) syntax::TypeAliasDeclaration, S); 1414be14a22bSIlya Biryukov return true; 1415be14a22bSIlya Biryukov } 1416be14a22bSIlya Biryukov 14179b3f38f9SIlya Biryukov private: 1418cdce2fe5SMarcel Hlopko template <class T> SourceLocation getQualifiedNameStart(T *D) { 1419cdce2fe5SMarcel Hlopko static_assert((std::is_base_of<DeclaratorDecl, T>::value || 1420cdce2fe5SMarcel Hlopko std::is_base_of<TypedefNameDecl, T>::value), 1421cdce2fe5SMarcel Hlopko "only DeclaratorDecl and TypedefNameDecl are supported."); 1422cdce2fe5SMarcel Hlopko 1423cdce2fe5SMarcel Hlopko auto DN = D->getDeclName(); 1424cdce2fe5SMarcel Hlopko bool IsAnonymous = DN.isIdentifier() && !DN.getAsIdentifierInfo(); 1425cdce2fe5SMarcel Hlopko if (IsAnonymous) 1426cdce2fe5SMarcel Hlopko return SourceLocation(); 1427cdce2fe5SMarcel Hlopko 1428ba41a0f7SEduardo Caldas if (const auto *DD = dyn_cast<DeclaratorDecl>(D)) { 1429cdce2fe5SMarcel Hlopko if (DD->getQualifierLoc()) { 1430cdce2fe5SMarcel Hlopko return DD->getQualifierLoc().getBeginLoc(); 1431cdce2fe5SMarcel Hlopko } 1432cdce2fe5SMarcel Hlopko } 1433cdce2fe5SMarcel Hlopko 1434cdce2fe5SMarcel Hlopko return D->getLocation(); 1435cdce2fe5SMarcel Hlopko } 1436cdce2fe5SMarcel Hlopko 1437cdce2fe5SMarcel Hlopko SourceRange getInitializerRange(Decl *D) { 1438ba41a0f7SEduardo Caldas if (auto *V = dyn_cast<VarDecl>(D)) { 1439cdce2fe5SMarcel Hlopko auto *I = V->getInit(); 1440cdce2fe5SMarcel Hlopko // Initializers in range-based-for are not part of the declarator 1441cdce2fe5SMarcel Hlopko if (I && !V->isCXXForRangeDecl()) 1442cdce2fe5SMarcel Hlopko return I->getSourceRange(); 1443cdce2fe5SMarcel Hlopko } 1444cdce2fe5SMarcel Hlopko 1445cdce2fe5SMarcel Hlopko return SourceRange(); 1446cdce2fe5SMarcel Hlopko } 1447cdce2fe5SMarcel Hlopko 1448cdce2fe5SMarcel Hlopko /// Folds SimpleDeclarator node (if present) and in case this is the last 1449cdce2fe5SMarcel Hlopko /// declarator in the chain it also folds SimpleDeclaration node. 1450cdce2fe5SMarcel Hlopko template <class T> bool processDeclaratorAndDeclaration(T *D) { 1451cdce2fe5SMarcel Hlopko SourceRange Initializer = getInitializerRange(D); 1452cdce2fe5SMarcel Hlopko auto Range = getDeclaratorRange(Builder.sourceManager(), 1453cdce2fe5SMarcel Hlopko D->getTypeSourceInfo()->getTypeLoc(), 1454cdce2fe5SMarcel Hlopko getQualifiedNameStart(D), Initializer); 1455cdce2fe5SMarcel Hlopko 1456cdce2fe5SMarcel Hlopko // There doesn't have to be a declarator (e.g. `void foo(int)` only has 1457cdce2fe5SMarcel Hlopko // declaration, but no declarator). 1458cdce2fe5SMarcel Hlopko if (Range.getBegin().isValid()) { 1459cdce2fe5SMarcel Hlopko auto *N = new (allocator()) syntax::SimpleDeclarator; 1460cdce2fe5SMarcel Hlopko Builder.foldNode(Builder.getRange(Range), N, nullptr); 1461cdce2fe5SMarcel Hlopko Builder.markChild(N, syntax::NodeRole::SimpleDeclaration_declarator); 1462cdce2fe5SMarcel Hlopko } 1463cdce2fe5SMarcel Hlopko 1464cdce2fe5SMarcel Hlopko if (Builder.isResponsibleForCreatingDeclaration(D)) { 1465cdce2fe5SMarcel Hlopko Builder.foldNode(Builder.getDeclarationRange(D), 1466cdce2fe5SMarcel Hlopko new (allocator()) syntax::SimpleDeclaration, D); 1467cdce2fe5SMarcel Hlopko } 1468cdce2fe5SMarcel Hlopko return true; 1469cdce2fe5SMarcel Hlopko } 1470cdce2fe5SMarcel Hlopko 14717d382dcdSMarcel Hlopko /// Returns the range of the built node. 1472a711a3a4SMarcel Hlopko syntax::TrailingReturnType *BuildTrailingReturn(FunctionProtoTypeLoc L) { 14737d382dcdSMarcel Hlopko assert(L.getTypePtr()->hasTrailingReturn()); 14747d382dcdSMarcel Hlopko 14757d382dcdSMarcel Hlopko auto ReturnedType = L.getReturnLoc(); 14767d382dcdSMarcel Hlopko // Build node for the declarator, if any. 14777d382dcdSMarcel Hlopko auto ReturnDeclaratorRange = 14787d382dcdSMarcel Hlopko getDeclaratorRange(this->Builder.sourceManager(), ReturnedType, 14797d382dcdSMarcel Hlopko /*Name=*/SourceLocation(), 14807d382dcdSMarcel Hlopko /*Initializer=*/SourceLocation()); 1481a711a3a4SMarcel Hlopko syntax::SimpleDeclarator *ReturnDeclarator = nullptr; 14827d382dcdSMarcel Hlopko if (ReturnDeclaratorRange.isValid()) { 1483a711a3a4SMarcel Hlopko ReturnDeclarator = new (allocator()) syntax::SimpleDeclarator; 1484a711a3a4SMarcel Hlopko Builder.foldNode(Builder.getRange(ReturnDeclaratorRange), 1485a711a3a4SMarcel Hlopko ReturnDeclarator, nullptr); 14867d382dcdSMarcel Hlopko } 14877d382dcdSMarcel Hlopko 14887d382dcdSMarcel Hlopko // Build node for trailing return type. 1489a711a3a4SMarcel Hlopko auto Return = Builder.getRange(ReturnedType.getSourceRange()); 14907d382dcdSMarcel Hlopko const auto *Arrow = Return.begin() - 1; 14917d382dcdSMarcel Hlopko assert(Arrow->kind() == tok::arrow); 14927d382dcdSMarcel Hlopko auto Tokens = llvm::makeArrayRef(Arrow, Return.end()); 149342f6fec3SEduardo Caldas Builder.markChildToken(Arrow, syntax::NodeRole::ArrowToken); 1494a711a3a4SMarcel Hlopko if (ReturnDeclarator) 1495a711a3a4SMarcel Hlopko Builder.markChild(ReturnDeclarator, 14967d382dcdSMarcel Hlopko syntax::NodeRole::TrailingReturnType_declarator); 1497a711a3a4SMarcel Hlopko auto *R = new (allocator()) syntax::TrailingReturnType; 1498cdce2fe5SMarcel Hlopko Builder.foldNode(Tokens, R, L); 1499a711a3a4SMarcel Hlopko return R; 15007d382dcdSMarcel Hlopko } 150188bf9b3dSMarcel Hlopko 1502a711a3a4SMarcel Hlopko void foldExplicitTemplateInstantiation( 1503a711a3a4SMarcel Hlopko ArrayRef<syntax::Token> Range, const syntax::Token *ExternKW, 150488bf9b3dSMarcel Hlopko const syntax::Token *TemplateKW, 1505a711a3a4SMarcel Hlopko syntax::SimpleDeclaration *InnerDeclaration, Decl *From) { 150688bf9b3dSMarcel Hlopko assert(!ExternKW || ExternKW->kind() == tok::kw_extern); 150788bf9b3dSMarcel Hlopko assert(TemplateKW && TemplateKW->kind() == tok::kw_template); 150842f6fec3SEduardo Caldas Builder.markChildToken(ExternKW, syntax::NodeRole::ExternKeyword); 150988bf9b3dSMarcel Hlopko Builder.markChildToken(TemplateKW, syntax::NodeRole::IntroducerKeyword); 151088bf9b3dSMarcel Hlopko Builder.markChild( 151188bf9b3dSMarcel Hlopko InnerDeclaration, 151288bf9b3dSMarcel Hlopko syntax::NodeRole::ExplicitTemplateInstantiation_declaration); 1513a711a3a4SMarcel Hlopko Builder.foldNode( 1514a711a3a4SMarcel Hlopko Range, new (allocator()) syntax::ExplicitTemplateInstantiation, From); 151588bf9b3dSMarcel Hlopko } 151688bf9b3dSMarcel Hlopko 1517a711a3a4SMarcel Hlopko syntax::TemplateDeclaration *foldTemplateDeclaration( 1518a711a3a4SMarcel Hlopko ArrayRef<syntax::Token> Range, const syntax::Token *TemplateKW, 1519a711a3a4SMarcel Hlopko ArrayRef<syntax::Token> TemplatedDeclaration, Decl *From) { 152088bf9b3dSMarcel Hlopko assert(TemplateKW && TemplateKW->kind() == tok::kw_template); 152188bf9b3dSMarcel Hlopko Builder.markChildToken(TemplateKW, syntax::NodeRole::IntroducerKeyword); 1522a711a3a4SMarcel Hlopko 1523a711a3a4SMarcel Hlopko auto *N = new (allocator()) syntax::TemplateDeclaration; 1524a711a3a4SMarcel Hlopko Builder.foldNode(Range, N, From); 1525cdce2fe5SMarcel Hlopko Builder.markChild(N, syntax::NodeRole::TemplateDeclaration_declaration); 1526a711a3a4SMarcel Hlopko return N; 152788bf9b3dSMarcel Hlopko } 152888bf9b3dSMarcel Hlopko 15299b3f38f9SIlya Biryukov /// A small helper to save some typing. 15309b3f38f9SIlya Biryukov llvm::BumpPtrAllocator &allocator() { return Builder.allocator(); } 15319b3f38f9SIlya Biryukov 15329b3f38f9SIlya Biryukov syntax::TreeBuilder &Builder; 15331db5b348SEduardo Caldas const ASTContext &Context; 15349b3f38f9SIlya Biryukov }; 15359b3f38f9SIlya Biryukov } // namespace 15369b3f38f9SIlya Biryukov 15377d382dcdSMarcel Hlopko void syntax::TreeBuilder::noticeDeclWithoutSemicolon(Decl *D) { 1538e702bdb8SIlya Biryukov DeclsWithoutSemicolons.insert(D); 1539e702bdb8SIlya Biryukov } 1540e702bdb8SIlya Biryukov 1541def65bb4SIlya Biryukov void syntax::TreeBuilder::markChildToken(SourceLocation Loc, NodeRole Role) { 15429b3f38f9SIlya Biryukov if (Loc.isInvalid()) 15439b3f38f9SIlya Biryukov return; 15449b3f38f9SIlya Biryukov Pending.assignRole(*findToken(Loc), Role); 15459b3f38f9SIlya Biryukov } 15469b3f38f9SIlya Biryukov 15477d382dcdSMarcel Hlopko void syntax::TreeBuilder::markChildToken(const syntax::Token *T, NodeRole R) { 15487d382dcdSMarcel Hlopko if (!T) 15497d382dcdSMarcel Hlopko return; 15507d382dcdSMarcel Hlopko Pending.assignRole(*T, R); 15517d382dcdSMarcel Hlopko } 15527d382dcdSMarcel Hlopko 1553a711a3a4SMarcel Hlopko void syntax::TreeBuilder::markChild(syntax::Node *N, NodeRole R) { 1554a711a3a4SMarcel Hlopko assert(N); 1555a711a3a4SMarcel Hlopko setRole(N, R); 1556a711a3a4SMarcel Hlopko } 1557a711a3a4SMarcel Hlopko 1558a711a3a4SMarcel Hlopko void syntax::TreeBuilder::markChild(ASTPtr N, NodeRole R) { 1559a711a3a4SMarcel Hlopko auto *SN = Mapping.find(N); 1560a711a3a4SMarcel Hlopko assert(SN != nullptr); 1561a711a3a4SMarcel Hlopko setRole(SN, R); 15627d382dcdSMarcel Hlopko } 1563f9500cc4SEduardo Caldas void syntax::TreeBuilder::markChild(NestedNameSpecifierLoc NNSLoc, NodeRole R) { 1564f9500cc4SEduardo Caldas auto *SN = Mapping.find(NNSLoc); 1565f9500cc4SEduardo Caldas assert(SN != nullptr); 1566f9500cc4SEduardo Caldas setRole(SN, R); 1567f9500cc4SEduardo Caldas } 15687d382dcdSMarcel Hlopko 156958fa50f4SIlya Biryukov void syntax::TreeBuilder::markStmtChild(Stmt *Child, NodeRole Role) { 157058fa50f4SIlya Biryukov if (!Child) 157158fa50f4SIlya Biryukov return; 157258fa50f4SIlya Biryukov 1573b34b7691SDmitri Gribenko syntax::Tree *ChildNode; 1574b34b7691SDmitri Gribenko if (Expr *ChildExpr = dyn_cast<Expr>(Child)) { 157558fa50f4SIlya Biryukov // This is an expression in a statement position, consume the trailing 157658fa50f4SIlya Biryukov // semicolon and form an 'ExpressionStatement' node. 1577b34b7691SDmitri Gribenko markExprChild(ChildExpr, NodeRole::ExpressionStatement_expression); 1578a711a3a4SMarcel Hlopko ChildNode = new (allocator()) syntax::ExpressionStatement; 1579a711a3a4SMarcel Hlopko // (!) 'getStmtRange()' ensures this covers a trailing semicolon. 1580a711a3a4SMarcel Hlopko Pending.foldChildren(Arena, getStmtRange(Child), ChildNode); 1581b34b7691SDmitri Gribenko } else { 1582b34b7691SDmitri Gribenko ChildNode = Mapping.find(Child); 158358fa50f4SIlya Biryukov } 1584b34b7691SDmitri Gribenko assert(ChildNode != nullptr); 1585a711a3a4SMarcel Hlopko setRole(ChildNode, Role); 158658fa50f4SIlya Biryukov } 158758fa50f4SIlya Biryukov 158858fa50f4SIlya Biryukov void syntax::TreeBuilder::markExprChild(Expr *Child, NodeRole Role) { 1589be14a22bSIlya Biryukov if (!Child) 1590be14a22bSIlya Biryukov return; 1591a711a3a4SMarcel Hlopko Child = Child->IgnoreImplicit(); 1592be14a22bSIlya Biryukov 1593a711a3a4SMarcel Hlopko syntax::Tree *ChildNode = Mapping.find(Child); 1594a711a3a4SMarcel Hlopko assert(ChildNode != nullptr); 1595a711a3a4SMarcel Hlopko setRole(ChildNode, Role); 159658fa50f4SIlya Biryukov } 159758fa50f4SIlya Biryukov 15989b3f38f9SIlya Biryukov const syntax::Token *syntax::TreeBuilder::findToken(SourceLocation L) const { 159988bf9b3dSMarcel Hlopko if (L.isInvalid()) 160088bf9b3dSMarcel Hlopko return nullptr; 1601c1bbefefSIlya Biryukov auto It = LocationToToken.find(L.getRawEncoding()); 1602c1bbefefSIlya Biryukov assert(It != LocationToToken.end()); 1603c1bbefefSIlya Biryukov return It->second; 16049b3f38f9SIlya Biryukov } 16059b3f38f9SIlya Biryukov 16069b3f38f9SIlya Biryukov syntax::TranslationUnit * 16079b3f38f9SIlya Biryukov syntax::buildSyntaxTree(Arena &A, const TranslationUnitDecl &TU) { 16089b3f38f9SIlya Biryukov TreeBuilder Builder(A); 16099b3f38f9SIlya Biryukov BuildTreeVisitor(TU.getASTContext(), Builder).TraverseAST(TU.getASTContext()); 16109b3f38f9SIlya Biryukov return std::move(Builder).finalize(); 16119b3f38f9SIlya Biryukov } 1612