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" |
| 17 | #include "clang/Basic/IdentifierTable.h" |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 18 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 22 | // Decl Allocation/Deallocation Method Implementations |
| 23 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 24 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 25 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 26 | void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>(); |
| 27 | return new (Mem) TranslationUnitDecl(); |
| 28 | } |
| 29 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 30 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 31 | SourceLocation L, IdentifierInfo *Id) { |
| 32 | void *Mem = C.getAllocator().Allocate<NamespaceDecl>(); |
| 33 | return new (Mem) NamespaceDecl(DC, L, Id); |
| 34 | } |
| 35 | |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 36 | void NamespaceDecl::Destroy(ASTContext& C) { |
| 37 | // NamespaceDecl uses "NextDeclarator" to chain namespace declarations |
| 38 | // together. They are all top-level Decls. |
| 39 | |
Ted Kremenek | ebf27b1 | 2008-05-24 15:09:56 +0000 | [diff] [blame] | 40 | this->~NamespaceDecl(); |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 41 | C.getAllocator().Deallocate((void *)this); |
| 42 | } |
| 43 | |
| 44 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 45 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
| 46 | SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) { |
| 47 | void *Mem = C.getAllocator().Allocate<ImplicitParamDecl>(); |
| 48 | return new (Mem) ImplicitParamDecl(ImplicitParam, DC, L, Id, T, PrevDecl); |
| 49 | } |
| 50 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 51 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 52 | SourceLocation L, |
| 53 | IdentifierInfo *Id, QualType T, |
Steve Naroff | 0eb07bf | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 54 | StorageClass S, ScopedDecl *PrevDecl, |
| 55 | SourceLocation TypeSpecStartLoc) { |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 56 | void *Mem = C.getAllocator().Allocate<VarDecl>(); |
Steve Naroff | 0eb07bf | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 57 | return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl, TypeSpecStartLoc); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 60 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 61 | SourceLocation L, IdentifierInfo *Id, |
| 62 | QualType T, StorageClass S, |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 63 | Expr *DefArg, ScopedDecl *PrevDecl) { |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 64 | void *Mem = C.getAllocator().Allocate<ParmVarDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 65 | return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 68 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 69 | SourceLocation L, |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 70 | DeclarationName N, QualType T, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 71 | StorageClass S, bool isInline, |
Steve Naroff | 0eb07bf | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 72 | ScopedDecl *PrevDecl, |
| 73 | SourceLocation TypeSpecStartLoc) { |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 74 | void *Mem = C.getAllocator().Allocate<FunctionDecl>(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 75 | return new (Mem) FunctionDecl(Function, DC, L, N, T, S, isInline, PrevDecl, |
Steve Naroff | 0eb07bf | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 76 | TypeSpecStartLoc); |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 79 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 80 | void *Mem = C.getAllocator().Allocate<BlockDecl>(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 81 | return new (Mem) BlockDecl(DC, L); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 84 | FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 85 | IdentifierInfo *Id, QualType T, Expr *BW) { |
| 86 | void *Mem = C.getAllocator().Allocate<FieldDecl>(); |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 87 | return new (Mem) FieldDecl(L, Id, T, BW); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 91 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 92 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 93 | IdentifierInfo *Id, QualType T, |
| 94 | Expr *E, const llvm::APSInt &V, |
| 95 | ScopedDecl *PrevDecl){ |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 96 | void *Mem = C.getAllocator().Allocate<EnumConstantDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 97 | return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 100 | void EnumConstantDecl::Destroy(ASTContext& C) { |
| 101 | if (Init) Init->Destroy(C); |
| 102 | Decl::Destroy(C); |
| 103 | } |
| 104 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 105 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 106 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 107 | IdentifierInfo *Id, QualType T, |
| 108 | ScopedDecl *PD) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 109 | void *Mem = C.getAllocator().Allocate<TypedefDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 110 | return new (Mem) TypedefDecl(DC, L, Id, T, PD); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 113 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 114 | IdentifierInfo *Id, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 115 | ScopedDecl *PrevDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 116 | void *Mem = C.getAllocator().Allocate<EnumDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 117 | return new (Mem) EnumDecl(DC, L, Id, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Ted Kremenek | df91eca | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 120 | void EnumDecl::Destroy(ASTContext& C) { |
| 121 | if (getEnumConstantList()) getEnumConstantList()->Destroy(C); |
| 122 | Decl::Destroy(C); |
| 123 | } |
| 124 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 125 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, |
| 126 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 127 | StringLiteral *Str) { |
| 128 | void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>(); |
| 129 | return new (Mem) FileScopeAsmDecl(L, Str); |
| 130 | } |
| 131 | |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 132 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 133 | // ScopedDecl Implementation |
| 134 | //===----------------------------------------------------------------------===// |
| 135 | |
| 136 | void ScopedDecl::setLexicalDeclContext(DeclContext *DC) { |
| 137 | if (DC == getLexicalDeclContext()) |
| 138 | return; |
| 139 | |
| 140 | if (isInSemaDC()) { |
| 141 | MultipleDC *MDC = new MultipleDC(); |
| 142 | MDC->SemanticDC = getDeclContext(); |
| 143 | MDC->LexicalDC = DC; |
| 144 | DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1; |
| 145 | } else { |
| 146 | getMultipleDC()->LexicalDC = DC; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | ScopedDecl::~ScopedDecl() { |
| 151 | if (isOutOfSemaDC()) |
| 152 | delete getMultipleDC(); |
| 153 | } |
| 154 | |
| 155 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 156 | // FunctionDecl Implementation |
| 157 | //===----------------------------------------------------------------------===// |
| 158 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | FunctionDecl::~FunctionDecl() { |
| 160 | delete[] ParamInfo; |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 163 | void FunctionDecl::Destroy(ASTContext& C) { |
Ted Kremenek | b65cf41 | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 164 | if (Body) |
| 165 | Body->Destroy(C); |
| 166 | |
| 167 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 168 | (*I)->Destroy(C); |
| 169 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 170 | Decl::Destroy(C); |
| 171 | } |
| 172 | |
| 173 | |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 174 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
| 175 | for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) { |
| 176 | if (FD->Body) { |
| 177 | Definition = FD; |
| 178 | return FD->Body; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Ted Kremenek | 4f03fd6 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 185 | // Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams() |
| 186 | static unsigned getNumTypeParams(QualType T) { |
| 187 | const FunctionType *FT = T->getAsFunctionType(); |
Chris Lattner | d8bdba5 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 188 | if (isa<FunctionTypeNoProto>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 189 | return 0; |
Chris Lattner | d8bdba5 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 190 | return cast<FunctionTypeProto>(FT)->getNumArgs(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Ted Kremenek | 4f03fd6 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 193 | unsigned FunctionDecl::getNumParams() const { |
| 194 | // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar; |
| 195 | if (!ParamInfo) |
| 196 | return 0; |
| 197 | |
| 198 | return getNumTypeParams(getType()); |
| 199 | } |
| 200 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
| 202 | assert(ParamInfo == 0 && "Already has param info!"); |
Ted Kremenek | 4f03fd6 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 203 | assert(NumParams == getNumTypeParams(getType()) && |
| 204 | "Parameter count mismatch!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 205 | |
| 206 | // Zero params -> null pointer. |
| 207 | if (NumParams) { |
| 208 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 209 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 210 | } |
| 211 | } |
| 212 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 213 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 214 | /// needed to call this function. This may be fewer than the number of |
| 215 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 216 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 217 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 218 | unsigned NumRequiredArgs = getNumParams(); |
| 219 | while (NumRequiredArgs > 0 |
| 220 | && getParamDecl(NumRequiredArgs-1)->getDefaultArg()) |
| 221 | --NumRequiredArgs; |
| 222 | |
| 223 | return NumRequiredArgs; |
| 224 | } |
| 225 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 226 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 227 | /// function represents, if any. |
| 228 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 229 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 230 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 231 | else |
| 232 | return OO_None; |
| 233 | } |
| 234 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 235 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 236 | // TagdDecl Implementation |
| 237 | //===----------------------------------------------------------------------===// |
| 238 | |
| 239 | TagDecl* TagDecl::getDefinition(ASTContext& C) const { |
| 240 | QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this)); |
| 241 | TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl()); |
| 242 | return D->isDefinition() ? D : 0; |
| 243 | } |
| 244 | |
| 245 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 246 | // RecordDecl Implementation |
| 247 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 249 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 250 | IdentifierInfo *Id) |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 251 | : TagDecl(DK, TK, DC, L, Id, 0) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 252 | |
| 253 | HasFlexibleArrayMember = false; |
| 254 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
| 255 | Members = 0; |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 256 | NumMembers = -1; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 260 | SourceLocation L, IdentifierInfo *Id, |
| 261 | RecordDecl* PrevDecl) { |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 262 | |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 263 | void *Mem = C.getAllocator().Allocate<RecordDecl>(); |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 264 | RecordDecl* R = new (Mem) RecordDecl(Record, TK, DC, L, Id); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 265 | C.getTypeDeclType(R, PrevDecl); |
| 266 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 269 | RecordDecl::~RecordDecl() { |
| 270 | delete[] Members; |
| 271 | } |
| 272 | |
| 273 | void RecordDecl::Destroy(ASTContext& C) { |
| 274 | if (isDefinition()) |
| 275 | for (field_iterator I=field_begin(), E=field_end(); I!=E; ++I) |
| 276 | (*I)->Destroy(C); |
| 277 | |
| 278 | TagDecl::Destroy(C); |
| 279 | } |
| 280 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | /// defineBody - When created, RecordDecl's correspond to a forward declared |
| 282 | /// record. This method is used to mark the decl as being defined, with the |
| 283 | /// specified contents. |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 284 | void RecordDecl::defineBody(ASTContext& C, FieldDecl **members, |
| 285 | unsigned numMembers) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | assert(!isDefinition() && "Cannot redefine record!"); |
| 287 | setDefinition(true); |
| 288 | NumMembers = numMembers; |
| 289 | if (numMembers) { |
| 290 | Members = new FieldDecl*[numMembers]; |
| 291 | memcpy(Members, members, numMembers*sizeof(Decl*)); |
| 292 | } |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 293 | |
| 294 | // Let ASTContext know that this is the defining RecordDecl this type. |
| 295 | C.setTagDefinition(this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 299 | FieldDecl *RecordDecl::getMember(IdentifierInfo *II) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 300 | if (Members == 0 || NumMembers < 0) |
| 301 | return 0; |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 303 | // Linear search. When C++ classes come along, will likely need to revisit. |
| 304 | for (int i = 0; i != NumMembers; ++i) |
| 305 | if (Members[i]->getIdentifier() == II) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 306 | return Members[i]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | return 0; |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 308 | } |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 309 | |
| 310 | //===----------------------------------------------------------------------===// |
| 311 | // BlockDecl Implementation |
| 312 | //===----------------------------------------------------------------------===// |
| 313 | |
| 314 | BlockDecl::~BlockDecl() { |
| 315 | } |
| 316 | |
| 317 | void BlockDecl::Destroy(ASTContext& C) { |
| 318 | if (Body) |
| 319 | Body->Destroy(C); |
| 320 | |
| 321 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 322 | (*I)->Destroy(C); |
| 323 | |
| 324 | Decl::Destroy(C); |
| 325 | } |