blob: b3c44f6d032d79fe3a4dbd7d5037cfd2f3a12557 [file] [log] [blame]
Eugene Zelenko21fadad2017-11-21 23:26:08 +00001//===- TemplateName.cpp - C++ Template Name Representation ----------------===//
Douglas Gregordc572a32009-03-30 22:58:21 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregordc572a32009-03-30 22:58:21 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the TemplateName interface and subclasses.
10//
11//===----------------------------------------------------------------------===//
Chris Lattner098d94a2009-04-02 06:07:12 +000012
Douglas Gregordc572a32009-03-30 22:58:21 +000013#include "clang/AST/TemplateName.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000014#include "clang/AST/DeclBase.h"
Douglas Gregordc572a32009-03-30 22:58:21 +000015#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000017#include "clang/AST/PrettyPrinter.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/TemplateBase.h"
Jeffrey Yasskin823015d2010-04-08 00:03:06 +000019#include "clang/Basic/Diagnostic.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000020#include "clang/Basic/LLVM.h"
Chris Lattnerc61089a2009-06-30 01:26:17 +000021#include "clang/Basic/LangOptions.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000022#include "clang/Basic/OperatorKinds.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/FoldingSet.h"
25#include "llvm/Support/Casting.h"
26#include "llvm/Support/Compiler.h"
Douglas Gregordc572a32009-03-30 22:58:21 +000027#include "llvm/Support/raw_ostream.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000028#include <cassert>
29#include <string>
30
Douglas Gregordc572a32009-03-30 22:58:21 +000031using namespace clang;
32
Fangrui Song6907ce22018-07-30 19:24:48 +000033TemplateArgument
Douglas Gregor5590be02011-01-15 06:45:20 +000034SubstTemplateTemplateParmPackStorage::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +000035 return TemplateArgument(llvm::makeArrayRef(Arguments, size()));
Douglas Gregor5590be02011-01-15 06:45:20 +000036}
37
John McCalld9dfe3a2011-06-30 08:33:18 +000038void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID) {
39 Profile(ID, Parameter, Replacement);
40}
41
Fangrui Song6907ce22018-07-30 19:24:48 +000042void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID,
John McCalld9dfe3a2011-06-30 08:33:18 +000043 TemplateTemplateParmDecl *parameter,
44 TemplateName replacement) {
45 ID.AddPointer(parameter);
46 ID.AddPointer(replacement.getAsVoidPointer());
47}
48
49void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
50 ASTContext &Context) {
Benjamin Kramercce63472015-08-05 09:40:22 +000051 Profile(ID, Context, Parameter, getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +000052}
53
Fangrui Song6907ce22018-07-30 19:24:48 +000054void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor5590be02011-01-15 06:45:20 +000055 ASTContext &Context,
56 TemplateTemplateParmDecl *Parameter,
57 const TemplateArgument &ArgPack) {
58 ID.AddPointer(Parameter);
59 ArgPack.Profile(ID, Context);
60}
61
Chandler Carruthbd452fb2015-12-30 06:21:02 +000062TemplateName::TemplateName(void *Ptr) {
63 Storage = StorageType::getFromOpaqueValue(Ptr);
64}
65
Chandler Carruth21c90602015-12-30 03:24:14 +000066TemplateName::TemplateName(TemplateDecl *Template) : Storage(Template) {}
67TemplateName::TemplateName(OverloadedTemplateStorage *Storage)
68 : Storage(Storage) {}
69TemplateName::TemplateName(SubstTemplateTemplateParmStorage *Storage)
70 : Storage(Storage) {}
71TemplateName::TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
72 : Storage(Storage) {}
73TemplateName::TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) {}
74TemplateName::TemplateName(DependentTemplateName *Dep) : Storage(Dep) {}
75
76bool TemplateName::isNull() const { return Storage.isNull(); }
77
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +000078TemplateName::NameKind TemplateName::getKind() const {
79 if (Storage.is<TemplateDecl *>())
80 return Template;
Douglas Gregor5590be02011-01-15 06:45:20 +000081 if (Storage.is<DependentTemplateName *>())
82 return DependentTemplate;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +000083 if (Storage.is<QualifiedTemplateName *>())
84 return QualifiedTemplate;
John McCalld9dfe3a2011-06-30 08:33:18 +000085
86 UncommonTemplateNameStorage *uncommon
87 = Storage.get<UncommonTemplateNameStorage*>();
88 if (uncommon->getAsOverloadedStorage())
89 return OverloadedTemplate;
90 if (uncommon->getAsSubstTemplateTemplateParm())
91 return SubstTemplateTemplateParm;
92 return SubstTemplateTemplateParmPack;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +000093}
94
Douglas Gregordc572a32009-03-30 22:58:21 +000095TemplateDecl *TemplateName::getAsTemplateDecl() const {
96 if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
97 return Template;
Mike Stump11289f42009-09-09 15:08:12 +000098
Douglas Gregordc572a32009-03-30 22:58:21 +000099 if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
100 return QTN->getTemplateDecl();
101
John McCalld9dfe3a2011-06-30 08:33:18 +0000102 if (SubstTemplateTemplateParmStorage *sub = getAsSubstTemplateTemplateParm())
103 return sub->getReplacement().getAsTemplateDecl();
104
Craig Topper36250ad2014-05-12 05:36:57 +0000105 return nullptr;
Douglas Gregordc572a32009-03-30 22:58:21 +0000106}
107
Chandler Carruth21c90602015-12-30 03:24:14 +0000108OverloadedTemplateStorage *TemplateName::getAsOverloadedTemplate() const {
109 if (UncommonTemplateNameStorage *Uncommon =
110 Storage.dyn_cast<UncommonTemplateNameStorage *>())
111 return Uncommon->getAsOverloadedStorage();
112
113 return nullptr;
114}
115
116SubstTemplateTemplateParmStorage *
117TemplateName::getAsSubstTemplateTemplateParm() const {
118 if (UncommonTemplateNameStorage *uncommon =
119 Storage.dyn_cast<UncommonTemplateNameStorage *>())
120 return uncommon->getAsSubstTemplateTemplateParm();
121
122 return nullptr;
123}
124
125SubstTemplateTemplateParmPackStorage *
126TemplateName::getAsSubstTemplateTemplateParmPack() const {
127 if (UncommonTemplateNameStorage *Uncommon =
128 Storage.dyn_cast<UncommonTemplateNameStorage *>())
129 return Uncommon->getAsSubstTemplateTemplateParmPack();
130
131 return nullptr;
132}
133
134QualifiedTemplateName *TemplateName::getAsQualifiedTemplateName() const {
135 return Storage.dyn_cast<QualifiedTemplateName *>();
136}
137
138DependentTemplateName *TemplateName::getAsDependentTemplateName() const {
139 return Storage.dyn_cast<DependentTemplateName *>();
140}
141
Richard Smith1abacfc2017-08-29 22:14:43 +0000142TemplateName TemplateName::getNameToSubstitute() const {
143 TemplateDecl *Decl = getAsTemplateDecl();
144
145 // Substituting a dependent template name: preserve it as written.
146 if (!Decl)
147 return *this;
148
149 // If we have a template declaration, use the most recent non-friend
150 // declaration of that template.
151 Decl = cast<TemplateDecl>(Decl->getMostRecentDecl());
152 while (Decl->getFriendObjectKind()) {
153 Decl = cast<TemplateDecl>(Decl->getPreviousDecl());
154 assert(Decl && "all declarations of template are friends");
155 }
156 return TemplateName(Decl);
157}
158
Douglas Gregordc572a32009-03-30 22:58:21 +0000159bool TemplateName::isDependent() const {
160 if (TemplateDecl *Template = getAsTemplateDecl()) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000161 if (isa<TemplateTemplateParmDecl>(Template))
162 return true;
163 // FIXME: Hack, getDeclContext() can be null if Template is still
164 // initializing due to PCH reading, so we check it before using it.
165 // Should probably modify TemplateSpecializationType to allow constructing
166 // it without the isDependent() checking.
167 return Template->getDeclContext() &&
168 Template->getDeclContext()->isDependentContext();
Douglas Gregordc572a32009-03-30 22:58:21 +0000169 }
170
John McCalld28ae272009-12-02 08:04:21 +0000171 assert(!getAsOverloadedTemplate() &&
172 "overloaded templates shouldn't survive to here");
Mike Stump11289f42009-09-09 15:08:12 +0000173
Douglas Gregordc572a32009-03-30 22:58:21 +0000174 return true;
175}
176
Douglas Gregor678d76c2011-07-01 01:22:09 +0000177bool TemplateName::isInstantiationDependent() const {
178 if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
179 if (QTN->getQualifier()->isInstantiationDependent())
180 return true;
181 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000182
Douglas Gregor678d76c2011-07-01 01:22:09 +0000183 return isDependent();
184}
185
Douglas Gregor506bd562010-12-13 22:49:22 +0000186bool TemplateName::containsUnexpandedParameterPack() const {
Richard Smith77a9c602018-02-28 03:02:23 +0000187 if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
188 if (QTN->getQualifier()->containsUnexpandedParameterPack())
189 return true;
190 }
191
Douglas Gregor506bd562010-12-13 22:49:22 +0000192 if (TemplateDecl *Template = getAsTemplateDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000193 if (TemplateTemplateParmDecl *TTP
Douglas Gregor506bd562010-12-13 22:49:22 +0000194 = dyn_cast<TemplateTemplateParmDecl>(Template))
195 return TTP->isParameterPack();
196
197 return false;
198 }
199
200 if (DependentTemplateName *DTN = getAsDependentTemplateName())
Fangrui Song6907ce22018-07-30 19:24:48 +0000201 return DTN->getQualifier() &&
Douglas Gregor506bd562010-12-13 22:49:22 +0000202 DTN->getQualifier()->containsUnexpandedParameterPack();
203
Craig Topper36250ad2014-05-12 05:36:57 +0000204 return getAsSubstTemplateTemplateParmPack() != nullptr;
Douglas Gregor506bd562010-12-13 22:49:22 +0000205}
206
Mike Stump11289f42009-09-09 15:08:12 +0000207void
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000208TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
Douglas Gregor7de59662009-05-29 20:38:28 +0000209 bool SuppressNNS) const {
Douglas Gregordc572a32009-03-30 22:58:21 +0000210 if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000211 OS << *Template;
Douglas Gregordc572a32009-03-30 22:58:21 +0000212 else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
Douglas Gregordce2b622009-04-01 00:28:59 +0000213 if (!SuppressNNS)
Douglas Gregor7de59662009-05-29 20:38:28 +0000214 QTN->getQualifier()->print(OS, Policy);
Douglas Gregordc572a32009-03-30 22:58:21 +0000215 if (QTN->hasTemplateKeyword())
216 OS << "template ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000217 OS << *QTN->getDecl();
Douglas Gregordc572a32009-03-30 22:58:21 +0000218 } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
Douglas Gregor308047d2009-09-09 00:23:06 +0000219 if (!SuppressNNS && DTN->getQualifier())
Douglas Gregor7de59662009-05-29 20:38:28 +0000220 DTN->getQualifier()->print(OS, Policy);
Douglas Gregordc572a32009-03-30 22:58:21 +0000221 OS << "template ";
Fangrui Song6907ce22018-07-30 19:24:48 +0000222
Douglas Gregor71395fa2009-11-04 00:56:37 +0000223 if (DTN->isIdentifier())
224 OS << DTN->getIdentifier()->getName();
225 else
226 OS << "operator " << getOperatorSpelling(DTN->getOperator());
John McCalld9dfe3a2011-06-30 08:33:18 +0000227 } else if (SubstTemplateTemplateParmStorage *subst
228 = getAsSubstTemplateTemplateParm()) {
229 subst->getReplacement().print(OS, Policy, SuppressNNS);
Douglas Gregor5590be02011-01-15 06:45:20 +0000230 } else if (SubstTemplateTemplateParmPackStorage *SubstPack
231 = getAsSubstTemplateTemplateParmPack())
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000232 OS << *SubstPack->getParameterPack();
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000233 else {
234 OverloadedTemplateStorage *OTS = getAsOverloadedTemplate();
235 (*OTS->begin())->printName(OS);
236 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000237}
Douglas Gregoraa594892009-03-31 18:38:02 +0000238
Jeffrey Yasskin823015d2010-04-08 00:03:06 +0000239const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
240 TemplateName N) {
241 std::string NameStr;
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000242 llvm::raw_string_ostream OS(NameStr);
Jeffrey Yasskin823015d2010-04-08 00:03:06 +0000243 LangOptions LO;
244 LO.CPlusPlus = true;
245 LO.Bool = true;
David Blaikiee7504912013-03-05 06:21:38 +0000246 OS << '\'';
Jeffrey Yasskin823015d2010-04-08 00:03:06 +0000247 N.print(OS, PrintingPolicy(LO));
David Blaikiee7504912013-03-05 06:21:38 +0000248 OS << '\'';
Jeffrey Yasskin823015d2010-04-08 00:03:06 +0000249 OS.flush();
250 return DB << NameStr;
251}
252
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000253void TemplateName::dump(raw_ostream &OS) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +0000254 LangOptions LO; // FIXME!
255 LO.CPlusPlus = true;
256 LO.Bool = true;
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000257 print(OS, PrintingPolicy(LO));
258}
259
Yaron Kerencdae9412016-01-29 19:38:18 +0000260LLVM_DUMP_METHOD void TemplateName::dump() const {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000261 dump(llvm::errs());
Douglas Gregoraa594892009-03-31 18:38:02 +0000262}