blob: b56c0cebfa5833bcf78e9b5fbabbe9998cc8a5c3 [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"
Chris Lattnere4f21422009-06-30 01:26:17 +000018#include "clang/Basic/LangOptions.h"
Douglas Gregor7532dc62009-03-30 22:58:21 +000019#include "llvm/Support/raw_ostream.h"
Douglas Gregor7532dc62009-03-30 22:58:21 +000020using namespace clang;
21
22TemplateDecl *TemplateName::getAsTemplateDecl() const {
23 if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
24 return Template;
Mike Stump1eb44332009-09-09 15:08:12 +000025
Douglas Gregor7532dc62009-03-30 22:58:21 +000026 if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
27 return QTN->getTemplateDecl();
28
29 return 0;
30}
31
32bool TemplateName::isDependent() const {
33 if (TemplateDecl *Template = getAsTemplateDecl()) {
Mike Stump1eb44332009-09-09 15:08:12 +000034 return isa<TemplateTemplateParmDecl>(Template) ||
Douglas Gregord99cbe62009-07-29 18:26:50 +000035 Template->getDeclContext()->isDependentContext();
Douglas Gregor7532dc62009-03-30 22:58:21 +000036 }
37
John McCall0bd6feb2009-12-02 08:04:21 +000038 assert(!getAsOverloadedTemplate() &&
39 "overloaded templates shouldn't survive to here");
Mike Stump1eb44332009-09-09 15:08:12 +000040
Douglas Gregor7532dc62009-03-30 22:58:21 +000041 return true;
42}
43
Mike Stump1eb44332009-09-09 15:08:12 +000044void
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000045TemplateName::print(llvm::raw_ostream &OS, const PrintingPolicy &Policy,
46 bool SuppressNNS) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +000047 if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
Douglas Gregorca1bdd72009-11-04 00:56:37 +000048 OS << Template->getNameAsString();
Douglas Gregor7532dc62009-03-30 22:58:21 +000049 else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
Douglas Gregor17343172009-04-01 00:28:59 +000050 if (!SuppressNNS)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000051 QTN->getQualifier()->print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +000052 if (QTN->hasTemplateKeyword())
53 OS << "template ";
Douglas Gregord99cbe62009-07-29 18:26:50 +000054 OS << QTN->getDecl()->getNameAsString();
Douglas Gregor7532dc62009-03-30 22:58:21 +000055 } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000056 if (!SuppressNNS && DTN->getQualifier())
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000057 DTN->getQualifier()->print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +000058 OS << "template ";
Douglas Gregorca1bdd72009-11-04 00:56:37 +000059
60 if (DTN->isIdentifier())
61 OS << DTN->getIdentifier()->getName();
62 else
63 OS << "operator " << getOperatorSpelling(DTN->getOperator());
Douglas Gregor7532dc62009-03-30 22:58:21 +000064 }
65}
Douglas Gregorde650ae2009-03-31 18:38:02 +000066
Douglas Gregor9bde7732009-03-31 20:22:05 +000067void TemplateName::dump() const {
Chris Lattnere4f21422009-06-30 01:26:17 +000068 LangOptions LO; // FIXME!
69 LO.CPlusPlus = true;
70 LO.Bool = true;
71 print(llvm::errs(), PrintingPolicy(LO));
Douglas Gregorde650ae2009-03-31 18:38:02 +000072}