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" |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 16 | #include "clang/AST/Stmt.h" |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/Basic/IdentifierTable.h" |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 19 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 23 | // Decl Allocation/Deallocation Method Implementations |
| 24 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 25 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 26 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 27 | void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>(); |
| 28 | return new (Mem) TranslationUnitDecl(); |
| 29 | } |
| 30 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 31 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 32 | SourceLocation L, IdentifierInfo *Id) { |
| 33 | void *Mem = C.getAllocator().Allocate<NamespaceDecl>(); |
| 34 | return new (Mem) NamespaceDecl(DC, L, Id); |
| 35 | } |
| 36 | |
Ted Kremenek | d1ac17a | 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 | ebf27b1 | 2008-05-24 15:09:56 +0000 | [diff] [blame] | 41 | this->~NamespaceDecl(); |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 42 | C.getAllocator().Deallocate((void *)this); |
| 43 | } |
| 44 | |
| 45 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 46 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
| 47 | SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) { |
| 48 | void *Mem = C.getAllocator().Allocate<ImplicitParamDecl>(); |
| 49 | return new (Mem) ImplicitParamDecl(ImplicitParam, DC, L, Id, T, PrevDecl); |
| 50 | } |
| 51 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 52 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 53 | SourceLocation L, IdentifierInfo *Id, |
| 54 | QualType T, StorageClass S, |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 55 | Expr *DefArg, ScopedDecl *PrevDecl) { |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 56 | void *Mem = C.getAllocator().Allocate<ParmVarDecl>(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 57 | return new (Mem) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg, PrevDecl); |
| 58 | } |
| 59 | |
| 60 | QualType ParmVarDecl::getOriginalType() const { |
| 61 | if (const ParmVarWithOriginalTypeDecl *PVD = |
| 62 | dyn_cast<ParmVarWithOriginalTypeDecl>(this)) |
| 63 | return PVD->OriginalType; |
| 64 | return getType(); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Fariborz Jahanian | 73da9e4 | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 67 | ParmVarWithOriginalTypeDecl *ParmVarWithOriginalTypeDecl::Create( |
| 68 | ASTContext &C, DeclContext *DC, |
| 69 | SourceLocation L, IdentifierInfo *Id, |
| 70 | QualType T, QualType OT, StorageClass S, |
| 71 | Expr *DefArg, ScopedDecl *PrevDecl) { |
| 72 | void *Mem = C.getAllocator().Allocate<ParmVarWithOriginalTypeDecl>(); |
| 73 | return new (Mem) ParmVarWithOriginalTypeDecl(DC, L, Id, T, OT, S, |
| 74 | DefArg, PrevDecl); |
| 75 | } |
| 76 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 77 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 78 | SourceLocation L, |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 79 | DeclarationName N, QualType T, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 80 | StorageClass S, bool isInline, |
Steve Naroff | 0eb07bf | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 81 | ScopedDecl *PrevDecl, |
| 82 | SourceLocation TypeSpecStartLoc) { |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 83 | void *Mem = C.getAllocator().Allocate<FunctionDecl>(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 84 | return new (Mem) FunctionDecl(Function, DC, L, N, T, S, isInline, PrevDecl, |
Steve Naroff | 0eb07bf | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 85 | TypeSpecStartLoc); |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 88 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 89 | void *Mem = C.getAllocator().Allocate<BlockDecl>(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 90 | return new (Mem) BlockDecl(DC, L); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 93 | FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 94 | IdentifierInfo *Id, QualType T, Expr *BW, |
| 95 | bool Mutable, ScopedDecl *PrevDecl) { |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 96 | void *Mem = C.getAllocator().Allocate<FieldDecl>(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 97 | return new (Mem) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable, PrevDecl); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 100 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 101 | if (!isImplicit() || getDeclName()) |
| 102 | return false; |
| 103 | |
| 104 | if (const RecordType *Record = getType()->getAsRecordType()) |
| 105 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 106 | |
| 107 | return false; |
| 108 | } |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 110 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 111 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 112 | IdentifierInfo *Id, QualType T, |
| 113 | Expr *E, const llvm::APSInt &V, |
| 114 | ScopedDecl *PrevDecl){ |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 115 | void *Mem = C.getAllocator().Allocate<EnumConstantDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 116 | return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 119 | void EnumConstantDecl::Destroy(ASTContext& C) { |
| 120 | if (Init) Init->Destroy(C); |
| 121 | Decl::Destroy(C); |
| 122 | } |
| 123 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 124 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 125 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 126 | IdentifierInfo *Id, QualType T, |
| 127 | ScopedDecl *PD) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 128 | void *Mem = C.getAllocator().Allocate<TypedefDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 129 | return new (Mem) TypedefDecl(DC, L, Id, T, PD); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 132 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 133 | IdentifierInfo *Id, |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 134 | EnumDecl *PrevDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 135 | void *Mem = C.getAllocator().Allocate<EnumDecl>(); |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 136 | EnumDecl *Enum = new (Mem) EnumDecl(DC, L, Id, 0); |
| 137 | C.getTypeDeclType(Enum, PrevDecl); |
| 138 | return Enum; |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Ted Kremenek | df91eca | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 141 | void EnumDecl::Destroy(ASTContext& C) { |
Ted Kremenek | df91eca | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 142 | Decl::Destroy(C); |
| 143 | } |
| 144 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 145 | void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) { |
| 146 | assert(!isDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 147 | IntegerType = NewType; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame^] | 148 | TagDecl::completeDefinition(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 151 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, |
| 152 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 153 | StringLiteral *Str) { |
| 154 | void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>(); |
| 155 | return new (Mem) FileScopeAsmDecl(L, Str); |
| 156 | } |
| 157 | |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 158 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 159 | // ScopedDecl Implementation |
| 160 | //===----------------------------------------------------------------------===// |
| 161 | |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 162 | void ScopedDecl::setDeclContext(DeclContext *DC) { |
| 163 | if (isOutOfSemaDC()) |
| 164 | delete getMultipleDC(); |
| 165 | |
| 166 | DeclCtx = reinterpret_cast<uintptr_t>(DC); |
| 167 | } |
| 168 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 169 | void ScopedDecl::setLexicalDeclContext(DeclContext *DC) { |
| 170 | if (DC == getLexicalDeclContext()) |
| 171 | return; |
| 172 | |
| 173 | if (isInSemaDC()) { |
| 174 | MultipleDC *MDC = new MultipleDC(); |
| 175 | MDC->SemanticDC = getDeclContext(); |
| 176 | MDC->LexicalDC = DC; |
| 177 | DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1; |
| 178 | } else { |
| 179 | getMultipleDC()->LexicalDC = DC; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | ScopedDecl::~ScopedDecl() { |
| 184 | if (isOutOfSemaDC()) |
| 185 | delete getMultipleDC(); |
| 186 | } |
| 187 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 188 | bool ScopedDecl::declarationReplaces(NamedDecl *OldD) const { |
| 189 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 190 | |
| 191 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 192 | // For function declarations, we keep track of redeclarations. |
| 193 | return FD->getPreviousDeclaration() == OldD; |
| 194 | |
| 195 | // For non-function declarations, if the declarations are of the |
| 196 | // same kind then this must be a redeclaration, or semantic analysis |
| 197 | // would not have given us the new declaration. |
| 198 | return this->getKind() == OldD->getKind(); |
| 199 | } |
| 200 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 201 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 202 | // VarDecl Implementation |
| 203 | //===----------------------------------------------------------------------===// |
| 204 | |
| 205 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
| 206 | SourceLocation L, |
| 207 | IdentifierInfo *Id, QualType T, |
| 208 | StorageClass S, ScopedDecl *PrevDecl, |
| 209 | SourceLocation TypeSpecStartLoc) { |
| 210 | void *Mem = C.getAllocator().Allocate<VarDecl>(); |
| 211 | return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl, TypeSpecStartLoc); |
| 212 | } |
| 213 | |
| 214 | void VarDecl::Destroy(ASTContext& C) { |
| 215 | this->~VarDecl(); |
| 216 | C.getAllocator().Deallocate((void *)this); |
| 217 | } |
| 218 | |
| 219 | VarDecl::~VarDecl() { |
| 220 | delete getInit(); |
| 221 | } |
| 222 | |
| 223 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 224 | // FunctionDecl Implementation |
| 225 | //===----------------------------------------------------------------------===// |
| 226 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 227 | FunctionDecl::~FunctionDecl() { |
| 228 | delete[] ParamInfo; |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 231 | void FunctionDecl::Destroy(ASTContext& C) { |
Ted Kremenek | b65cf41 | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 232 | if (Body) |
| 233 | Body->Destroy(C); |
| 234 | |
| 235 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 236 | (*I)->Destroy(C); |
| 237 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 238 | Decl::Destroy(C); |
| 239 | } |
| 240 | |
| 241 | |
Douglas Gregor | f009795 | 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Ted Kremenek | 4f03fd6 | 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 | d8bdba5 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 256 | if (isa<FunctionTypeNoProto>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 257 | return 0; |
Chris Lattner | d8bdba5 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 258 | return cast<FunctionTypeProto>(FT)->getNumArgs(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Ted Kremenek | 4f03fd6 | 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 | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 269 | void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 270 | unsigned NumParams) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 271 | assert(ParamInfo == 0 && "Already has param info!"); |
Ted Kremenek | 4f03fd6 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 272 | assert(NumParams == getNumTypeParams(getType()) && |
| 273 | "Parameter count mismatch!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | |
| 275 | // Zero params -> null pointer. |
| 276 | if (NumParams) { |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 277 | void *Mem = C.getAllocator().Allocate<ParmVarDecl*>(NumParams); |
| 278 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 280 | } |
| 281 | } |
| 282 | |
Chris Lattner | 8123a95 | 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 | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 286 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 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 | 1cd1b1e | 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 | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 299 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 300 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 301 | else |
| 302 | return OO_None; |
| 303 | } |
| 304 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 305 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 306 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 307 | //===----------------------------------------------------------------------===// |
| 308 | |
Douglas Gregor | 0b7a158 | 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 | 4b7c983 | 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 | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 330 | // RecordDecl Implementation |
| 331 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 332 | |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 333 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 334 | IdentifierInfo *Id) |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 335 | : TagDecl(DK, TK, DC, L, Id, 0) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 336 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 337 | AnonymousStructOrUnion = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 338 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 342 | SourceLocation L, IdentifierInfo *Id, |
| 343 | RecordDecl* PrevDecl) { |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 344 | |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 345 | void *Mem = C.getAllocator().Allocate<RecordDecl>(); |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 346 | RecordDecl* R = new (Mem) RecordDecl(Record, TK, DC, L, Id); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 347 | C.getTypeDeclType(R, PrevDecl); |
| 348 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 351 | RecordDecl::~RecordDecl() { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void RecordDecl::Destroy(ASTContext& C) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 355 | DeclContext::DestroyDecls(C); |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 356 | TagDecl::Destroy(C); |
| 357 | } |
| 358 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 359 | /// completeDefinition - Notes that the definition of this type is now |
| 360 | /// complete. |
| 361 | void RecordDecl::completeDefinition(ASTContext& C) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame^] | 363 | TagDecl::completeDefinition(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 366 | //===----------------------------------------------------------------------===// |
| 367 | // BlockDecl Implementation |
| 368 | //===----------------------------------------------------------------------===// |
| 369 | |
| 370 | BlockDecl::~BlockDecl() { |
| 371 | } |
| 372 | |
| 373 | void BlockDecl::Destroy(ASTContext& C) { |
| 374 | if (Body) |
| 375 | Body->Destroy(C); |
| 376 | |
| 377 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 378 | (*I)->Destroy(C); |
| 379 | |
| 380 | Decl::Destroy(C); |
| 381 | } |