Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Decl.cpp - Declaration AST Node Implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Argyrios Kyrtzidis | e184bae | 2008-06-04 13:04:04 +0000 | [diff] [blame] | 10 | // This file implements the Decl subclasses. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Stmt.h" |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 22 | #include "clang/AST/PrettyPrinter.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 24 | #include "clang/Basic/IdentifierTable.h" |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 25 | #include "clang/Parse/DeclSpec.h" |
| 26 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 27 | #include <vector> |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 28 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 31 | void Attr::Destroy(ASTContext &C) { |
| 32 | if (Next) { |
| 33 | Next->Destroy(C); |
| 34 | Next = 0; |
| 35 | } |
| 36 | this->~Attr(); |
| 37 | C.Deallocate((void*)this); |
| 38 | } |
| 39 | |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 40 | /// \brief Return the TypeLoc wrapper for the type source info. |
| 41 | TypeLoc DeclaratorInfo::getTypeLoc() const { |
| 42 | return TypeLoc::Create(Ty, (void*)(this + 1)); |
| 43 | } |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 44 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 46 | // Decl Allocation/Deallocation Method Implementations |
| 47 | //===----------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 49 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 50 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 51 | return new (C) TranslationUnitDecl(C); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 54 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 55 | SourceLocation L, IdentifierInfo *Id) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 56 | return new (C) NamespaceDecl(DC, L, Id); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 59 | void NamespaceDecl::Destroy(ASTContext& C) { |
| 60 | // NamespaceDecl uses "NextDeclarator" to chain namespace declarations |
| 61 | // together. They are all top-level Decls. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | ebf27b1 | 2008-05-24 15:09:56 +0000 | [diff] [blame] | 63 | this->~NamespaceDecl(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 64 | C.Deallocate((void *)this); |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 68 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 69 | SourceLocation L, IdentifierInfo *Id, QualType T) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 70 | return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Daniel Dunbar | b286a78 | 2009-04-14 02:08:49 +0000 | [diff] [blame] | 73 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 74 | switch (SC) { |
| 75 | case VarDecl::None: break; |
| 76 | case VarDecl::Auto: return "auto"; break; |
| 77 | case VarDecl::Extern: return "extern"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | case VarDecl::PrivateExtern: return "__private_extern__"; break; |
Daniel Dunbar | b286a78 | 2009-04-14 02:08:49 +0000 | [diff] [blame] | 79 | case VarDecl::Register: return "register"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | case VarDecl::Static: return "static"; break; |
Daniel Dunbar | b286a78 | 2009-04-14 02:08:49 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | assert(0 && "Invalid storage class"); |
| 84 | return 0; |
| 85 | } |
| 86 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 87 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 88 | SourceLocation L, IdentifierInfo *Id, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 89 | QualType T, DeclaratorInfo *DInfo, |
| 90 | StorageClass S, Expr *DefArg) { |
| 91 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, DInfo, S, DefArg); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | QualType ParmVarDecl::getOriginalType() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | if (const OriginalParmVarDecl *PVD = |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 96 | dyn_cast<OriginalParmVarDecl>(this)) |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 97 | return PVD->OriginalType; |
| 98 | return getType(); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame^] | 101 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 102 | if (const Expr *E = getInit()) |
| 103 | return E->getSourceRange(); |
| 104 | |
| 105 | if (const Expr *E = getUninstantiatedDefaultArg()) |
| 106 | return E->getSourceRange(); |
| 107 | |
| 108 | return SourceRange(); |
| 109 | } |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 110 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame^] | 111 | void VarDecl::setInit(ASTContext &C, Expr *I) { |
| 112 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| 113 | Eval->~EvaluatedStmt(); |
| 114 | C.Deallocate(Eval); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame^] | 117 | Init = I; |
| 118 | } |
| 119 | |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 120 | bool VarDecl::isExternC(ASTContext &Context) const { |
| 121 | if (!Context.getLangOptions().CPlusPlus) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | return (getDeclContext()->isTranslationUnit() && |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 123 | getStorageClass() != Static) || |
| 124 | (getDeclContext()->isFunctionOrMethod() && hasExternalStorage()); |
| 125 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 127 | DC = DC->getParent()) { |
| 128 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 129 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
| 130 | return getStorageClass() != Static; |
| 131 | |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | if (DC->isFunctionOrMethod()) |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 142 | OriginalParmVarDecl *OriginalParmVarDecl::Create( |
Fariborz Jahanian | 73da9e4 | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 143 | ASTContext &C, DeclContext *DC, |
| 144 | SourceLocation L, IdentifierInfo *Id, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 145 | QualType T, DeclaratorInfo *DInfo, |
| 146 | QualType OT, StorageClass S, Expr *DefArg) { |
| 147 | return new (C) OriginalParmVarDecl(DC, L, Id, T, DInfo, OT, S, DefArg); |
Fariborz Jahanian | 73da9e4 | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 150 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | SourceLocation L, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 152 | DeclarationName N, QualType T, |
| 153 | DeclaratorInfo *DInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | StorageClass S, bool isInline, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 155 | bool hasWrittenPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | FunctionDecl *New |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 157 | = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline); |
Anders Carlsson | a75e853 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 158 | New->HasWrittenPrototype = hasWrittenPrototype; |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 159 | return New; |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 162 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 163 | return new (C) BlockDecl(DC, L); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 166 | FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 167 | IdentifierInfo *Id, QualType T, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 168 | DeclaratorInfo *DInfo, Expr *BW, bool Mutable) { |
| 169 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 172 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 173 | if (!isImplicit() || getDeclName()) |
| 174 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 176 | if (const RecordType *Record = getType()->getAs<RecordType>()) |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 177 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 178 | |
| 179 | return false; |
| 180 | } |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 182 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 183 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 184 | IdentifierInfo *Id, QualType T, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 185 | Expr *E, const llvm::APSInt &V) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 186 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 189 | void EnumConstantDecl::Destroy(ASTContext& C) { |
| 190 | if (Init) Init->Destroy(C); |
| 191 | Decl::Destroy(C); |
| 192 | } |
| 193 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 194 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 195 | SourceLocation L, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 196 | IdentifierInfo *Id, QualType T) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 197 | return new (C) TypedefDecl(DC, L, Id, T); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 200 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 201 | IdentifierInfo *Id, SourceLocation TKL, |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 202 | EnumDecl *PrevDecl) { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 203 | EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL); |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 204 | C.getTypeDeclType(Enum, PrevDecl); |
| 205 | return Enum; |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Ted Kremenek | df91eca | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 208 | void EnumDecl::Destroy(ASTContext& C) { |
Ted Kremenek | df91eca | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 209 | Decl::Destroy(C); |
| 210 | } |
| 211 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 212 | void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) { |
| 213 | assert(!isDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 214 | IntegerType = NewType; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 215 | TagDecl::completeDefinition(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 218 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 219 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 220 | StringLiteral *Str) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 221 | return new (C) FileScopeAsmDecl(DC, L, Str); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 224 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 225 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 226 | //===----------------------------------------------------------------------===// |
| 227 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 228 | std::string NamedDecl::getQualifiedNameAsString() const { |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 229 | return getQualifiedNameAsString(getASTContext().getLangOptions()); |
| 230 | } |
| 231 | |
| 232 | std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 233 | std::vector<std::string> Names; |
| 234 | std::string QualName; |
| 235 | const DeclContext *Ctx = getDeclContext(); |
| 236 | |
| 237 | if (Ctx->isFunctionOrMethod()) |
| 238 | return getNameAsString(); |
| 239 | |
| 240 | while (Ctx) { |
| 241 | if (Ctx->isFunctionOrMethod()) |
| 242 | // FIXME: That probably will happen, when D was member of local |
| 243 | // scope class/struct/union. How do we handle this case? |
| 244 | break; |
| 245 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | if (const ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 247 | = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) { |
| 248 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 249 | std::string TemplateArgsStr |
| 250 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 251 | TemplateArgs.getFlatArgumentList(), |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 252 | TemplateArgs.flat_size(), |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 253 | P); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 254 | Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr); |
| 255 | } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx)) |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 256 | Names.push_back(ND->getNameAsString()); |
| 257 | else |
| 258 | break; |
| 259 | |
| 260 | Ctx = Ctx->getParent(); |
| 261 | } |
| 262 | |
| 263 | std::vector<std::string>::reverse_iterator |
| 264 | I = Names.rbegin(), |
| 265 | End = Names.rend(); |
| 266 | |
| 267 | for (; I!=End; ++I) |
| 268 | QualName += *I + "::"; |
| 269 | |
| 270 | QualName += getNameAsString(); |
| 271 | |
| 272 | return QualName; |
| 273 | } |
| 274 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 275 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 276 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 277 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 278 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 279 | // We want to keep it, unless it nominates same namespace. |
| 280 | if (getKind() == Decl::UsingDirective) { |
| 281 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == |
| 282 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); |
| 283 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 285 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 286 | // For function declarations, we keep track of redeclarations. |
| 287 | return FD->getPreviousDeclaration() == OldD; |
| 288 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 289 | // For function templates, the underlying function declarations are linked. |
| 290 | if (const FunctionTemplateDecl *FunctionTemplate |
| 291 | = dyn_cast<FunctionTemplateDecl>(this)) |
| 292 | if (const FunctionTemplateDecl *OldFunctionTemplate |
| 293 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 294 | return FunctionTemplate->getTemplatedDecl() |
| 295 | ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 297 | // For method declarations, we keep track of redeclarations. |
| 298 | if (isa<ObjCMethodDecl>(this)) |
| 299 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 301 | // For non-function declarations, if the declarations are of the |
| 302 | // same kind then this must be a redeclaration, or semantic analysis |
| 303 | // would not have given us the new declaration. |
| 304 | return this->getKind() == OldD->getKind(); |
| 305 | } |
| 306 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 307 | bool NamedDecl::hasLinkage() const { |
| 308 | if (const VarDecl *VD = dyn_cast<VarDecl>(this)) |
| 309 | return VD->hasExternalStorage() || VD->isFileVarDecl(); |
| 310 | |
| 311 | if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this)) |
| 312 | return true; |
| 313 | |
| 314 | return false; |
| 315 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 316 | |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 317 | NamedDecl *NamedDecl::getUnderlyingDecl() { |
| 318 | NamedDecl *ND = this; |
| 319 | while (true) { |
| 320 | if (UsingDecl *UD = dyn_cast<UsingDecl>(ND)) |
| 321 | ND = UD->getTargetDecl(); |
| 322 | else if (ObjCCompatibleAliasDecl *AD |
| 323 | = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 324 | return AD->getClassInterface(); |
| 325 | else |
| 326 | return ND; |
| 327 | } |
| 328 | } |
| 329 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 330 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 331 | // DeclaratorDecl Implementation |
| 332 | //===----------------------------------------------------------------------===// |
| 333 | |
| 334 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
| 335 | if (DeclInfo) |
| 336 | return DeclInfo->getTypeLoc().getTypeSpecRange().getBegin(); |
| 337 | return SourceLocation(); |
| 338 | } |
| 339 | |
| 340 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 341 | // VarDecl Implementation |
| 342 | //===----------------------------------------------------------------------===// |
| 343 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 344 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 345 | IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 346 | StorageClass S) { |
| 347 | return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void VarDecl::Destroy(ASTContext& C) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 351 | Expr *Init = getInit(); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 352 | if (Init) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 353 | Init->Destroy(C); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 354 | if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) { |
| 355 | Eval->~EvaluatedStmt(); |
| 356 | C.Deallocate(Eval); |
| 357 | } |
| 358 | } |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 359 | this->~VarDecl(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 360 | C.Deallocate((void *)this); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | VarDecl::~VarDecl() { |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 366 | SourceRange VarDecl::getSourceRange() const { |
| 367 | if (getInit()) |
| 368 | return SourceRange(getLocation(), getInit()->getLocEnd()); |
| 369 | return SourceRange(getLocation(), getLocation()); |
| 370 | } |
| 371 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 372 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() { |
| 373 | return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 374 | } |
| 375 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 376 | bool VarDecl::isTentativeDefinition(ASTContext &Context) const { |
| 377 | if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus) |
| 378 | return false; |
| 379 | |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 380 | const VarDecl *Def = 0; |
| 381 | return (!getDefinition(Def) && |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 382 | (getStorageClass() == None || getStorageClass() == Static)); |
| 383 | } |
| 384 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 385 | const Expr *VarDecl::getDefinition(const VarDecl *&Def) const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 386 | redecl_iterator I = redecls_begin(), E = redecls_end(); |
| 387 | while (I != E && !I->getInit()) |
| 388 | ++I; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 389 | |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 390 | if (I != E) { |
| 391 | Def = *I; |
| 392 | return I->getInit(); |
| 393 | } |
| 394 | return 0; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 397 | VarDecl *VarDecl::getCanonicalDecl() { |
Argyrios Kyrtzidis | f23e839 | 2009-07-18 08:50:13 +0000 | [diff] [blame] | 398 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 401 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 402 | // FunctionDecl Implementation |
| 403 | //===----------------------------------------------------------------------===// |
| 404 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 405 | void FunctionDecl::Destroy(ASTContext& C) { |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 406 | if (Body && Body.isOffset()) |
| 407 | Body.get(C.getExternalSource())->Destroy(C); |
Ted Kremenek | b65cf41 | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 408 | |
| 409 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 410 | (*I)->Destroy(C); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 411 | |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 412 | C.Deallocate(ParamInfo); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 413 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 414 | Decl::Destroy(C); |
| 415 | } |
| 416 | |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 417 | void FunctionDecl::getNameForDiagnostic(std::string &S, |
| 418 | const PrintingPolicy &Policy, |
| 419 | bool Qualified) const { |
| 420 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 421 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 422 | if (TemplateArgs) |
| 423 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 424 | TemplateArgs->getFlatArgumentList(), |
| 425 | TemplateArgs->flat_size(), |
| 426 | Policy); |
| 427 | |
| 428 | } |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 429 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 430 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 431 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 432 | if (I->Body) { |
| 433 | Definition = *I; |
| 434 | return I->Body.get(getASTContext().getExternalSource()); |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 441 | Stmt *FunctionDecl::getBodyIfAvailable() const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 442 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 443 | if (I->Body && !I->Body.isOffset()) { |
| 444 | return I->Body.get(0); |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 445 | } |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 451 | void FunctionDecl::setBody(Stmt *B) { |
| 452 | Body = B; |
Argyrios Kyrtzidis | 1a5364e | 2009-06-22 17:13:31 +0000 | [diff] [blame] | 453 | if (B) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 454 | EndRangeLoc = B->getLocEnd(); |
| 455 | } |
| 456 | |
John McCall | 07a5c22 | 2009-08-15 02:09:25 +0000 | [diff] [blame] | 457 | bool FunctionDecl::isMain(ASTContext &Context) const { |
| 458 | return !Context.getLangOptions().Freestanding && |
| 459 | getDeclContext()->getLookupContext()->isTranslationUnit() && |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 460 | getIdentifier() && getIdentifier()->isStr("main"); |
| 461 | } |
| 462 | |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 463 | bool FunctionDecl::isExternC(ASTContext &Context) const { |
| 464 | // In C, any non-static, non-overloadable function has external |
| 465 | // linkage. |
| 466 | if (!Context.getLangOptions().CPlusPlus) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 467 | return getStorageClass() != Static && !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 468 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 470 | DC = DC->getParent()) { |
| 471 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 472 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | return getStorageClass() != Static && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 474 | !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 475 | |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return false; |
| 481 | } |
| 482 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 483 | bool FunctionDecl::isGlobal() const { |
| 484 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 485 | return Method->isStatic(); |
| 486 | |
| 487 | if (getStorageClass() == Static) |
| 488 | return false; |
| 489 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 491 | DC->isNamespace(); |
| 492 | DC = DC->getParent()) { |
| 493 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 494 | if (!Namespace->getDeclName()) |
| 495 | return false; |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return true; |
| 501 | } |
| 502 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 503 | /// \brief Returns a value indicating whether this function |
| 504 | /// corresponds to a builtin function. |
| 505 | /// |
| 506 | /// The function corresponds to a built-in function if it is |
| 507 | /// declared at translation scope or within an extern "C" block and |
| 508 | /// its name matches with the name of a builtin. The returned value |
| 509 | /// will be 0 for functions that do not correspond to a builtin, a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 511 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 512 | unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const { |
| 513 | if (!getIdentifier() || !getIdentifier()->getBuiltinID()) |
| 514 | return 0; |
| 515 | |
| 516 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| 517 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 518 | return BuiltinID; |
| 519 | |
| 520 | // This function has the name of a known C library |
| 521 | // function. Determine whether it actually refers to the C library |
| 522 | // function or whether it just has the same name. |
| 523 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 524 | // If this is a static function, it's not a builtin. |
| 525 | if (getStorageClass() == Static) |
| 526 | return 0; |
| 527 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 528 | // If this function is at translation-unit scope and we're not in |
| 529 | // C++, it refers to the C library function. |
| 530 | if (!Context.getLangOptions().CPlusPlus && |
| 531 | getDeclContext()->isTranslationUnit()) |
| 532 | return BuiltinID; |
| 533 | |
| 534 | // If the function is in an extern "C" linkage specification and is |
| 535 | // not marked "overloadable", it's the real function. |
| 536 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 538 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 539 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 540 | return BuiltinID; |
| 541 | |
| 542 | // Not a builtin |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 547 | /// getNumParams - Return the number of parameters this function must have |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 548 | /// based on its FunctionType. This is the length of the PararmInfo array |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 549 | /// after it has been created. |
| 550 | unsigned FunctionDecl::getNumParams() const { |
Chris Lattner | 11ddb7d | 2009-04-25 05:56:45 +0000 | [diff] [blame] | 551 | const FunctionType *FT = getType()->getAsFunctionType(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 552 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 553 | return 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 554 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 558 | void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 559 | unsigned NumParams) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 560 | assert(ParamInfo == 0 && "Already has param info!"); |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 561 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 563 | // Zero params -> null pointer. |
| 564 | if (NumParams) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 565 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 566 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 567 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 568 | |
Argyrios Kyrtzidis | 96888cc | 2009-06-23 00:42:00 +0000 | [diff] [blame] | 569 | // Update source range. The check below allows us to set EndRangeLoc before |
| 570 | // setting the parameters. |
Argyrios Kyrtzidis | cb5f8f5 | 2009-06-23 00:42:15 +0000 | [diff] [blame] | 571 | if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 572 | EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 576 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 577 | /// needed to call this function. This may be fewer than the number of |
| 578 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 579 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 580 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 581 | unsigned NumRequiredArgs = getNumParams(); |
| 582 | while (NumRequiredArgs > 0 |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 583 | && getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 584 | --NumRequiredArgs; |
| 585 | |
| 586 | return NumRequiredArgs; |
| 587 | } |
| 588 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 589 | bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 590 | if (!isInline() || !hasAttr<GNUInlineAttr>()) |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 591 | return false; |
| 592 | |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 593 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 594 | if (I->isInline() && !I->hasAttr<GNUInlineAttr>()) |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 595 | return false; |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 596 | |
| 597 | return true; |
| 598 | } |
| 599 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 600 | bool FunctionDecl::isExternGNUInline(ASTContext &Context) const { |
| 601 | if (!hasActiveGNUInlineAttribute(Context)) |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 602 | return false; |
| 603 | |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 604 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 605 | if (I->getStorageClass() == Extern && I->hasAttr<GNUInlineAttr>()) |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 606 | return true; |
| 607 | |
| 608 | return false; |
| 609 | } |
| 610 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | void |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 612 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 613 | redeclarable_base::setPreviousDeclaration(PrevDecl); |
Argyrios Kyrtzidis | f23e839 | 2009-07-18 08:50:13 +0000 | [diff] [blame] | 614 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 615 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 616 | FunctionTemplateDecl *PrevFunTmpl |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 617 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 618 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 619 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 620 | } |
| 621 | } |
| 622 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 623 | FunctionDecl *FunctionDecl::getCanonicalDecl() { |
Argyrios Kyrtzidis | f23e839 | 2009-07-18 08:50:13 +0000 | [diff] [blame] | 624 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 627 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 628 | /// function represents, if any. |
| 629 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 630 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 631 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 632 | else |
| 633 | return OO_None; |
| 634 | } |
| 635 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 636 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 638 | = TemplateOrSpecialization |
| 639 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 640 | return Info->Template.getPointer(); |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 641 | } |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | const TemplateArgumentList * |
| 646 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 648 | = TemplateOrSpecialization |
| 649 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 650 | return Info->TemplateArguments; |
| 651 | } |
| 652 | return 0; |
| 653 | } |
| 654 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 655 | void |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 656 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context, |
| 657 | FunctionTemplateDecl *Template, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 658 | const TemplateArgumentList *TemplateArgs, |
| 659 | void *InsertPos) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 661 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 662 | if (!Info) |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 663 | Info = new (Context) FunctionTemplateSpecializationInfo; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 665 | Info->Function = this; |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 666 | Info->Template.setPointer(Template); |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 667 | Info->Template.setInt(TSK_ImplicitInstantiation - 1); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 668 | Info->TemplateArguments = TemplateArgs; |
| 669 | TemplateOrSpecialization = Info; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 671 | // Insert this function template specialization into the set of known |
| 672 | // function template specialiations. |
| 673 | Template->getSpecializations().InsertNode(Info, InsertPos); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 676 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 677 | // For a function template specialization, query the specialization |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 678 | // information object. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 680 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
| 681 | if (Info) |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 682 | return Info->getTemplateSpecializationKind(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 683 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 684 | if (!getInstantiatedFromMemberFunction()) |
| 685 | return TSK_Undeclared; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 687 | // Find the class template specialization corresponding to this instantiation |
| 688 | // of a member function. |
| 689 | const DeclContext *Parent = getDeclContext(); |
| 690 | while (Parent && !isa<ClassTemplateSpecializationDecl>(Parent)) |
| 691 | Parent = Parent->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 692 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 693 | if (!Parent) |
| 694 | return TSK_Undeclared; |
| 695 | |
| 696 | return cast<ClassTemplateSpecializationDecl>(Parent)->getSpecializationKind(); |
| 697 | } |
| 698 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 699 | void |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 700 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 702 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
| 703 | assert(Info && "Not a function template specialization"); |
| 704 | Info->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 707 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 708 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 709 | //===----------------------------------------------------------------------===// |
| 710 | |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 711 | SourceRange TagDecl::getSourceRange() const { |
| 712 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 713 | return SourceRange(TagKeywordLoc, E); |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 716 | TagDecl* TagDecl::getCanonicalDecl() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 717 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 720 | void TagDecl::startDefinition() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 721 | if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { |
| 722 | TagT->decl.setPointer(this); |
| 723 | TagT->decl.setInt(1); |
| 724 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | void TagDecl::completeDefinition() { |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 728 | IsDefinition = true; |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 729 | if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { |
| 730 | assert(TagT->decl.getPointer() == this && |
| 731 | "Attempt to redefine a tag definition?"); |
| 732 | TagT->decl.setInt(0); |
| 733 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 736 | TagDecl* TagDecl::getDefinition(ASTContext& C) const { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 737 | if (isDefinition()) |
| 738 | return const_cast<TagDecl *>(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | |
| 740 | for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 741 | R != REnd; ++R) |
| 742 | if (R->isDefinition()) |
| 743 | return *R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 745 | return 0; |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 746 | } |
| 747 | |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 748 | TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) { |
| 749 | switch (TypeSpec) { |
| 750 | default: llvm::llvm_unreachable("unexpected type specifier"); |
| 751 | case DeclSpec::TST_struct: return TK_struct; |
| 752 | case DeclSpec::TST_class: return TK_class; |
| 753 | case DeclSpec::TST_union: return TK_union; |
| 754 | case DeclSpec::TST_enum: return TK_enum; |
| 755 | } |
| 756 | } |
| 757 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 758 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 759 | // RecordDecl Implementation |
| 760 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 761 | |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 762 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 763 | IdentifierInfo *Id, RecordDecl *PrevDecl, |
| 764 | SourceLocation TKL) |
| 765 | : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 766 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 767 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 768 | HasObjectMember = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 769 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 773 | SourceLocation L, IdentifierInfo *Id, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 774 | SourceLocation TKL, RecordDecl* PrevDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 776 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 777 | C.getTypeDeclType(R, PrevDecl); |
| 778 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 781 | RecordDecl::~RecordDecl() { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | void RecordDecl::Destroy(ASTContext& C) { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 785 | TagDecl::Destroy(C); |
| 786 | } |
| 787 | |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 788 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 789 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 790 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 791 | } |
| 792 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 793 | /// completeDefinition - Notes that the definition of this type is now |
| 794 | /// complete. |
| 795 | void RecordDecl::completeDefinition(ASTContext& C) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 796 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 797 | TagDecl::completeDefinition(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 800 | //===----------------------------------------------------------------------===// |
| 801 | // BlockDecl Implementation |
| 802 | //===----------------------------------------------------------------------===// |
| 803 | |
| 804 | BlockDecl::~BlockDecl() { |
| 805 | } |
| 806 | |
| 807 | void BlockDecl::Destroy(ASTContext& C) { |
| 808 | if (Body) |
| 809 | Body->Destroy(C); |
| 810 | |
| 811 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 812 | (*I)->Destroy(C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | |
| 814 | C.Deallocate(ParamInfo); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 815 | Decl::Destroy(C); |
| 816 | } |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 817 | |
| 818 | void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 819 | unsigned NParms) { |
| 820 | assert(ParamInfo == 0 && "Already has param info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 821 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 822 | // Zero params -> null pointer. |
| 823 | if (NParms) { |
| 824 | NumParams = NParms; |
| 825 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
| 826 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
| 827 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | unsigned BlockDecl::getNumParams() const { |
| 832 | return NumParams; |
| 833 | } |