NestedNameSpecifier.cpp revision bad351822117eaf280081494e3dbe4a06c0dbfcf
1//===--- NestedNameSpecifier.cpp - C++ nested name specifiers -----*- 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 NestedNameSpecifier class, which represents
11//  a C++ nested-name-specifier.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/NestedNameSpecifier.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/Type.h"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace clang;
21
22DeclContext *
23NestedNameSpecifier::computeDeclContext(ASTContext &Context) const {
24  // The simple case: we're storing a DeclContext
25  if ((Data & 0x01) == 0)
26    return reinterpret_cast<DeclContext *>(Data);
27
28  Type *T = getAsType();
29  if (!T)
30    return 0;
31
32  // Retrieve the DeclContext associated with this type.
33  const TagType *TagT = T->getAsTagType();
34  assert(TagT && "No DeclContext from a non-tag type");
35  return TagT->getDecl();
36}
37
38void NestedNameSpecifier::Print(llvm::raw_ostream &OS,
39                                const NestedNameSpecifier *First,
40                                const NestedNameSpecifier *Last) {
41  for (; First != Last; ++First) {
42    if (Type *T = First->getAsType()) {
43      std::string TypeStr;
44
45      // If this is a qualified name type, suppress the qualification:
46      // it's part of our nested-name-specifier sequence anyway.
47      if (const QualifiedNameType *QualT = dyn_cast<QualifiedNameType>(T))
48        T = QualT->getNamedType().getTypePtr();
49
50      if (const TagType *TagT = dyn_cast<TagType>(T))
51        TagT->getAsStringInternal(TypeStr, true);
52      else
53        T->getAsStringInternal(TypeStr);
54      OS << TypeStr;
55    } else if (NamedDecl *NamedDC
56                 = dyn_cast_or_null<NamedDecl>(First->getAsDeclContext()))
57      OS << NamedDC->getNameAsString();
58
59    OS <<  "::";
60  }
61}
62