blob: ebd07f486783272b48940b33692855b68a662a52 [file] [log] [blame]
Douglas Gregor7532dc62009-03-30 22:58:21 +00001//===--- TemplateName.h - C++ Template Name Representation-------*- C++ -*-===//
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 defines the TemplateName interface and subclasses.
11//
12//===----------------------------------------------------------------------===//
Chris Lattnerc960ee32009-04-02 06:07:12 +000013
Douglas Gregor7532dc62009-03-30 22:58:21 +000014#include "clang/AST/TemplateName.h"
Douglas Gregor1aee05d2011-01-15 06:45:20 +000015#include "clang/AST/TemplateBase.h"
Douglas Gregor7532dc62009-03-30 22:58:21 +000016#include "clang/AST/DeclTemplate.h"
17#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000018#include "clang/AST/PrettyPrinter.h"
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +000019#include "clang/Basic/Diagnostic.h"
Chris Lattnere4f21422009-06-30 01:26:17 +000020#include "clang/Basic/LangOptions.h"
Douglas Gregor7532dc62009-03-30 22:58:21 +000021#include "llvm/Support/raw_ostream.h"
Douglas Gregor7532dc62009-03-30 22:58:21 +000022using namespace clang;
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +000023using namespace llvm;
Douglas Gregor7532dc62009-03-30 22:58:21 +000024
Douglas Gregor1aee05d2011-01-15 06:45:20 +000025TemplateArgument
26SubstTemplateTemplateParmPackStorage::getArgumentPack() const {
27 return TemplateArgument(Arguments, size());
28}
29
30void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID) {
31 Profile(ID, Context, Parameter, TemplateArgument(Arguments, size()));
32}
33
34void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
35 ASTContext &Context,
36 TemplateTemplateParmDecl *Parameter,
37 const TemplateArgument &ArgPack) {
38 ID.AddPointer(Parameter);
39 ArgPack.Profile(ID, Context);
40}
41
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +000042TemplateName::NameKind TemplateName::getKind() const {
43 if (Storage.is<TemplateDecl *>())
44 return Template;
Douglas Gregor1aee05d2011-01-15 06:45:20 +000045 if (Storage.is<DependentTemplateName *>())
46 return DependentTemplate;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +000047 if (Storage.is<QualifiedTemplateName *>())
48 return QualifiedTemplate;
Douglas Gregor1aee05d2011-01-15 06:45:20 +000049
50 return getAsOverloadedTemplate()? OverloadedTemplate
51 : SubstTemplateTemplateParmPack;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +000052}
53
Douglas Gregor7532dc62009-03-30 22:58:21 +000054TemplateDecl *TemplateName::getAsTemplateDecl() const {
55 if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
56 return Template;
Mike Stump1eb44332009-09-09 15:08:12 +000057
Douglas Gregor7532dc62009-03-30 22:58:21 +000058 if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
59 return QTN->getTemplateDecl();
60
61 return 0;
62}
63
64bool TemplateName::isDependent() const {
65 if (TemplateDecl *Template = getAsTemplateDecl()) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +000066 if (isa<TemplateTemplateParmDecl>(Template))
67 return true;
68 // FIXME: Hack, getDeclContext() can be null if Template is still
69 // initializing due to PCH reading, so we check it before using it.
70 // Should probably modify TemplateSpecializationType to allow constructing
71 // it without the isDependent() checking.
72 return Template->getDeclContext() &&
73 Template->getDeclContext()->isDependentContext();
Douglas Gregor7532dc62009-03-30 22:58:21 +000074 }
75
John McCall0bd6feb2009-12-02 08:04:21 +000076 assert(!getAsOverloadedTemplate() &&
77 "overloaded templates shouldn't survive to here");
Mike Stump1eb44332009-09-09 15:08:12 +000078
Douglas Gregor7532dc62009-03-30 22:58:21 +000079 return true;
80}
81
Douglas Gregord0937222010-12-13 22:49:22 +000082bool TemplateName::containsUnexpandedParameterPack() const {
83 if (TemplateDecl *Template = getAsTemplateDecl()) {
84 if (TemplateTemplateParmDecl *TTP
85 = dyn_cast<TemplateTemplateParmDecl>(Template))
86 return TTP->isParameterPack();
87
88 return false;
89 }
90
91 if (DependentTemplateName *DTN = getAsDependentTemplateName())
92 return DTN->getQualifier() &&
93 DTN->getQualifier()->containsUnexpandedParameterPack();
94
Douglas Gregor1aee05d2011-01-15 06:45:20 +000095 return getAsSubstTemplateTemplateParmPack() != 0;
Douglas Gregord0937222010-12-13 22:49:22 +000096}
97
Mike Stump1eb44332009-09-09 15:08:12 +000098void
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000099TemplateName::print(llvm::raw_ostream &OS, const PrintingPolicy &Policy,
100 bool SuppressNNS) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +0000101 if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000102 OS << Template;
Douglas Gregor7532dc62009-03-30 22:58:21 +0000103 else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
Douglas Gregor17343172009-04-01 00:28:59 +0000104 if (!SuppressNNS)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000105 QTN->getQualifier()->print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +0000106 if (QTN->hasTemplateKeyword())
107 OS << "template ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000108 OS << QTN->getDecl();
Douglas Gregor7532dc62009-03-30 22:58:21 +0000109 } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000110 if (!SuppressNNS && DTN->getQualifier())
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000111 DTN->getQualifier()->print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +0000112 OS << "template ";
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000113
114 if (DTN->isIdentifier())
115 OS << DTN->getIdentifier()->getName();
116 else
117 OS << "operator " << getOperatorSpelling(DTN->getOperator());
Douglas Gregor1aee05d2011-01-15 06:45:20 +0000118 } else if (SubstTemplateTemplateParmPackStorage *SubstPack
119 = getAsSubstTemplateTemplateParmPack())
120 OS << SubstPack->getParameterPack()->getNameAsString();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000121 else {
122 OverloadedTemplateStorage *OTS = getAsOverloadedTemplate();
123 (*OTS->begin())->printName(OS);
124 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000125}
Douglas Gregorde650ae2009-03-31 18:38:02 +0000126
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +0000127const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
128 TemplateName N) {
129 std::string NameStr;
130 raw_string_ostream OS(NameStr);
131 LangOptions LO;
132 LO.CPlusPlus = true;
133 LO.Bool = true;
134 N.print(OS, PrintingPolicy(LO));
135 OS.flush();
136 return DB << NameStr;
137}
138
Douglas Gregor9bde7732009-03-31 20:22:05 +0000139void TemplateName::dump() const {
Chris Lattnere4f21422009-06-30 01:26:17 +0000140 LangOptions LO; // FIXME!
141 LO.CPlusPlus = true;
142 LO.Bool = true;
143 print(llvm::errs(), PrintingPolicy(LO));
Douglas Gregorde650ae2009-03-31 18:38:02 +0000144}