blob: c8b7fc2dcf982f696a13174f3a784a6f66957dc8 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000017#include "clang/AST/Expr.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000018#include "clang/Basic/IdentifierTable.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000019
Chris Lattner6d9a6852006-10-25 05:11:20 +000020using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000021
Chris Lattner88f70d62008-03-15 05:43:15 +000022//===----------------------------------------------------------------------===//
Chris Lattnera7b32872008-03-15 06:12:44 +000023// Decl Allocation/Deallocation Method Implementations
24//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000025
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000026TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000027 return new (C) TranslationUnitDecl();
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000028}
29
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000030NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
31 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000032 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000033}
34
Ted Kremenek78aa98f2008-05-20 04:49:55 +000035void NamespaceDecl::Destroy(ASTContext& C) {
36 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
37 // together. They are all top-level Decls.
38
Ted Kremeneka08154d2008-05-24 15:09:56 +000039 this->~NamespaceDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +000040 C.Deallocate((void *)this);
Ted Kremenek78aa98f2008-05-20 04:49:55 +000041}
42
43
Chris Lattner5696e7b2008-06-17 18:05:57 +000044ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor6e6ad602009-01-20 01:17:11 +000045 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000046 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner5696e7b2008-06-17 18:05:57 +000047}
48
Chris Lattnerbec41342008-04-22 18:39:57 +000049ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000050 SourceLocation L, IdentifierInfo *Id,
51 QualType T, StorageClass S,
Douglas Gregor6e6ad602009-01-20 01:17:11 +000052 Expr *DefArg) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000053 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahaniana0befc02008-12-20 23:29:59 +000054}
55
56QualType ParmVarDecl::getOriginalType() const {
57 if (const ParmVarWithOriginalTypeDecl *PVD =
58 dyn_cast<ParmVarWithOriginalTypeDecl>(this))
59 return PVD->OriginalType;
60 return getType();
Chris Lattner4b08ca82008-03-15 21:10:16 +000061}
62
Fariborz Jahanian7664ffb2008-12-20 20:56:12 +000063ParmVarWithOriginalTypeDecl *ParmVarWithOriginalTypeDecl::Create(
64 ASTContext &C, DeclContext *DC,
65 SourceLocation L, IdentifierInfo *Id,
66 QualType T, QualType OT, StorageClass S,
Douglas Gregor6e6ad602009-01-20 01:17:11 +000067 Expr *DefArg) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000068 return new (C) ParmVarWithOriginalTypeDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian7664ffb2008-12-20 20:56:12 +000069}
70
Chris Lattnerbec41342008-04-22 18:39:57 +000071FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000072 SourceLocation L,
Douglas Gregor92751d42008-11-17 22:58:34 +000073 DeclarationName N, QualType T,
Chris Lattner5072bae2008-03-15 21:24:04 +000074 StorageClass S, bool isInline,
Steve Naroff22315692008-10-03 00:02:03 +000075 SourceLocation TypeSpecStartLoc) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000076 return new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
Steve Naroff22315692008-10-03 00:02:03 +000077 TypeSpecStartLoc);
Chris Lattner5072bae2008-03-15 21:24:04 +000078}
79
Steve Naroff1d95e5a2008-10-10 01:28:17 +000080BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000081 return new (C) BlockDecl(DC, L);
Steve Naroff415d3d52008-10-08 17:01:13 +000082}
83
Douglas Gregor91f84212008-12-11 16:49:14 +000084FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
85 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregor6e6ad602009-01-20 01:17:11 +000086 bool Mutable) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000087 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattneree1284a2008-03-16 00:16:02 +000088}
89
Douglas Gregorf4d33272009-01-07 19:46:03 +000090bool FieldDecl::isAnonymousStructOrUnion() const {
91 if (!isImplicit() || getDeclName())
92 return false;
93
94 if (const RecordType *Record = getType()->getAsRecordType())
95 return Record->getDecl()->isAnonymousStructOrUnion();
96
97 return false;
98}
Chris Lattner5072bae2008-03-15 21:24:04 +000099
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000100EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
101 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000102 IdentifierInfo *Id, QualType T,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000103 Expr *E, const llvm::APSInt &V) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000104 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnera7b32872008-03-15 06:12:44 +0000105}
106
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000107void EnumConstantDecl::Destroy(ASTContext& C) {
108 if (Init) Init->Destroy(C);
109 Decl::Destroy(C);
110}
111
Chris Lattnerbec41342008-04-22 18:39:57 +0000112TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000113 SourceLocation L,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000114 IdentifierInfo *Id, QualType T) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000115 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnera7b32872008-03-15 06:12:44 +0000116}
117
Chris Lattnerbec41342008-04-22 18:39:57 +0000118EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000119 IdentifierInfo *Id,
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000120 EnumDecl *PrevDecl) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000121 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000122 EnumDecl *Enum = new (Mem) EnumDecl(DC, L, Id);
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000123 C.getTypeDeclType(Enum, PrevDecl);
124 return Enum;
Chris Lattnera7b32872008-03-15 06:12:44 +0000125}
126
Ted Kremenek123f0252008-09-02 20:13:32 +0000127void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek123f0252008-09-02 20:13:32 +0000128 Decl::Destroy(C);
129}
130
Douglas Gregor91f84212008-12-11 16:49:14 +0000131void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
132 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor91f84212008-12-11 16:49:14 +0000133 IntegerType = NewType;
Douglas Gregordee1be82009-01-17 00:42:38 +0000134 TagDecl::completeDefinition();
Douglas Gregor91f84212008-12-11 16:49:14 +0000135}
136
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000137FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000138 SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000139 StringLiteral *Str) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000140 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattneree1284a2008-03-16 00:16:02 +0000141}
142
Chris Lattnera7b32872008-03-15 06:12:44 +0000143//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000144// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000145//===----------------------------------------------------------------------===//
146
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000147bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000148 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
149
150 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
151 // For function declarations, we keep track of redeclarations.
152 return FD->getPreviousDeclaration() == OldD;
153
154 // For non-function declarations, if the declarations are of the
155 // same kind then this must be a redeclaration, or semantic analysis
156 // would not have given us the new declaration.
157 return this->getKind() == OldD->getKind();
158}
159
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000160
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000161//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000162// VarDecl Implementation
163//===----------------------------------------------------------------------===//
164
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000165VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
166 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopes394ec982008-12-17 23:39:55 +0000167 SourceLocation TypeSpecStartLoc) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000168 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopes394ec982008-12-17 23:39:55 +0000169}
170
171void VarDecl::Destroy(ASTContext& C) {
172 this->~VarDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +0000173 C.Deallocate((void *)this);
Nuno Lopes394ec982008-12-17 23:39:55 +0000174}
175
176VarDecl::~VarDecl() {
177 delete getInit();
178}
179
180//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000181// FunctionDecl Implementation
182//===----------------------------------------------------------------------===//
183
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000184void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000185 if (Body)
186 Body->Destroy(C);
187
188 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
189 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000190
Steve Naroff13ae6f42009-01-27 21:25:57 +0000191 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000192
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000193 Decl::Destroy(C);
194}
195
196
Douglas Gregor89f238c2008-04-21 02:02:58 +0000197Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
198 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
199 if (FD->Body) {
200 Definition = FD;
201 return FD->Body;
202 }
203 }
204
205 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000206}
207
Ted Kremenek82c1cc32008-10-29 18:41:34 +0000208// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
209static unsigned getNumTypeParams(QualType T) {
210 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000211 if (isa<FunctionTypeNoProto>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000212 return 0;
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000213 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000214}
215
Ted Kremenek82c1cc32008-10-29 18:41:34 +0000216unsigned FunctionDecl::getNumParams() const {
217 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
218 if (!ParamInfo)
219 return 0;
220
221 return getNumTypeParams(getType());
222}
223
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000224void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
225 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000226 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek82c1cc32008-10-29 18:41:34 +0000227 assert(NumParams == getNumTypeParams(getType()) &&
228 "Parameter count mismatch!");
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000229
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000230 // Zero params -> null pointer.
231 if (NumParams) {
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000232 void *Mem = C.getAllocator().Allocate<ParmVarDecl*>(NumParams);
233 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000234 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000235 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000236}
Chris Lattner41943152007-01-25 04:52:46 +0000237
Chris Lattner58258242008-04-10 02:22:51 +0000238/// getMinRequiredArguments - Returns the minimum number of arguments
239/// needed to call this function. This may be fewer than the number of
240/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000241/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000242unsigned FunctionDecl::getMinRequiredArguments() const {
243 unsigned NumRequiredArgs = getNumParams();
244 while (NumRequiredArgs > 0
245 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
246 --NumRequiredArgs;
247
248 return NumRequiredArgs;
249}
250
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000251/// getOverloadedOperator - Which C++ overloaded operator this
252/// function represents, if any.
253OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +0000254 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
255 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000256 else
257 return OO_None;
258}
259
Chris Lattner59a25942008-03-31 00:36:02 +0000260//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000261// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +0000262//===----------------------------------------------------------------------===//
263
Douglas Gregordee1be82009-01-17 00:42:38 +0000264void TagDecl::startDefinition() {
265 cast<TagType>(TypeForDecl)->decl.setPointer(this);
266 cast<TagType>(TypeForDecl)->decl.setInt(1);
267}
268
269void TagDecl::completeDefinition() {
270 assert((!TypeForDecl ||
271 cast<TagType>(TypeForDecl)->decl.getPointer() == this) &&
272 "Attempt to redefine a tag definition?");
273 IsDefinition = true;
274 cast<TagType>(TypeForDecl)->decl.setPointer(this);
275 cast<TagType>(TypeForDecl)->decl.setInt(0);
276}
277
Ted Kremenek21475702008-09-05 17:16:31 +0000278TagDecl* TagDecl::getDefinition(ASTContext& C) const {
279 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
280 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
281 return D->isDefinition() ? D : 0;
282}
283
284//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000285// RecordDecl Implementation
286//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000287
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +0000288RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek47923c72008-09-05 01:34:33 +0000289 IdentifierInfo *Id)
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000290 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek52baf502008-09-02 21:12:32 +0000291 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000292 AnonymousStructOrUnion = false;
Ted Kremenek52baf502008-09-02 21:12:32 +0000293 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +0000294}
295
296RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +0000297 SourceLocation L, IdentifierInfo *Id,
298 RecordDecl* PrevDecl) {
Ted Kremenek47923c72008-09-05 01:34:33 +0000299
Steve Naroff13ae6f42009-01-27 21:25:57 +0000300 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek21475702008-09-05 17:16:31 +0000301 C.getTypeDeclType(R, PrevDecl);
302 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +0000303}
304
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000305RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000306}
307
308void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000309 TagDecl::Destroy(C);
310}
311
Douglas Gregor91f84212008-12-11 16:49:14 +0000312/// completeDefinition - Notes that the definition of this type is now
313/// complete.
314void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +0000315 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +0000316 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +0000317}
Steve Naroffcc321422007-03-26 23:09:51 +0000318
Steve Naroff415d3d52008-10-08 17:01:13 +0000319//===----------------------------------------------------------------------===//
320// BlockDecl Implementation
321//===----------------------------------------------------------------------===//
322
323BlockDecl::~BlockDecl() {
324}
325
326void BlockDecl::Destroy(ASTContext& C) {
327 if (Body)
328 Body->Destroy(C);
329
330 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
331 (*I)->Destroy(C);
332
333 Decl::Destroy(C);
334}