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" |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/Stmt.h" |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 21 | #include <vector> |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 22 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 26 | // Decl Allocation/Deallocation Method Implementations |
| 27 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 28 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 29 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 30 | return new (C) TranslationUnitDecl(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 33 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 34 | SourceLocation L, IdentifierInfo *Id) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 35 | return new (C) NamespaceDecl(DC, L, Id); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 38 | void NamespaceDecl::Destroy(ASTContext& C) { |
| 39 | // NamespaceDecl uses "NextDeclarator" to chain namespace declarations |
| 40 | // together. They are all top-level Decls. |
| 41 | |
Ted Kremenek | ebf27b1 | 2008-05-24 15:09:56 +0000 | [diff] [blame] | 42 | this->~NamespaceDecl(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 43 | C.Deallocate((void *)this); |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 47 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 48 | SourceLocation L, IdentifierInfo *Id, QualType T) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 49 | return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 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, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 55 | Expr *DefArg) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 56 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | QualType ParmVarDecl::getOriginalType() const { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 60 | if (const OriginalParmVarDecl *PVD = |
| 61 | dyn_cast<OriginalParmVarDecl>(this)) |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 62 | return PVD->OriginalType; |
| 63 | return getType(); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 66 | OriginalParmVarDecl *OriginalParmVarDecl::Create( |
Fariborz Jahanian | 73da9e4 | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 67 | ASTContext &C, DeclContext *DC, |
| 68 | SourceLocation L, IdentifierInfo *Id, |
| 69 | QualType T, QualType OT, StorageClass S, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 70 | Expr *DefArg) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 71 | return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg); |
Fariborz Jahanian | 73da9e4 | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 74 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 75 | SourceLocation L, |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 76 | DeclarationName N, QualType T, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 77 | StorageClass S, bool isInline, |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 78 | bool hasPrototype, |
Steve Naroff | 0eb07bf | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 79 | SourceLocation TypeSpecStartLoc) { |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 80 | FunctionDecl *New |
| 81 | = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline, |
| 82 | TypeSpecStartLoc); |
| 83 | New->HasPrototype = hasPrototype; |
| 84 | return New; |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 87 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 88 | return new (C) BlockDecl(DC, L); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 91 | FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 92 | IdentifierInfo *Id, QualType T, Expr *BW, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 93 | bool Mutable) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 94 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 97 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 98 | if (!isImplicit() || getDeclName()) |
| 99 | return false; |
| 100 | |
| 101 | if (const RecordType *Record = getType()->getAsRecordType()) |
| 102 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 103 | |
| 104 | return false; |
| 105 | } |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 107 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 108 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 109 | IdentifierInfo *Id, QualType T, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 110 | Expr *E, const llvm::APSInt &V) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 111 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 114 | void EnumConstantDecl::Destroy(ASTContext& C) { |
| 115 | if (Init) Init->Destroy(C); |
| 116 | Decl::Destroy(C); |
| 117 | } |
| 118 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 119 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 120 | SourceLocation L, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 121 | IdentifierInfo *Id, QualType T) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 122 | return new (C) TypedefDecl(DC, L, Id, T); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 125 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 126 | IdentifierInfo *Id, |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 127 | EnumDecl *PrevDecl) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 128 | EnumDecl *Enum = new (C) EnumDecl(DC, L, Id); |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 129 | C.getTypeDeclType(Enum, PrevDecl); |
| 130 | return Enum; |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Ted Kremenek | df91eca | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 133 | void EnumDecl::Destroy(ASTContext& C) { |
Ted Kremenek | df91eca | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 134 | Decl::Destroy(C); |
| 135 | } |
| 136 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 137 | void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) { |
| 138 | assert(!isDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 139 | IntegerType = NewType; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 140 | TagDecl::completeDefinition(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 143 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 144 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 145 | StringLiteral *Str) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 146 | return new (C) FileScopeAsmDecl(DC, L, Str); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 149 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 150 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 151 | //===----------------------------------------------------------------------===// |
| 152 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 153 | std::string NamedDecl::getQualifiedNameAsString() const { |
| 154 | std::vector<std::string> Names; |
| 155 | std::string QualName; |
| 156 | const DeclContext *Ctx = getDeclContext(); |
| 157 | |
| 158 | if (Ctx->isFunctionOrMethod()) |
| 159 | return getNameAsString(); |
| 160 | |
| 161 | while (Ctx) { |
| 162 | if (Ctx->isFunctionOrMethod()) |
| 163 | // FIXME: That probably will happen, when D was member of local |
| 164 | // scope class/struct/union. How do we handle this case? |
| 165 | break; |
| 166 | |
| 167 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx)) |
| 168 | Names.push_back(ND->getNameAsString()); |
| 169 | else |
| 170 | break; |
| 171 | |
| 172 | Ctx = Ctx->getParent(); |
| 173 | } |
| 174 | |
| 175 | std::vector<std::string>::reverse_iterator |
| 176 | I = Names.rbegin(), |
| 177 | End = Names.rend(); |
| 178 | |
| 179 | for (; I!=End; ++I) |
| 180 | QualName += *I + "::"; |
| 181 | |
| 182 | QualName += getNameAsString(); |
| 183 | |
| 184 | return QualName; |
| 185 | } |
| 186 | |
| 187 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 188 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 189 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 190 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 191 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 192 | // We want to keep it, unless it nominates same namespace. |
| 193 | if (getKind() == Decl::UsingDirective) { |
| 194 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == |
| 195 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); |
| 196 | } |
| 197 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 198 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 199 | // For function declarations, we keep track of redeclarations. |
| 200 | return FD->getPreviousDeclaration() == OldD; |
| 201 | |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 202 | // For method declarations, we keep track of redeclarations. |
| 203 | if (isa<ObjCMethodDecl>(this)) |
| 204 | return false; |
| 205 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 206 | // For non-function declarations, if the declarations are of the |
| 207 | // same kind then this must be a redeclaration, or semantic analysis |
| 208 | // would not have given us the new declaration. |
| 209 | return this->getKind() == OldD->getKind(); |
| 210 | } |
| 211 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 212 | bool NamedDecl::hasLinkage() const { |
| 213 | if (const VarDecl *VD = dyn_cast<VarDecl>(this)) |
| 214 | return VD->hasExternalStorage() || VD->isFileVarDecl(); |
| 215 | |
| 216 | if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this)) |
| 217 | return true; |
| 218 | |
| 219 | return false; |
| 220 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 221 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 222 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 223 | // VarDecl Implementation |
| 224 | //===----------------------------------------------------------------------===// |
| 225 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 226 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 227 | IdentifierInfo *Id, QualType T, StorageClass S, |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 228 | SourceLocation TypeSpecStartLoc) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 229 | return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void VarDecl::Destroy(ASTContext& C) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 233 | Expr *Init = getInit(); |
| 234 | if (Init) |
| 235 | Init->Destroy(C); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 236 | this->~VarDecl(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 237 | C.Deallocate((void *)this); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | VarDecl::~VarDecl() { |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 244 | // FunctionDecl Implementation |
| 245 | //===----------------------------------------------------------------------===// |
| 246 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 247 | void FunctionDecl::Destroy(ASTContext& C) { |
Ted Kremenek | b65cf41 | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 248 | if (Body) |
| 249 | Body->Destroy(C); |
| 250 | |
| 251 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 252 | (*I)->Destroy(C); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 253 | |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 254 | C.Deallocate(ParamInfo); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 256 | Decl::Destroy(C); |
| 257 | } |
| 258 | |
| 259 | |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 260 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
| 261 | for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) { |
| 262 | if (FD->Body) { |
| 263 | Definition = FD; |
| 264 | return FD->Body; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 271 | bool FunctionDecl::isMain() const { |
| 272 | return getDeclContext()->getLookupContext()->isTranslationUnit() && |
| 273 | getIdentifier() && getIdentifier()->isStr("main"); |
| 274 | } |
| 275 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 276 | /// \brief Returns a value indicating whether this function |
| 277 | /// corresponds to a builtin function. |
| 278 | /// |
| 279 | /// The function corresponds to a built-in function if it is |
| 280 | /// declared at translation scope or within an extern "C" block and |
| 281 | /// its name matches with the name of a builtin. The returned value |
| 282 | /// will be 0 for functions that do not correspond to a builtin, a |
| 283 | /// value of type \c Builtin::ID if in the target-independent range |
| 284 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 285 | unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const { |
| 286 | if (!getIdentifier() || !getIdentifier()->getBuiltinID()) |
| 287 | return 0; |
| 288 | |
| 289 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| 290 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 291 | return BuiltinID; |
| 292 | |
| 293 | // This function has the name of a known C library |
| 294 | // function. Determine whether it actually refers to the C library |
| 295 | // function or whether it just has the same name. |
| 296 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 297 | // If this is a static function, it's not a builtin. |
| 298 | if (getStorageClass() == Static) |
| 299 | return 0; |
| 300 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 301 | // If this function is at translation-unit scope and we're not in |
| 302 | // C++, it refers to the C library function. |
| 303 | if (!Context.getLangOptions().CPlusPlus && |
| 304 | getDeclContext()->isTranslationUnit()) |
| 305 | return BuiltinID; |
| 306 | |
| 307 | // If the function is in an extern "C" linkage specification and is |
| 308 | // not marked "overloadable", it's the real function. |
| 309 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
| 310 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
| 311 | == LinkageSpecDecl::lang_c && |
| 312 | !getAttr<OverloadableAttr>()) |
| 313 | return BuiltinID; |
| 314 | |
| 315 | // Not a builtin |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | |
Ted Kremenek | 4f03fd6 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 320 | // Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams() |
| 321 | static unsigned getNumTypeParams(QualType T) { |
| 322 | const FunctionType *FT = T->getAsFunctionType(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 323 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 324 | return 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 325 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Ted Kremenek | 4f03fd6 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 328 | unsigned FunctionDecl::getNumParams() const { |
| 329 | // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar; |
| 330 | if (!ParamInfo) |
| 331 | return 0; |
| 332 | |
| 333 | return getNumTypeParams(getType()); |
| 334 | } |
| 335 | |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 336 | void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 337 | unsigned NumParams) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 338 | assert(ParamInfo == 0 && "Already has param info!"); |
Ted Kremenek | 4f03fd6 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 339 | assert(NumParams == getNumTypeParams(getType()) && |
| 340 | "Parameter count mismatch!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | |
| 342 | // Zero params -> null pointer. |
| 343 | if (NumParams) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 344 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 345 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 346 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 347 | } |
| 348 | } |
| 349 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 350 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 351 | /// needed to call this function. This may be fewer than the number of |
| 352 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 353 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 354 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 355 | unsigned NumRequiredArgs = getNumParams(); |
| 356 | while (NumRequiredArgs > 0 |
| 357 | && getParamDecl(NumRequiredArgs-1)->getDefaultArg()) |
| 358 | --NumRequiredArgs; |
| 359 | |
| 360 | return NumRequiredArgs; |
| 361 | } |
| 362 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 363 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 364 | /// function represents, if any. |
| 365 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 366 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 367 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 368 | else |
| 369 | return OO_None; |
| 370 | } |
| 371 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 372 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 373 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 374 | //===----------------------------------------------------------------------===// |
| 375 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 376 | void TagDecl::startDefinition() { |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 377 | TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType()); |
| 378 | TagT->decl.setPointer(this); |
| 379 | TagT->getAsTagType()->decl.setInt(1); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void TagDecl::completeDefinition() { |
| 383 | assert((!TypeForDecl || |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 384 | TypeForDecl->getAsTagType()->decl.getPointer() == this) && |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 385 | "Attempt to redefine a tag definition?"); |
| 386 | IsDefinition = true; |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 387 | TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType()); |
| 388 | TagT->decl.setPointer(this); |
| 389 | TagT->decl.setInt(0); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 392 | TagDecl* TagDecl::getDefinition(ASTContext& C) const { |
| 393 | QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this)); |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 394 | TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl()); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 395 | return D->isDefinition() ? D : 0; |
| 396 | } |
| 397 | |
| 398 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 399 | // RecordDecl Implementation |
| 400 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 401 | |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 402 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 403 | IdentifierInfo *Id) |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 404 | : TagDecl(DK, TK, DC, L, Id) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 405 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 406 | AnonymousStructOrUnion = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 407 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 411 | SourceLocation L, IdentifierInfo *Id, |
| 412 | RecordDecl* PrevDecl) { |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 413 | |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 414 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 415 | C.getTypeDeclType(R, PrevDecl); |
| 416 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 419 | RecordDecl::~RecordDecl() { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | void RecordDecl::Destroy(ASTContext& C) { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 423 | TagDecl::Destroy(C); |
| 424 | } |
| 425 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 426 | /// completeDefinition - Notes that the definition of this type is now |
| 427 | /// complete. |
| 428 | void RecordDecl::completeDefinition(ASTContext& C) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 429 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 430 | TagDecl::completeDefinition(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 433 | //===----------------------------------------------------------------------===// |
| 434 | // BlockDecl Implementation |
| 435 | //===----------------------------------------------------------------------===// |
| 436 | |
| 437 | BlockDecl::~BlockDecl() { |
| 438 | } |
| 439 | |
| 440 | void BlockDecl::Destroy(ASTContext& C) { |
| 441 | if (Body) |
| 442 | Body->Destroy(C); |
| 443 | |
| 444 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 445 | (*I)->Destroy(C); |
| 446 | |
| 447 | Decl::Destroy(C); |
| 448 | } |