DeclCXX.cpp revision b0d178d78fd9a67627b89a7e2dafde2c93fbcd1c
1//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
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 implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
15#include "clang/AST/ASTContext.h"
16using namespace clang;
17
18//===----------------------------------------------------------------------===//
19// Decl Allocation/Deallocation Method Implementations
20//===----------------------------------------------------------------------===//
21
22CXXFieldDecl *CXXFieldDecl::Create(ASTContext &C, CXXRecordDecl *RD,
23                                   SourceLocation L, IdentifierInfo *Id,
24                                   QualType T, Expr *BW) {
25  void *Mem = C.getAllocator().Allocate<CXXFieldDecl>();
26  return new (Mem) CXXFieldDecl(RD, L, Id, T, BW);
27}
28
29CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
30                                     SourceLocation L, IdentifierInfo *Id,
31                                     CXXRecordDecl* PrevDecl) {
32  void *Mem = C.getAllocator().Allocate<CXXRecordDecl>();
33  CXXRecordDecl* R = new (Mem) CXXRecordDecl(TK, DC, L, Id);
34  C.getTypeDeclType(R, PrevDecl);
35  return R;
36}
37
38CXXRecordDecl::~CXXRecordDecl() {
39  delete [] Bases;
40}
41
42void
43CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
44                        unsigned NumBases) {
45  if (this->Bases)
46    delete [] this->Bases;
47
48  this->Bases = new CXXBaseSpecifier[NumBases];
49  this->NumBases = NumBases;
50  for (unsigned i = 0; i < NumBases; ++i)
51    this->Bases[i] = *Bases[i];
52}
53
54CXXMethodDecl *
55CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
56                      SourceLocation L, IdentifierInfo *Id,
57                      QualType T, bool isStatic, bool isInline,
58                      ScopedDecl *PrevDecl) {
59  void *Mem = C.getAllocator().Allocate<CXXMethodDecl>();
60  return new (Mem) CXXMethodDecl(RD, L, Id, T, isStatic, isInline, PrevDecl);
61}
62
63QualType CXXMethodDecl::getThisType(ASTContext &C) const {
64  // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
65  // If the member function is declared const, the type of this is const X*,
66  // if the member function is declared volatile, the type of this is
67  // volatile X*, and if the member function is declared const volatile,
68  // the type of this is const volatile X*.
69
70  assert(isInstance() && "No 'this' for static methods!");
71  QualType ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(
72                                            cast<CXXRecordDecl>(getParent())));
73  ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
74  return C.getPointerType(ClassTy).withConst();
75}
76
77CXXClassVarDecl *CXXClassVarDecl::Create(ASTContext &C, CXXRecordDecl *RD,
78                                   SourceLocation L, IdentifierInfo *Id,
79                                   QualType T, ScopedDecl *PrevDecl) {
80  void *Mem = C.getAllocator().Allocate<CXXClassVarDecl>();
81  return new (Mem) CXXClassVarDecl(RD, L, Id, T, PrevDecl);
82}
83
84OverloadedFunctionDecl *
85OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
86                               IdentifierInfo *Id) {
87  void *Mem = C.getAllocator().Allocate<OverloadedFunctionDecl>();
88  return new (Mem) OverloadedFunctionDecl(DC, Id);
89}
90