Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | c960ee3 | 2009-04-02 06:07:12 +0000 | [diff] [blame] | 13 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 14 | #include "clang/AST/TemplateName.h" |
| 15 | #include "clang/AST/DeclTemplate.h" |
| 16 | #include "clang/AST/NestedNameSpecifier.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 17 | #include "clang/AST/PrettyPrinter.h" |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
| 22 | TemplateDecl *TemplateName::getAsTemplateDecl() const { |
| 23 | if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>()) |
| 24 | return Template; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 25 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 26 | if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) |
| 27 | return QTN->getTemplateDecl(); |
| 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 32 | OverloadedFunctionDecl *TemplateName::getAsOverloadedFunctionDecl() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 33 | if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 34 | = Storage.dyn_cast<OverloadedFunctionDecl *>()) |
| 35 | return Ovl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 36 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 37 | if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) |
| 38 | return QTN->getOverloadedFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 39 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 40 | return 0; |
| 41 | } |
| 42 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 43 | bool TemplateName::isDependent() const { |
| 44 | if (TemplateDecl *Template = getAsTemplateDecl()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 45 | return isa<TemplateTemplateParmDecl>(Template) || |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 46 | Template->getDeclContext()->isDependentContext(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 49 | if (OverloadedFunctionDecl *Ovl = getAsOverloadedFunctionDecl()) |
| 50 | return Ovl->getDeclContext()->isDependentContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 51 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 55 | void |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 56 | TemplateName::print(llvm::raw_ostream &OS, const PrintingPolicy &Policy, |
| 57 | bool SuppressNNS) const { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 58 | if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>()) |
| 59 | OS << Template->getIdentifier()->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 60 | else if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 61 | = Storage.dyn_cast<OverloadedFunctionDecl *>()) |
| 62 | OS << Ovl->getNameAsString(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 63 | else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 64 | if (!SuppressNNS) |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 65 | QTN->getQualifier()->print(OS, Policy); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 66 | if (QTN->hasTemplateKeyword()) |
| 67 | OS << "template "; |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 68 | OS << QTN->getDecl()->getNameAsString(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 69 | } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) { |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 70 | if (!SuppressNNS && DTN->getQualifier()) |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 71 | DTN->getQualifier()->print(OS, Policy); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 72 | OS << "template "; |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 73 | // FIXME: Shouldn't we have a more general kind of name? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 74 | OS << DTN->getName()->getName(); |
| 75 | } |
| 76 | } |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 77 | |
Douglas Gregor | 9bde773 | 2009-03-31 20:22:05 +0000 | [diff] [blame] | 78 | void TemplateName::dump() const { |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 79 | LangOptions LO; // FIXME! |
| 80 | LO.CPlusPlus = true; |
| 81 | LO.Bool = true; |
| 82 | print(llvm::errs(), PrintingPolicy(LO)); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 83 | } |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 84 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 85 | TemplateDecl *QualifiedTemplateName::getTemplateDecl() const { |
| 86 | return dyn_cast<TemplateDecl>(Template); |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | OverloadedFunctionDecl * |
| 90 | QualifiedTemplateName::getOverloadedFunctionDecl() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 91 | return dyn_cast<OverloadedFunctionDecl>(Template); |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 92 | } |
| 93 | |