blob: ff02f9a31cb299c78f0beab3d6140d1a37c38837 [file] [log] [blame]
John McCall275c10a2009-10-29 07:48:15 +00001//===--- 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 McCall833ca992009-10-29 08:12:44 +000019#include "clang/AST/TypeLoc.h"
John McCall275c10a2009-10-29 07:48:15 +000020
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// TemplateArgument Implementation
25//===----------------------------------------------------------------------===//
26
John McCall275c10a2009-10-29 07:48:15 +000027/// \brief Construct a template argument pack.
28void 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
46void 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 Gregor788cd062009-11-11 01:00:40 +000061 case Template:
Douglas Gregor7bb87fc2009-11-11 16:39:34 +000062 ID.AddPointer(Context.getCanonicalTemplateName(getAsTemplate())
63 .getAsVoidPointer());
Douglas Gregor788cd062009-11-11 01:00:40 +000064 break;
65
John McCall275c10a2009-10-29 07:48:15 +000066 case Integral:
67 getAsIntegral()->Profile(ID);
68 getIntegralType().Profile(ID);
69 break;
70
71 case Expression:
72 getAsExpr()->Profile(ID, Context, true);
73 break;
74
75 case Pack:
76 ID.AddInteger(Args.NumArgs);
77 for (unsigned I = 0; I != Args.NumArgs; ++I)
78 Args.Args[I].Profile(ID, Context);
79 }
80}
John McCall833ca992009-10-29 08:12:44 +000081
82//===----------------------------------------------------------------------===//
83// TemplateArgumentLoc Implementation
84//===----------------------------------------------------------------------===//
85
John McCall828bff22009-10-29 18:45:58 +000086SourceRange TemplateArgumentLoc::getSourceRange() const {
John McCall833ca992009-10-29 08:12:44 +000087 switch (Argument.getKind()) {
88 case TemplateArgument::Expression:
John McCall828bff22009-10-29 18:45:58 +000089 return getSourceExpression()->getSourceRange();
Douglas Gregor788cd062009-11-11 01:00:40 +000090
John McCall833ca992009-10-29 08:12:44 +000091 case TemplateArgument::Declaration:
John McCall828bff22009-10-29 18:45:58 +000092 return getSourceDeclExpression()->getSourceRange();
Douglas Gregor788cd062009-11-11 01:00:40 +000093
John McCall828bff22009-10-29 18:45:58 +000094 case TemplateArgument::Type:
95 return getSourceDeclaratorInfo()->getTypeLoc().getFullSourceRange();
Douglas Gregor788cd062009-11-11 01:00:40 +000096
97 case TemplateArgument::Template:
98 if (getTemplateQualifierRange().isValid())
99 return SourceRange(getTemplateQualifierRange().getBegin(),
100 getTemplateNameLoc());
101 return SourceRange(getTemplateNameLoc());
102
John McCall833ca992009-10-29 08:12:44 +0000103 case TemplateArgument::Integral:
104 case TemplateArgument::Pack:
105 case TemplateArgument::Null:
John McCall828bff22009-10-29 18:45:58 +0000106 return SourceRange();
John McCall833ca992009-10-29 08:12:44 +0000107 }
108
109 // Silence bonus gcc warning.
John McCall828bff22009-10-29 18:45:58 +0000110 return SourceRange();
John McCall833ca992009-10-29 08:12:44 +0000111}