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 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 61 | case Template: |
| 62 | ID.AddPointer(getAsTemplate().getAsVoidPointer()); |
| 63 | break; |
| 64 | |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 65 | case Integral: |
| 66 | getAsIntegral()->Profile(ID); |
| 67 | getIntegralType().Profile(ID); |
| 68 | break; |
| 69 | |
| 70 | case Expression: |
| 71 | getAsExpr()->Profile(ID, Context, true); |
| 72 | break; |
| 73 | |
| 74 | case Pack: |
| 75 | ID.AddInteger(Args.NumArgs); |
| 76 | for (unsigned I = 0; I != Args.NumArgs; ++I) |
| 77 | Args.Args[I].Profile(ID, Context); |
| 78 | } |
| 79 | } |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 80 | |
| 81 | //===----------------------------------------------------------------------===// |
| 82 | // TemplateArgumentLoc Implementation |
| 83 | //===----------------------------------------------------------------------===// |
| 84 | |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 85 | SourceRange TemplateArgumentLoc::getSourceRange() const { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 86 | switch (Argument.getKind()) { |
| 87 | case TemplateArgument::Expression: |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 88 | return getSourceExpression()->getSourceRange(); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 89 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 90 | case TemplateArgument::Declaration: |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 91 | return getSourceDeclExpression()->getSourceRange(); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 92 | |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 93 | case TemplateArgument::Type: |
| 94 | return getSourceDeclaratorInfo()->getTypeLoc().getFullSourceRange(); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 95 | |
| 96 | case TemplateArgument::Template: |
| 97 | if (getTemplateQualifierRange().isValid()) |
| 98 | return SourceRange(getTemplateQualifierRange().getBegin(), |
| 99 | getTemplateNameLoc()); |
| 100 | return SourceRange(getTemplateNameLoc()); |
| 101 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 102 | case TemplateArgument::Integral: |
| 103 | case TemplateArgument::Pack: |
| 104 | case TemplateArgument::Null: |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 105 | return SourceRange(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Silence bonus gcc warning. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 109 | return SourceRange(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 110 | } |