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" |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 16 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 20 | // Decl Allocation/Deallocation Method Implementations |
| 21 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 22 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 23 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 24 | void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>(); |
| 25 | return new (Mem) TranslationUnitDecl(); |
| 26 | } |
| 27 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 28 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 29 | SourceLocation L, IdentifierInfo *Id) { |
| 30 | void *Mem = C.getAllocator().Allocate<NamespaceDecl>(); |
| 31 | return new (Mem) NamespaceDecl(DC, L, Id); |
| 32 | } |
| 33 | |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 34 | void NamespaceDecl::Destroy(ASTContext& C) { |
| 35 | // NamespaceDecl uses "NextDeclarator" to chain namespace declarations |
| 36 | // together. They are all top-level Decls. |
| 37 | |
Ted Kremenek | ebf27b1 | 2008-05-24 15:09:56 +0000 | [diff] [blame] | 38 | this->~NamespaceDecl(); |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 39 | C.getAllocator().Deallocate((void *)this); |
| 40 | } |
| 41 | |
| 42 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 43 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
| 44 | SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) { |
| 45 | void *Mem = C.getAllocator().Allocate<ImplicitParamDecl>(); |
| 46 | return new (Mem) ImplicitParamDecl(ImplicitParam, DC, L, Id, T, PrevDecl); |
| 47 | } |
| 48 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 49 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 50 | SourceLocation L, |
| 51 | IdentifierInfo *Id, QualType T, |
| 52 | StorageClass S, ScopedDecl *PrevDecl) { |
| 53 | void *Mem = C.getAllocator().Allocate<VarDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 54 | return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 57 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 58 | SourceLocation L, IdentifierInfo *Id, |
| 59 | QualType T, StorageClass S, |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 60 | Expr *DefArg, ScopedDecl *PrevDecl) { |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 61 | void *Mem = C.getAllocator().Allocate<ParmVarDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 62 | return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 65 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 66 | SourceLocation L, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 67 | IdentifierInfo *Id, QualType T, |
| 68 | StorageClass S, bool isInline, |
| 69 | ScopedDecl *PrevDecl) { |
| 70 | void *Mem = C.getAllocator().Allocate<FunctionDecl>(); |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 71 | return new (Mem) FunctionDecl(Function, DC, L, Id, T, S, isInline, PrevDecl); |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 74 | FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 75 | IdentifierInfo *Id, QualType T, Expr *BW) { |
| 76 | void *Mem = C.getAllocator().Allocate<FieldDecl>(); |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 77 | return new (Mem) FieldDecl(L, Id, T, BW); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 81 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 82 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 83 | IdentifierInfo *Id, QualType T, |
| 84 | Expr *E, const llvm::APSInt &V, |
| 85 | ScopedDecl *PrevDecl){ |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 86 | void *Mem = C.getAllocator().Allocate<EnumConstantDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 87 | return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 90 | void EnumConstantDecl::Destroy(ASTContext& C) { |
| 91 | if (Init) Init->Destroy(C); |
| 92 | Decl::Destroy(C); |
| 93 | } |
| 94 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 95 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 96 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 97 | IdentifierInfo *Id, QualType T, |
| 98 | ScopedDecl *PD) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 99 | void *Mem = C.getAllocator().Allocate<TypedefDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 100 | return new (Mem) TypedefDecl(DC, L, Id, T, PD); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 103 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 104 | IdentifierInfo *Id, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 105 | ScopedDecl *PrevDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 106 | void *Mem = C.getAllocator().Allocate<EnumDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 107 | return new (Mem) EnumDecl(DC, L, Id, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 110 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 111 | SourceLocation L, IdentifierInfo *Id, |
| 112 | ScopedDecl *PrevDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 113 | void *Mem = C.getAllocator().Allocate<RecordDecl>(); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 114 | Kind DK; |
| 115 | switch (TK) { |
Ted Kremenek | 9bcf44a | 2008-06-16 23:45:12 +0000 | [diff] [blame] | 116 | default: assert(0 && "Invalid TagKind!"); |
| 117 | case TK_enum: assert(0 && "Enum TagKind passed for Record!"); |
| 118 | case TK_struct: DK = Struct; break; |
| 119 | case TK_union: DK = Union; break; |
| 120 | case TK_class: DK = Class; break; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 121 | } |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 122 | return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 125 | void EnumDecl::Destroy(ASTContext& C) { |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 126 | if (getEnumConstantList()) getEnumConstantList()->Destroy(C); |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 127 | Decl::Destroy(C); |
| 128 | } |
| 129 | |
| 130 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 131 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, |
| 132 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 133 | StringLiteral *Str) { |
| 134 | void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>(); |
| 135 | return new (Mem) FileScopeAsmDecl(L, Str); |
| 136 | } |
| 137 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 138 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
| 139 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 140 | LanguageIDs Lang, Decl *D) { |
| 141 | void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>(); |
| 142 | return new (Mem) LinkageSpecDecl(L, Lang, D); |
| 143 | } |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 144 | |
| 145 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 146 | // NamedDecl Implementation |
| 147 | //===----------------------------------------------------------------------===// |
| 148 | |
Chris Lattner | fd5de47 | 2007-10-06 22:53:46 +0000 | [diff] [blame] | 149 | const char *NamedDecl::getName() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | if (const IdentifierInfo *II = getIdentifier()) |
| 151 | return II->getName(); |
| 152 | return ""; |
| 153 | } |
| 154 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 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 | |
| 185 | unsigned FunctionDecl::getNumParams() const { |
Chris Lattner | d8bdba5 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 186 | const FunctionType *FT = getType()->getAsFunctionType(); |
| 187 | if (isa<FunctionTypeNoProto>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 188 | return 0; |
Chris Lattner | d8bdba5 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 189 | return cast<FunctionTypeProto>(FT)->getNumArgs(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
| 193 | assert(ParamInfo == 0 && "Already has param info!"); |
| 194 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
| 195 | |
| 196 | // Zero params -> null pointer. |
| 197 | if (NumParams) { |
| 198 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 199 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 200 | } |
| 201 | } |
| 202 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 203 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 204 | /// needed to call this function. This may be fewer than the number of |
| 205 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 206 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 207 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 208 | unsigned NumRequiredArgs = getNumParams(); |
| 209 | while (NumRequiredArgs > 0 |
| 210 | && getParamDecl(NumRequiredArgs-1)->getDefaultArg()) |
| 211 | --NumRequiredArgs; |
| 212 | |
| 213 | return NumRequiredArgs; |
| 214 | } |
| 215 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 216 | //===----------------------------------------------------------------------===// |
| 217 | // RecordDecl Implementation |
| 218 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame^] | 220 | RecordDecl::~RecordDecl() { |
| 221 | delete[] Members; |
| 222 | } |
| 223 | |
| 224 | void RecordDecl::Destroy(ASTContext& C) { |
| 225 | if (isDefinition()) |
| 226 | for (field_iterator I=field_begin(), E=field_end(); I!=E; ++I) |
| 227 | (*I)->Destroy(C); |
| 228 | |
| 229 | TagDecl::Destroy(C); |
| 230 | } |
| 231 | |
| 232 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 233 | /// defineBody - When created, RecordDecl's correspond to a forward declared |
| 234 | /// record. This method is used to mark the decl as being defined, with the |
| 235 | /// specified contents. |
| 236 | void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) { |
| 237 | assert(!isDefinition() && "Cannot redefine record!"); |
| 238 | setDefinition(true); |
| 239 | NumMembers = numMembers; |
| 240 | if (numMembers) { |
| 241 | Members = new FieldDecl*[numMembers]; |
| 242 | memcpy(Members, members, numMembers*sizeof(Decl*)); |
| 243 | } |
| 244 | } |
| 245 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 246 | FieldDecl *RecordDecl::getMember(IdentifierInfo *II) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | if (Members == 0 || NumMembers < 0) |
| 248 | return 0; |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 249 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 250 | // Linear search. When C++ classes come along, will likely need to revisit. |
| 251 | for (int i = 0; i != NumMembers; ++i) |
| 252 | if (Members[i]->getIdentifier() == II) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | return Members[i]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 254 | return 0; |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 255 | } |