John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 1 | //===--- TemplateBase.cpp - Common template AST class implementation ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements common classes used throughout C++ template |
| 11 | // representations. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/ADT/FoldingSet.h" |
| 16 | #include "clang/AST/TemplateBase.h" |
| 17 | #include "clang/AST/DeclBase.h" |
| 18 | #include "clang/AST/Expr.h" |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame^] | 19 | #include "clang/AST/TypeLoc.h" |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // TemplateArgument Implementation |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 27 | /// \brief Construct a template argument pack. |
| 28 | void TemplateArgument::setArgumentPack(TemplateArgument *args, unsigned NumArgs, |
| 29 | bool CopyArgs) { |
| 30 | assert(isNull() && "Must call setArgumentPack on a null argument"); |
| 31 | |
| 32 | Kind = Pack; |
| 33 | Args.NumArgs = NumArgs; |
| 34 | Args.CopyArgs = CopyArgs; |
| 35 | if (!Args.CopyArgs) { |
| 36 | Args.Args = args; |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | // FIXME: Allocate in ASTContext |
| 41 | Args.Args = new TemplateArgument[NumArgs]; |
| 42 | for (unsigned I = 0; I != Args.NumArgs; ++I) |
| 43 | Args.Args[I] = args[I]; |
| 44 | } |
| 45 | |
| 46 | void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID, |
| 47 | ASTContext &Context) const { |
| 48 | ID.AddInteger(Kind); |
| 49 | switch (Kind) { |
| 50 | case Null: |
| 51 | break; |
| 52 | |
| 53 | case Type: |
| 54 | getAsType().Profile(ID); |
| 55 | break; |
| 56 | |
| 57 | case Declaration: |
| 58 | ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : 0); |
| 59 | break; |
| 60 | |
| 61 | case Integral: |
| 62 | getAsIntegral()->Profile(ID); |
| 63 | getIntegralType().Profile(ID); |
| 64 | break; |
| 65 | |
| 66 | case Expression: |
| 67 | getAsExpr()->Profile(ID, Context, true); |
| 68 | break; |
| 69 | |
| 70 | case Pack: |
| 71 | ID.AddInteger(Args.NumArgs); |
| 72 | for (unsigned I = 0; I != Args.NumArgs; ++I) |
| 73 | Args.Args[I].Profile(ID, Context); |
| 74 | } |
| 75 | } |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame^] | 76 | |
| 77 | //===----------------------------------------------------------------------===// |
| 78 | // TemplateArgumentLoc Implementation |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | |
| 81 | SourceLocation TemplateArgumentLoc::getLocation() const { |
| 82 | switch (Argument.getKind()) { |
| 83 | case TemplateArgument::Expression: |
| 84 | return getSourceExpression()->getExprLoc(); |
| 85 | case TemplateArgument::Type: |
| 86 | return getSourceDeclaratorInfo()-> |
| 87 | getTypeLoc().getFullSourceRange().getBegin(); |
| 88 | case TemplateArgument::Declaration: |
| 89 | case TemplateArgument::Integral: |
| 90 | case TemplateArgument::Pack: |
| 91 | case TemplateArgument::Null: |
| 92 | return SourceLocation(); |
| 93 | } |
| 94 | |
| 95 | // Silence bonus gcc warning. |
| 96 | return SourceLocation(); |
| 97 | } |