blob: 73ff402b074e8612a9d4dca405ed8762da7272a5 [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"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000017#include "clang/AST/PrettyPrinter.h"
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattnere4f21422009-06-30 01:26:17 +000019#include "clang/Basic/LangOptions.h"
Douglas Gregor7532dc62009-03-30 22:58:21 +000020#include "llvm/Support/raw_ostream.h"
Douglas Gregor7532dc62009-03-30 22:58:21 +000021using namespace clang;
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +000022using namespace llvm;
Douglas Gregor7532dc62009-03-30 22:58:21 +000023
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +000024TemplateName::NameKind TemplateName::getKind() const {
25 if (Storage.is<TemplateDecl *>())
26 return Template;
27 if (Storage.is<OverloadedTemplateStorage *>())
28 return OverloadedTemplate;
29 if (Storage.is<QualifiedTemplateName *>())
30 return QualifiedTemplate;
31 assert(Storage.is<DependentTemplateName *>() && "There's a case unhandled!");
32 return DependentTemplate;
33}
34
Douglas Gregor7532dc62009-03-30 22:58:21 +000035TemplateDecl *TemplateName::getAsTemplateDecl() const {
36 if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
37 return Template;
Mike Stump1eb44332009-09-09 15:08:12 +000038
Douglas Gregor7532dc62009-03-30 22:58:21 +000039 if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
40 return QTN->getTemplateDecl();
41
42 return 0;
43}
44
45bool TemplateName::isDependent() const {
46 if (TemplateDecl *Template = getAsTemplateDecl()) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +000047 if (isa<TemplateTemplateParmDecl>(Template))
48 return true;
49 // FIXME: Hack, getDeclContext() can be null if Template is still
50 // initializing due to PCH reading, so we check it before using it.
51 // Should probably modify TemplateSpecializationType to allow constructing
52 // it without the isDependent() checking.
53 return Template->getDeclContext() &&
54 Template->getDeclContext()->isDependentContext();
Douglas Gregor7532dc62009-03-30 22:58:21 +000055 }
56
John McCall0bd6feb2009-12-02 08:04:21 +000057 assert(!getAsOverloadedTemplate() &&
58 "overloaded templates shouldn't survive to here");
Mike Stump1eb44332009-09-09 15:08:12 +000059
Douglas Gregor7532dc62009-03-30 22:58:21 +000060 return true;
61}
62
Douglas Gregord0937222010-12-13 22:49:22 +000063bool TemplateName::containsUnexpandedParameterPack() const {
64 if (TemplateDecl *Template = getAsTemplateDecl()) {
65 if (TemplateTemplateParmDecl *TTP
66 = dyn_cast<TemplateTemplateParmDecl>(Template))
67 return TTP->isParameterPack();
68
69 return false;
70 }
71
72 if (DependentTemplateName *DTN = getAsDependentTemplateName())
73 return DTN->getQualifier() &&
74 DTN->getQualifier()->containsUnexpandedParameterPack();
75
76 return false;
77}
78
Mike Stump1eb44332009-09-09 15:08:12 +000079void
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000080TemplateName::print(llvm::raw_ostream &OS, const PrintingPolicy &Policy,
81 bool SuppressNNS) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +000082 if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
Benjamin Kramer900fc632010-04-17 09:33:03 +000083 OS << Template;
Douglas Gregor7532dc62009-03-30 22:58:21 +000084 else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
Douglas Gregor17343172009-04-01 00:28:59 +000085 if (!SuppressNNS)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000086 QTN->getQualifier()->print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +000087 if (QTN->hasTemplateKeyword())
88 OS << "template ";
Benjamin Kramer900fc632010-04-17 09:33:03 +000089 OS << QTN->getDecl();
Douglas Gregor7532dc62009-03-30 22:58:21 +000090 } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000091 if (!SuppressNNS && DTN->getQualifier())
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000092 DTN->getQualifier()->print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +000093 OS << "template ";
Douglas Gregorca1bdd72009-11-04 00:56:37 +000094
95 if (DTN->isIdentifier())
96 OS << DTN->getIdentifier()->getName();
97 else
98 OS << "operator " << getOperatorSpelling(DTN->getOperator());
Douglas Gregor7532dc62009-03-30 22:58:21 +000099 }
100}
Douglas Gregorde650ae2009-03-31 18:38:02 +0000101
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +0000102const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
103 TemplateName N) {
104 std::string NameStr;
105 raw_string_ostream OS(NameStr);
106 LangOptions LO;
107 LO.CPlusPlus = true;
108 LO.Bool = true;
109 N.print(OS, PrintingPolicy(LO));
110 OS.flush();
111 return DB << NameStr;
112}
113
Douglas Gregor9bde7732009-03-31 20:22:05 +0000114void TemplateName::dump() const {
Chris Lattnere4f21422009-06-30 01:26:17 +0000115 LangOptions LO; // FIXME!
116 LO.CPlusPlus = true;
117 LO.Bool = true;
118 print(llvm::errs(), PrintingPolicy(LO));
Douglas Gregorde650ae2009-03-31 18:38:02 +0000119}