Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Decl.cpp - Declaration AST Node Implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Argiris Kirtzidis | e7dfca1 | 2008-06-04 13:04:04 +0000 | [diff] [blame] | 10 | // This file implements the Decl subclasses. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/Stmt.h" |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 20 | #include <vector> |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 25 | // Decl Allocation/Deallocation Method Implementations |
| 26 | //===----------------------------------------------------------------------===// |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 27 | |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 28 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 29 | return new (C) TranslationUnitDecl(); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 32 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 33 | SourceLocation L, IdentifierInfo *Id) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 34 | return new (C) NamespaceDecl(DC, L, Id); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 37 | void NamespaceDecl::Destroy(ASTContext& C) { |
| 38 | // NamespaceDecl uses "NextDeclarator" to chain namespace declarations |
| 39 | // together. They are all top-level Decls. |
| 40 | |
Ted Kremenek | 0f43384 | 2008-05-24 15:09:56 +0000 | [diff] [blame] | 41 | this->~NamespaceDecl(); |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 42 | C.Deallocate((void *)this); |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 46 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 47 | SourceLocation L, IdentifierInfo *Id, QualType T) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 48 | return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 51 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 52 | SourceLocation L, IdentifierInfo *Id, |
| 53 | QualType T, StorageClass S, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 54 | Expr *DefArg) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 55 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg); |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | QualType ParmVarDecl::getOriginalType() const { |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 59 | if (const OriginalParmVarDecl *PVD = |
| 60 | dyn_cast<OriginalParmVarDecl>(this)) |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 61 | return PVD->OriginalType; |
| 62 | return getType(); |
Chris Lattner | 48d225c | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 65 | OriginalParmVarDecl *OriginalParmVarDecl::Create( |
Fariborz Jahanian | 160e881 | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 66 | ASTContext &C, DeclContext *DC, |
| 67 | SourceLocation L, IdentifierInfo *Id, |
| 68 | QualType T, QualType OT, StorageClass S, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 69 | Expr *DefArg) { |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 70 | return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg); |
Fariborz Jahanian | 160e881 | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 73 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 74 | SourceLocation L, |
Douglas Gregor | 6704b31 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 75 | DeclarationName N, QualType T, |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 76 | StorageClass S, bool isInline, |
Steve Naroff | 71cd776 | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 77 | SourceLocation TypeSpecStartLoc) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 78 | return new (C) FunctionDecl(Function, DC, L, N, T, S, isInline, |
Steve Naroff | 71cd776 | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 79 | TypeSpecStartLoc); |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 82 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 83 | return new (C) BlockDecl(DC, L); |
Steve Naroff | 9ac456d | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 86 | FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 87 | IdentifierInfo *Id, QualType T, Expr *BW, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 88 | bool Mutable) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 89 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable); |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Douglas Gregor | c7f0161 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 92 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 93 | if (!isImplicit() || getDeclName()) |
| 94 | return false; |
| 95 | |
| 96 | if (const RecordType *Record = getType()->getAsRecordType()) |
| 97 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 98 | |
| 99 | return false; |
| 100 | } |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 101 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 102 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 103 | SourceLocation L, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 104 | IdentifierInfo *Id, QualType T, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 105 | Expr *E, const llvm::APSInt &V) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 106 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 109 | void EnumConstantDecl::Destroy(ASTContext& C) { |
| 110 | if (Init) Init->Destroy(C); |
| 111 | Decl::Destroy(C); |
| 112 | } |
| 113 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 114 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 115 | SourceLocation L, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 116 | IdentifierInfo *Id, QualType T) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 117 | return new (C) TypedefDecl(DC, L, Id, T); |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 120 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 121 | IdentifierInfo *Id, |
Douglas Gregor | ae64489 | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 122 | EnumDecl *PrevDecl) { |
Steve Naroff | 207b9ec | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 123 | EnumDecl *Enum = new (C) EnumDecl(DC, L, Id); |
Douglas Gregor | ae64489 | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 124 | C.getTypeDeclType(Enum, PrevDecl); |
| 125 | return Enum; |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Ted Kremenek | 25d8be1 | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 128 | void EnumDecl::Destroy(ASTContext& C) { |
Ted Kremenek | 25d8be1 | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 129 | Decl::Destroy(C); |
| 130 | } |
| 131 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 132 | void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) { |
| 133 | assert(!isDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 134 | IntegerType = NewType; |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 135 | TagDecl::completeDefinition(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 138 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 139 | SourceLocation L, |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 140 | StringLiteral *Str) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 141 | return new (C) FileScopeAsmDecl(DC, L, Str); |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 144 | //===----------------------------------------------------------------------===// |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 145 | // NamedDecl Implementation |
Argiris Kirtzidis | 881964b | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 146 | //===----------------------------------------------------------------------===// |
| 147 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 148 | std::string NamedDecl::getQualifiedNameAsString() const { |
| 149 | std::vector<std::string> Names; |
| 150 | std::string QualName; |
| 151 | const DeclContext *Ctx = getDeclContext(); |
| 152 | |
| 153 | if (Ctx->isFunctionOrMethod()) |
| 154 | return getNameAsString(); |
| 155 | |
| 156 | while (Ctx) { |
| 157 | if (Ctx->isFunctionOrMethod()) |
| 158 | // FIXME: That probably will happen, when D was member of local |
| 159 | // scope class/struct/union. How do we handle this case? |
| 160 | break; |
| 161 | |
| 162 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx)) |
| 163 | Names.push_back(ND->getNameAsString()); |
| 164 | else |
| 165 | break; |
| 166 | |
| 167 | Ctx = Ctx->getParent(); |
| 168 | } |
| 169 | |
| 170 | std::vector<std::string>::reverse_iterator |
| 171 | I = Names.rbegin(), |
| 172 | End = Names.rend(); |
| 173 | |
| 174 | for (; I!=End; ++I) |
| 175 | QualName += *I + "::"; |
| 176 | |
| 177 | QualName += getNameAsString(); |
| 178 | |
| 179 | return QualName; |
| 180 | } |
| 181 | |
| 182 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 183 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6e71edc | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 184 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 185 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 186 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 187 | // We want to keep it, unless it nominates same namespace. |
| 188 | if (getKind() == Decl::UsingDirective) { |
| 189 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == |
| 190 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); |
| 191 | } |
| 192 | |
Douglas Gregor | 6e71edc | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 193 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 194 | // For function declarations, we keep track of redeclarations. |
| 195 | return FD->getPreviousDeclaration() == OldD; |
| 196 | |
| 197 | // For non-function declarations, if the declarations are of the |
| 198 | // same kind then this must be a redeclaration, or semantic analysis |
| 199 | // would not have given us the new declaration. |
| 200 | return this->getKind() == OldD->getKind(); |
| 201 | } |
| 202 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 203 | |
Argiris Kirtzidis | 881964b | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 204 | //===----------------------------------------------------------------------===// |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 205 | // VarDecl Implementation |
| 206 | //===----------------------------------------------------------------------===// |
| 207 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 208 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 209 | IdentifierInfo *Id, QualType T, StorageClass S, |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 210 | SourceLocation TypeSpecStartLoc) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 211 | return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc); |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void VarDecl::Destroy(ASTContext& C) { |
Sebastian Redl | 2ee5561 | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 215 | Expr *Init = getInit(); |
| 216 | if (Init) |
| 217 | Init->Destroy(C); |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 218 | this->~VarDecl(); |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 219 | C.Deallocate((void *)this); |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | VarDecl::~VarDecl() { |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | //===----------------------------------------------------------------------===// |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 226 | // FunctionDecl Implementation |
| 227 | //===----------------------------------------------------------------------===// |
| 228 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 229 | void FunctionDecl::Destroy(ASTContext& C) { |
Ted Kremenek | 345b93d | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 230 | if (Body) |
| 231 | Body->Destroy(C); |
| 232 | |
| 233 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 234 | (*I)->Destroy(C); |
Nuno Lopes | cb8cc5b | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 235 | |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 236 | C.Deallocate(ParamInfo); |
Nuno Lopes | cb8cc5b | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 237 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 238 | Decl::Destroy(C); |
| 239 | } |
| 240 | |
| 241 | |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 242 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
| 243 | for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) { |
| 244 | if (FD->Body) { |
| 245 | Definition = FD; |
| 246 | return FD->Body; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Ted Kremenek | a6ded35 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 253 | // Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams() |
| 254 | static unsigned getNumTypeParams(QualType T) { |
| 255 | const FunctionType *FT = T->getAsFunctionType(); |
Chris Lattner | e873359 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 256 | if (isa<FunctionTypeNoProto>(FT)) |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 257 | return 0; |
Chris Lattner | e873359 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 258 | return cast<FunctionTypeProto>(FT)->getNumArgs(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Ted Kremenek | a6ded35 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 261 | unsigned FunctionDecl::getNumParams() const { |
| 262 | // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar; |
| 263 | if (!ParamInfo) |
| 264 | return 0; |
| 265 | |
| 266 | return getNumTypeParams(getType()); |
| 267 | } |
| 268 | |
Ted Kremenek | 8494c96 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 269 | void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 270 | unsigned NumParams) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 271 | assert(ParamInfo == 0 && "Already has param info!"); |
Ted Kremenek | a6ded35 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 272 | assert(NumParams == getNumTypeParams(getType()) && |
| 273 | "Parameter count mismatch!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 274 | |
| 275 | // Zero params -> null pointer. |
| 276 | if (NumParams) { |
Steve Naroff | 207b9ec | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 277 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | 8494c96 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 278 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 279 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 280 | } |
| 281 | } |
| 282 | |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 283 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 284 | /// needed to call this function. This may be fewer than the number of |
| 285 | /// function parameters, if some of the parameters have default |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 286 | /// arguments (in C++). |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 287 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 288 | unsigned NumRequiredArgs = getNumParams(); |
| 289 | while (NumRequiredArgs > 0 |
| 290 | && getParamDecl(NumRequiredArgs-1)->getDefaultArg()) |
| 291 | --NumRequiredArgs; |
| 292 | |
| 293 | return NumRequiredArgs; |
| 294 | } |
| 295 | |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 296 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 297 | /// function represents, if any. |
| 298 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | 96a32dd | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 299 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 300 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 301 | else |
| 302 | return OO_None; |
| 303 | } |
| 304 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 305 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 306 | // TagDecl Implementation |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 307 | //===----------------------------------------------------------------------===// |
| 308 | |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 309 | void TagDecl::startDefinition() { |
| 310 | cast<TagType>(TypeForDecl)->decl.setPointer(this); |
| 311 | cast<TagType>(TypeForDecl)->decl.setInt(1); |
| 312 | } |
| 313 | |
| 314 | void TagDecl::completeDefinition() { |
| 315 | assert((!TypeForDecl || |
| 316 | cast<TagType>(TypeForDecl)->decl.getPointer() == this) && |
| 317 | "Attempt to redefine a tag definition?"); |
| 318 | IsDefinition = true; |
| 319 | cast<TagType>(TypeForDecl)->decl.setPointer(this); |
| 320 | cast<TagType>(TypeForDecl)->decl.setInt(0); |
| 321 | } |
| 322 | |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 323 | TagDecl* TagDecl::getDefinition(ASTContext& C) const { |
| 324 | QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this)); |
| 325 | TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl()); |
| 326 | return D->isDefinition() ? D : 0; |
| 327 | } |
| 328 | |
| 329 | //===----------------------------------------------------------------------===// |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 330 | // RecordDecl Implementation |
| 331 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 332 | |
Argiris Kirtzidis | 2538a8c | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 333 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 334 | IdentifierInfo *Id) |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 335 | : TagDecl(DK, TK, DC, L, Id) { |
Ted Kremenek | 6f0a241 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 336 | HasFlexibleArrayMember = false; |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 337 | AnonymousStructOrUnion = false; |
Ted Kremenek | 6f0a241 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 338 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6f0a241 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 342 | SourceLocation L, IdentifierInfo *Id, |
| 343 | RecordDecl* PrevDecl) { |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 344 | |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 345 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id); |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 346 | C.getTypeDeclType(R, PrevDecl); |
| 347 | return R; |
Ted Kremenek | 6f0a241 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Argiris Kirtzidis | d64c111 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 350 | RecordDecl::~RecordDecl() { |
Argiris Kirtzidis | d64c111 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void RecordDecl::Destroy(ASTContext& C) { |
Argiris Kirtzidis | d64c111 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 354 | TagDecl::Destroy(C); |
| 355 | } |
| 356 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 357 | /// completeDefinition - Notes that the definition of this type is now |
| 358 | /// complete. |
| 359 | void RecordDecl::completeDefinition(ASTContext& C) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 360 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 361 | TagDecl::completeDefinition(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Steve Naroff | 9ac456d | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 364 | //===----------------------------------------------------------------------===// |
| 365 | // BlockDecl Implementation |
| 366 | //===----------------------------------------------------------------------===// |
| 367 | |
| 368 | BlockDecl::~BlockDecl() { |
| 369 | } |
| 370 | |
| 371 | void BlockDecl::Destroy(ASTContext& C) { |
| 372 | if (Body) |
| 373 | Body->Destroy(C); |
| 374 | |
| 375 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 376 | (*I)->Destroy(C); |
| 377 | |
| 378 | Decl::Destroy(C); |
| 379 | } |