Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Decl.cpp - Declaration AST Node Implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Argiris Kirtzidis | e7dfca1 | 2008-06-04 13:04:04 +0000 | [diff] [blame] | 10 | // This file implements the Decl subclasses. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | d85ba92 | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 9054f98 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Stmt.h" |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Douglas Gregor | 3bf3bbc | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 21 | #include "clang/AST/PrettyPrinter.h" |
Chris Lattner | c46fcdd | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 23 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 24 | #include <vector> |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
Chris Lattner | 16ded57 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 28 | void Attr::Destroy(ASTContext &C) { |
| 29 | if (Next) { |
| 30 | Next->Destroy(C); |
| 31 | Next = 0; |
| 32 | } |
| 33 | this->~Attr(); |
| 34 | C.Deallocate((void*)this); |
| 35 | } |
| 36 | |
| 37 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 39 | // Decl Allocation/Deallocation Method Implementations |
| 40 | //===----------------------------------------------------------------------===// |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 16ded57 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 42 | |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 43 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
Argiris Kirtzidis | 2a6dca1 | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 44 | return new (C) TranslationUnitDecl(C); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 47 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 48 | SourceLocation L, IdentifierInfo *Id) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 49 | return new (C) NamespaceDecl(DC, L, Id); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 52 | void NamespaceDecl::Destroy(ASTContext& C) { |
| 53 | // NamespaceDecl uses "NextDeclarator" to chain namespace declarations |
| 54 | // together. They are all top-level Decls. |
| 55 | |
Ted Kremenek | 0f43384 | 2008-05-24 15:09:56 +0000 | [diff] [blame] | 56 | this->~NamespaceDecl(); |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 57 | C.Deallocate((void *)this); |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 61 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 62 | SourceLocation L, IdentifierInfo *Id, QualType T) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 63 | return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Daniel Dunbar | caf78fb | 2009-04-14 02:08:49 +0000 | [diff] [blame] | 66 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 67 | switch (SC) { |
| 68 | case VarDecl::None: break; |
| 69 | case VarDecl::Auto: return "auto"; break; |
| 70 | case VarDecl::Extern: return "extern"; break; |
| 71 | case VarDecl::PrivateExtern: return "__private_extern__"; break; |
| 72 | case VarDecl::Register: return "register"; break; |
| 73 | case VarDecl::Static: return "static"; break; |
| 74 | } |
| 75 | |
| 76 | assert(0 && "Invalid storage class"); |
| 77 | return 0; |
| 78 | } |
| 79 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 80 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 81 | SourceLocation L, IdentifierInfo *Id, |
| 82 | QualType T, StorageClass S, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 83 | Expr *DefArg) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 84 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg); |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | QualType ParmVarDecl::getOriginalType() const { |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 88 | if (const OriginalParmVarDecl *PVD = |
| 89 | dyn_cast<OriginalParmVarDecl>(this)) |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 90 | return PVD->OriginalType; |
| 91 | return getType(); |
Chris Lattner | 48d225c | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Douglas Gregor | 4833ff0 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 94 | void VarDecl::setInit(ASTContext &C, Expr *I) { |
| 95 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| 96 | Eval->~EvaluatedStmt(); |
| 97 | C.Deallocate(Eval); |
| 98 | } |
| 99 | |
| 100 | Init = I; |
| 101 | } |
| 102 | |
Douglas Gregor | e6b5d1d | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 103 | bool VarDecl::isExternC(ASTContext &Context) const { |
| 104 | if (!Context.getLangOptions().CPlusPlus) |
| 105 | return (getDeclContext()->isTranslationUnit() && |
| 106 | getStorageClass() != Static) || |
| 107 | (getDeclContext()->isFunctionOrMethod() && hasExternalStorage()); |
| 108 | |
| 109 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
| 110 | DC = DC->getParent()) { |
| 111 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 112 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
| 113 | return getStorageClass() != Static; |
| 114 | |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | if (DC->isFunctionOrMethod()) |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 125 | OriginalParmVarDecl *OriginalParmVarDecl::Create( |
Fariborz Jahanian | 160e881 | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 126 | ASTContext &C, DeclContext *DC, |
| 127 | SourceLocation L, IdentifierInfo *Id, |
| 128 | QualType T, QualType OT, StorageClass S, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 129 | Expr *DefArg) { |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 130 | return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg); |
Fariborz Jahanian | 160e881 | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 133 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 134 | SourceLocation L, |
Douglas Gregor | 6704b31 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 135 | DeclarationName N, QualType T, |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 136 | StorageClass S, bool isInline, |
Anders Carlsson | d9d6d50 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 137 | bool hasWrittenPrototype, |
Steve Naroff | 71cd776 | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 138 | SourceLocation TypeSpecStartLoc) { |
Douglas Gregor | 1f88aa7 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 139 | FunctionDecl *New |
| 140 | = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline, |
| 141 | TypeSpecStartLoc); |
Anders Carlsson | d9d6d50 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 142 | New->HasWrittenPrototype = hasWrittenPrototype; |
Douglas Gregor | 1f88aa7 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 143 | return New; |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 146 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 147 | return new (C) BlockDecl(DC, L); |
Steve Naroff | 9ac456d | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 150 | FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 151 | IdentifierInfo *Id, QualType T, Expr *BW, |
Steve Naroff | b79574e | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 152 | bool Mutable, SourceLocation TSSL) { |
| 153 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable, TSSL); |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Douglas Gregor | c7f0161 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 156 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 157 | if (!isImplicit() || getDeclName()) |
| 158 | return false; |
| 159 | |
Ted Kremenek | d9b39bf | 2009-07-17 17:50:17 +0000 | [diff] [blame^] | 160 | if (const RecordType *Record = getType()->getAsRecordType()) |
Douglas Gregor | c7f0161 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 161 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 162 | |
| 163 | return false; |
| 164 | } |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 165 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 166 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 167 | SourceLocation L, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 168 | IdentifierInfo *Id, QualType T, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 169 | Expr *E, const llvm::APSInt &V) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 170 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 173 | void EnumConstantDecl::Destroy(ASTContext& C) { |
| 174 | if (Init) Init->Destroy(C); |
| 175 | Decl::Destroy(C); |
| 176 | } |
| 177 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 178 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 179 | SourceLocation L, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 180 | IdentifierInfo *Id, QualType T) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 181 | return new (C) TypedefDecl(DC, L, Id, T); |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 184 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 185 | IdentifierInfo *Id, |
Douglas Gregor | ae64489 | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 186 | EnumDecl *PrevDecl) { |
Steve Naroff | 207b9ec | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 187 | EnumDecl *Enum = new (C) EnumDecl(DC, L, Id); |
Douglas Gregor | ae64489 | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 188 | C.getTypeDeclType(Enum, PrevDecl); |
| 189 | return Enum; |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Ted Kremenek | 25d8be1 | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 192 | void EnumDecl::Destroy(ASTContext& C) { |
Ted Kremenek | 25d8be1 | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 193 | Decl::Destroy(C); |
| 194 | } |
| 195 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 196 | void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) { |
| 197 | assert(!isDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 198 | IntegerType = NewType; |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 199 | TagDecl::completeDefinition(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 202 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 203 | SourceLocation L, |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 204 | StringLiteral *Str) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 205 | return new (C) FileScopeAsmDecl(DC, L, Str); |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 208 | //===----------------------------------------------------------------------===// |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 209 | // NamedDecl Implementation |
Argiris Kirtzidis | 881964b | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 210 | //===----------------------------------------------------------------------===// |
| 211 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 212 | std::string NamedDecl::getQualifiedNameAsString() const { |
| 213 | std::vector<std::string> Names; |
| 214 | std::string QualName; |
| 215 | const DeclContext *Ctx = getDeclContext(); |
| 216 | |
| 217 | if (Ctx->isFunctionOrMethod()) |
| 218 | return getNameAsString(); |
| 219 | |
| 220 | while (Ctx) { |
| 221 | if (Ctx->isFunctionOrMethod()) |
| 222 | // FIXME: That probably will happen, when D was member of local |
| 223 | // scope class/struct/union. How do we handle this case? |
| 224 | break; |
| 225 | |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 226 | if (const ClassTemplateSpecializationDecl *Spec |
| 227 | = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) { |
| 228 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Chris Lattner | 7099c78 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 229 | PrintingPolicy Policy(getASTContext().getLangOptions()); |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 230 | std::string TemplateArgsStr |
| 231 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 232 | TemplateArgs.getFlatArgumentList(), |
Douglas Gregor | 3bf3bbc | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 233 | TemplateArgs.flat_size(), |
| 234 | Policy); |
Douglas Gregor | b12249d | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 235 | Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr); |
| 236 | } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx)) |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 237 | Names.push_back(ND->getNameAsString()); |
| 238 | else |
| 239 | break; |
| 240 | |
| 241 | Ctx = Ctx->getParent(); |
| 242 | } |
| 243 | |
| 244 | std::vector<std::string>::reverse_iterator |
| 245 | I = Names.rbegin(), |
| 246 | End = Names.rend(); |
| 247 | |
| 248 | for (; I!=End; ++I) |
| 249 | QualName += *I + "::"; |
| 250 | |
| 251 | QualName += getNameAsString(); |
| 252 | |
| 253 | return QualName; |
| 254 | } |
| 255 | |
| 256 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 257 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6e71edc | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 258 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 259 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 260 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 261 | // We want to keep it, unless it nominates same namespace. |
| 262 | if (getKind() == Decl::UsingDirective) { |
| 263 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == |
| 264 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); |
| 265 | } |
| 266 | |
Douglas Gregor | 6e71edc | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 267 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 268 | // For function declarations, we keep track of redeclarations. |
| 269 | return FD->getPreviousDeclaration() == OldD; |
| 270 | |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 271 | // For function templates, the underlying function declarations are linked. |
| 272 | if (const FunctionTemplateDecl *FunctionTemplate |
| 273 | = dyn_cast<FunctionTemplateDecl>(this)) |
| 274 | if (const FunctionTemplateDecl *OldFunctionTemplate |
| 275 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 276 | return FunctionTemplate->getTemplatedDecl() |
| 277 | ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); |
| 278 | |
Steve Naroff | d85ba92 | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 279 | // For method declarations, we keep track of redeclarations. |
| 280 | if (isa<ObjCMethodDecl>(this)) |
| 281 | return false; |
| 282 | |
Douglas Gregor | 6e71edc | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 283 | // For non-function declarations, if the declarations are of the |
| 284 | // same kind then this must be a redeclaration, or semantic analysis |
| 285 | // would not have given us the new declaration. |
| 286 | return this->getKind() == OldD->getKind(); |
| 287 | } |
| 288 | |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 289 | bool NamedDecl::hasLinkage() const { |
| 290 | if (const VarDecl *VD = dyn_cast<VarDecl>(this)) |
| 291 | return VD->hasExternalStorage() || VD->isFileVarDecl(); |
| 292 | |
| 293 | if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this)) |
| 294 | return true; |
| 295 | |
| 296 | return false; |
| 297 | } |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 298 | |
Anders Carlsson | df5ab84 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 299 | NamedDecl *NamedDecl::getUnderlyingDecl() { |
| 300 | NamedDecl *ND = this; |
| 301 | while (true) { |
| 302 | if (UsingDecl *UD = dyn_cast<UsingDecl>(ND)) |
| 303 | ND = UD->getTargetDecl(); |
| 304 | else if (ObjCCompatibleAliasDecl *AD |
| 305 | = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 306 | return AD->getClassInterface(); |
| 307 | else |
| 308 | return ND; |
| 309 | } |
| 310 | } |
| 311 | |
Argiris Kirtzidis | 881964b | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 312 | //===----------------------------------------------------------------------===// |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 313 | // VarDecl Implementation |
| 314 | //===----------------------------------------------------------------------===// |
| 315 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 316 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 317 | IdentifierInfo *Id, QualType T, StorageClass S, |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 318 | SourceLocation TypeSpecStartLoc) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 319 | return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc); |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void VarDecl::Destroy(ASTContext& C) { |
Sebastian Redl | 2ee5561 | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 323 | Expr *Init = getInit(); |
Douglas Gregor | 4833ff0 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 324 | if (Init) { |
Sebastian Redl | 2ee5561 | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 325 | Init->Destroy(C); |
Douglas Gregor | 4833ff0 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 326 | if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) { |
| 327 | Eval->~EvaluatedStmt(); |
| 328 | C.Deallocate(Eval); |
| 329 | } |
| 330 | } |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 331 | this->~VarDecl(); |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 332 | C.Deallocate((void *)this); |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | VarDecl::~VarDecl() { |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Argiris Kirtzidis | 545473e | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 338 | SourceRange VarDecl::getSourceRange() const { |
| 339 | if (getInit()) |
| 340 | return SourceRange(getLocation(), getInit()->getLocEnd()); |
| 341 | return SourceRange(getLocation(), getLocation()); |
| 342 | } |
| 343 | |
Douglas Gregor | 2f728b2 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 344 | bool VarDecl::isTentativeDefinition(ASTContext &Context) const { |
| 345 | if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus) |
| 346 | return false; |
| 347 | |
Douglas Gregor | 9cdb4a1 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 348 | const VarDecl *Def = 0; |
| 349 | return (!getDefinition(Def) && |
Douglas Gregor | 2f728b2 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 350 | (getStorageClass() == None || getStorageClass() == Static)); |
| 351 | } |
| 352 | |
Ted Kremenek | 51787c7 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 353 | const Expr *VarDecl::getDefinition(const VarDecl *&Def) const { |
Argiris Kirtzidis | 1d2f3a3 | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 354 | redecl_iterator I = redecls_begin(), E = redecls_end(); |
| 355 | while (I != E && !I->getInit()) |
| 356 | ++I; |
Douglas Gregor | 2f728b2 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 357 | |
Argiris Kirtzidis | 1d2f3a3 | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 358 | if (I != E) { |
| 359 | Def = *I; |
| 360 | return I->getInit(); |
| 361 | } |
| 362 | return 0; |
Douglas Gregor | 2f728b2 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Argiris Kirtzidis | 349185b | 2009-07-14 03:20:08 +0000 | [diff] [blame] | 365 | void VarDecl::setPreviousDeclaration(VarDecl *PrevDecl) { |
| 366 | if (PrevDecl) { |
| 367 | // Point to previous. |
| 368 | PreviousDeclaration.setPointer(PrevDecl); |
| 369 | PreviousDeclaration.setInt(0); |
| 370 | |
| 371 | // First one will point to this one as latest. |
| 372 | VarDecl *First = PrevDecl->getFirstDeclaration(); |
| 373 | assert(First->PreviousDeclaration.getInt() == 1 && "Expected first"); |
| 374 | First->PreviousDeclaration.setPointer(this); |
| 375 | } else { |
| 376 | // This is first. |
| 377 | PreviousDeclaration.setPointer(this); |
| 378 | PreviousDeclaration.setInt(1); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | VarDecl *VarDecl::getFirstDeclaration() { |
| 383 | VarDecl *First = this; |
Argiris Kirtzidis | 9f000a3 | 2009-07-14 03:19:57 +0000 | [diff] [blame] | 384 | while (First->getPreviousDeclaration()) |
| 385 | First = First->getPreviousDeclaration(); |
Argiris Kirtzidis | 5ea54f6 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 386 | |
Argiris Kirtzidis | 9f000a3 | 2009-07-14 03:19:57 +0000 | [diff] [blame] | 387 | return First; |
| 388 | } |
| 389 | |
| 390 | Decl *VarDecl::getPrimaryDecl() const { |
| 391 | return const_cast<VarDecl *>(getFirstDeclaration()); |
Argiris Kirtzidis | 5ea54f6 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 394 | //===----------------------------------------------------------------------===// |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 395 | // FunctionDecl Implementation |
| 396 | //===----------------------------------------------------------------------===// |
| 397 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 398 | void FunctionDecl::Destroy(ASTContext& C) { |
Douglas Gregor | 3b9a7c8 | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 399 | if (Body && Body.isOffset()) |
| 400 | Body.get(C.getExternalSource())->Destroy(C); |
Ted Kremenek | 345b93d | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 401 | |
| 402 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 403 | (*I)->Destroy(C); |
Nuno Lopes | cb8cc5b | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 404 | |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 405 | C.Deallocate(ParamInfo); |
Nuno Lopes | cb8cc5b | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 406 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 407 | Decl::Destroy(C); |
| 408 | } |
| 409 | |
| 410 | |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 411 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Argiris Kirtzidis | 1d2f3a3 | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 412 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 413 | if (I->Body) { |
| 414 | Definition = *I; |
| 415 | return I->Body.get(getASTContext().getExternalSource()); |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 422 | Stmt *FunctionDecl::getBodyIfAvailable() const { |
Argiris Kirtzidis | 1d2f3a3 | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 423 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 424 | if (I->Body && !I->Body.isOffset()) { |
| 425 | return I->Body.get(0); |
Douglas Gregor | 3b9a7c8 | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 426 | } |
Douglas Gregor | e3241e9 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
Argiris Kirtzidis | 545473e | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 432 | void FunctionDecl::setBody(Stmt *B) { |
| 433 | Body = B; |
Argiris Kirtzidis | 25822a0 | 2009-06-22 17:13:31 +0000 | [diff] [blame] | 434 | if (B) |
Argiris Kirtzidis | 545473e | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 435 | EndRangeLoc = B->getLocEnd(); |
| 436 | } |
| 437 | |
Douglas Gregor | af68202 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 438 | bool FunctionDecl::isMain() const { |
| 439 | return getDeclContext()->getLookupContext()->isTranslationUnit() && |
| 440 | getIdentifier() && getIdentifier()->isStr("main"); |
| 441 | } |
| 442 | |
Douglas Gregor | e6b5d1d | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 443 | bool FunctionDecl::isExternC(ASTContext &Context) const { |
| 444 | // In C, any non-static, non-overloadable function has external |
| 445 | // linkage. |
| 446 | if (!Context.getLangOptions().CPlusPlus) |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 447 | return getStorageClass() != Static && !getAttr<OverloadableAttr>(); |
Douglas Gregor | e6b5d1d | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 448 | |
| 449 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
| 450 | DC = DC->getParent()) { |
| 451 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 452 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
Douglas Gregor | 98da6ae | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 453 | return getStorageClass() != Static && |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 454 | !getAttr<OverloadableAttr>(); |
Douglas Gregor | e6b5d1d | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 455 | |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | return false; |
| 461 | } |
| 462 | |
Douglas Gregor | cd7ac6f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 463 | bool FunctionDecl::isGlobal() const { |
| 464 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 465 | return Method->isStatic(); |
| 466 | |
| 467 | if (getStorageClass() == Static) |
| 468 | return false; |
| 469 | |
| 470 | for (const DeclContext *DC = getDeclContext(); |
| 471 | DC->isNamespace(); |
| 472 | DC = DC->getParent()) { |
| 473 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 474 | if (!Namespace->getDeclName()) |
| 475 | return false; |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return true; |
| 481 | } |
| 482 | |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 483 | /// \brief Returns a value indicating whether this function |
| 484 | /// corresponds to a builtin function. |
| 485 | /// |
| 486 | /// The function corresponds to a built-in function if it is |
| 487 | /// declared at translation scope or within an extern "C" block and |
| 488 | /// its name matches with the name of a builtin. The returned value |
| 489 | /// will be 0 for functions that do not correspond to a builtin, a |
| 490 | /// value of type \c Builtin::ID if in the target-independent range |
| 491 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 492 | unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const { |
| 493 | if (!getIdentifier() || !getIdentifier()->getBuiltinID()) |
| 494 | return 0; |
| 495 | |
| 496 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| 497 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 498 | return BuiltinID; |
| 499 | |
| 500 | // This function has the name of a known C library |
| 501 | // function. Determine whether it actually refers to the C library |
| 502 | // function or whether it just has the same name. |
| 503 | |
Douglas Gregor | 4d6b102 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 504 | // If this is a static function, it's not a builtin. |
| 505 | if (getStorageClass() == Static) |
| 506 | return 0; |
| 507 | |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 508 | // If this function is at translation-unit scope and we're not in |
| 509 | // C++, it refers to the C library function. |
| 510 | if (!Context.getLangOptions().CPlusPlus && |
| 511 | getDeclContext()->isTranslationUnit()) |
| 512 | return BuiltinID; |
| 513 | |
| 514 | // If the function is in an extern "C" linkage specification and is |
| 515 | // not marked "overloadable", it's the real function. |
| 516 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
| 517 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
| 518 | == LinkageSpecDecl::lang_c && |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 519 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 520 | return BuiltinID; |
| 521 | |
| 522 | // Not a builtin |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | |
Chris Lattner | d211202 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 527 | /// getNumParams - Return the number of parameters this function must have |
Chris Lattner | c13d473 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 528 | /// based on its FunctionType. This is the length of the PararmInfo array |
Chris Lattner | d211202 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 529 | /// after it has been created. |
| 530 | unsigned FunctionDecl::getNumParams() const { |
Chris Lattner | 9d957cb | 2009-04-25 05:56:45 +0000 | [diff] [blame] | 531 | const FunctionType *FT = getType()->getAsFunctionType(); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 532 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 533 | return 0; |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 534 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Chris Lattner | 9d957cb | 2009-04-25 05:56:45 +0000 | [diff] [blame] | 535 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Ted Kremenek | 8494c96 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 538 | void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 539 | unsigned NumParams) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 540 | assert(ParamInfo == 0 && "Already has param info!"); |
Chris Lattner | c13d473 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 541 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 542 | |
| 543 | // Zero params -> null pointer. |
| 544 | if (NumParams) { |
Steve Naroff | 207b9ec | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 545 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | 8494c96 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 546 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 547 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
Argiris Kirtzidis | 545473e | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 548 | |
Argiris Kirtzidis | ae78f1f | 2009-06-23 00:42:00 +0000 | [diff] [blame] | 549 | // Update source range. The check below allows us to set EndRangeLoc before |
| 550 | // setting the parameters. |
Argiris Kirtzidis | 0dba8f2 | 2009-06-23 00:42:15 +0000 | [diff] [blame] | 551 | if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) |
Argiris Kirtzidis | 545473e | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 552 | EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 556 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 557 | /// needed to call this function. This may be fewer than the number of |
| 558 | /// function parameters, if some of the parameters have default |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 559 | /// arguments (in C++). |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 560 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 561 | unsigned NumRequiredArgs = getNumParams(); |
| 562 | while (NumRequiredArgs > 0 |
Anders Carlsson | d2e57d9 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 563 | && getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 564 | --NumRequiredArgs; |
| 565 | |
| 566 | return NumRequiredArgs; |
| 567 | } |
| 568 | |
Douglas Gregor | 98da6ae | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 569 | bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const { |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 570 | if (!isInline() || !hasAttr<GNUInlineAttr>()) |
Douglas Gregor | 67e1144 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 571 | return false; |
| 572 | |
Argiris Kirtzidis | 1d2f3a3 | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 573 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 574 | if (I->isInline() && !I->hasAttr<GNUInlineAttr>()) |
Douglas Gregor | 67e1144 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 575 | return false; |
Douglas Gregor | 67e1144 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 576 | |
| 577 | return true; |
| 578 | } |
| 579 | |
Douglas Gregor | 98da6ae | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 580 | bool FunctionDecl::isExternGNUInline(ASTContext &Context) const { |
| 581 | if (!hasActiveGNUInlineAttribute(Context)) |
Douglas Gregor | 67e1144 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 582 | return false; |
| 583 | |
Argiris Kirtzidis | 1d2f3a3 | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 584 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 585 | if (I->getStorageClass() == Extern && I->hasAttr<GNUInlineAttr>()) |
Douglas Gregor | 67e1144 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 586 | return true; |
| 587 | |
| 588 | return false; |
| 589 | } |
| 590 | |
Douglas Gregor | 27b6d2b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 591 | void |
| 592 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
Argiris Kirtzidis | 349185b | 2009-07-14 03:20:08 +0000 | [diff] [blame] | 593 | if (PrevDecl) { |
| 594 | // Point to previous. |
| 595 | PreviousDeclaration.setPointer(PrevDecl); |
| 596 | PreviousDeclaration.setInt(0); |
| 597 | |
| 598 | // First one will point to this one as latest. |
| 599 | FunctionDecl *First = PrevDecl->getFirstDeclaration(); |
| 600 | assert(First->PreviousDeclaration.getInt() == 1 && "Expected first"); |
| 601 | First->PreviousDeclaration.setPointer(this); |
| 602 | } else { |
| 603 | // This is first. |
| 604 | PreviousDeclaration.setPointer(this); |
| 605 | PreviousDeclaration.setInt(1); |
| 606 | } |
Douglas Gregor | 27b6d2b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 607 | |
| 608 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 609 | FunctionTemplateDecl *PrevFunTmpl |
| 610 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 611 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 612 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 613 | } |
| 614 | } |
| 615 | |
Argiris Kirtzidis | 349185b | 2009-07-14 03:20:08 +0000 | [diff] [blame] | 616 | FunctionDecl *FunctionDecl::getFirstDeclaration() { |
| 617 | FunctionDecl *First = this; |
Argiris Kirtzidis | 9f000a3 | 2009-07-14 03:19:57 +0000 | [diff] [blame] | 618 | while (First->getPreviousDeclaration()) |
| 619 | First = First->getPreviousDeclaration(); |
Argiris Kirtzidis | 5ea54f6 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 620 | |
Argiris Kirtzidis | 9f000a3 | 2009-07-14 03:19:57 +0000 | [diff] [blame] | 621 | return First; |
| 622 | } |
| 623 | |
| 624 | Decl *FunctionDecl::getPrimaryDecl() const { |
| 625 | return const_cast<FunctionDecl *>(getFirstDeclaration()); |
Argiris Kirtzidis | 5ea54f6 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 628 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 629 | /// function represents, if any. |
| 630 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | 96a32dd | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 631 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 632 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 633 | else |
| 634 | return OO_None; |
| 635 | } |
| 636 | |
Douglas Gregor | 2ed1a54 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 637 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
| 638 | if (FunctionTemplateSpecializationInfo *Info |
| 639 | = TemplateOrSpecialization |
| 640 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 6967806 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 641 | return Info->Template.getPointer(); |
Douglas Gregor | 2ed1a54 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 642 | } |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | const TemplateArgumentList * |
| 647 | FunctionDecl::getTemplateSpecializationArgs() const { |
| 648 | if (FunctionTemplateSpecializationInfo *Info |
| 649 | = TemplateOrSpecialization |
| 650 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 651 | return Info->TemplateArguments; |
| 652 | } |
| 653 | return 0; |
| 654 | } |
| 655 | |
Douglas Gregor | 6f5e054 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 656 | void |
| 657 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context, |
| 658 | FunctionTemplateDecl *Template, |
Douglas Gregor | 27b6d2b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 659 | const TemplateArgumentList *TemplateArgs, |
| 660 | void *InsertPos) { |
Douglas Gregor | 2ed1a54 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 661 | FunctionTemplateSpecializationInfo *Info |
| 662 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 6f5e054 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 663 | if (!Info) |
Douglas Gregor | 2ed1a54 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 664 | Info = new (Context) FunctionTemplateSpecializationInfo; |
Douglas Gregor | 6f5e054 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 665 | |
Douglas Gregor | 27b6d2b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 666 | Info->Function = this; |
Douglas Gregor | 6967806 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 667 | Info->Template.setPointer(Template); |
| 668 | Info->Template.setInt(0); // Implicit instantiation, unless told otherwise |
Douglas Gregor | 6f5e054 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 669 | Info->TemplateArguments = TemplateArgs; |
| 670 | TemplateOrSpecialization = Info; |
Douglas Gregor | 27b6d2b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 671 | |
| 672 | // Insert this function template specialization into the set of known |
| 673 | // function template specialiations. |
| 674 | Template->getSpecializations().InsertNode(Info, InsertPos); |
Douglas Gregor | 6f5e054 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Douglas Gregor | 6967806 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 677 | bool FunctionDecl::isExplicitSpecialization() const { |
| 678 | // FIXME: check this property for explicit specializations of member |
| 679 | // functions of class templates. |
| 680 | FunctionTemplateSpecializationInfo *Info |
| 681 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
| 682 | if (!Info) |
| 683 | return false; |
| 684 | |
| 685 | return Info->isExplicitSpecialization(); |
| 686 | } |
| 687 | |
| 688 | void FunctionDecl::setExplicitSpecialization(bool ES) { |
| 689 | // FIXME: set this property for explicit specializations of member functions |
| 690 | // of class templates. |
| 691 | FunctionTemplateSpecializationInfo *Info |
| 692 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
| 693 | if (Info) |
| 694 | Info->setExplicitSpecialization(ES); |
| 695 | } |
| 696 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 697 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 698 | // TagDecl Implementation |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 699 | //===----------------------------------------------------------------------===// |
| 700 | |
Argiris Kirtzidis | b929dcc | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 701 | SourceRange TagDecl::getSourceRange() const { |
| 702 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
| 703 | return SourceRange(getLocation(), E); |
| 704 | } |
| 705 | |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 706 | void TagDecl::startDefinition() { |
Ted Kremenek | d9b39bf | 2009-07-17 17:50:17 +0000 | [diff] [blame^] | 707 | TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType()); |
Douglas Gregor | 9c7825b | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 708 | TagT->decl.setPointer(this); |
Ted Kremenek | d9b39bf | 2009-07-17 17:50:17 +0000 | [diff] [blame^] | 709 | TagT->getAsTagType()->decl.setInt(1); |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | void TagDecl::completeDefinition() { |
| 713 | assert((!TypeForDecl || |
Ted Kremenek | d9b39bf | 2009-07-17 17:50:17 +0000 | [diff] [blame^] | 714 | TypeForDecl->getAsTagType()->decl.getPointer() == this) && |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 715 | "Attempt to redefine a tag definition?"); |
| 716 | IsDefinition = true; |
Ted Kremenek | d9b39bf | 2009-07-17 17:50:17 +0000 | [diff] [blame^] | 717 | TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType()); |
Douglas Gregor | 9c7825b | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 718 | TagT->decl.setPointer(this); |
| 719 | TagT->decl.setInt(0); |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 722 | TagDecl* TagDecl::getDefinition(ASTContext& C) const { |
| 723 | QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this)); |
Ted Kremenek | d9b39bf | 2009-07-17 17:50:17 +0000 | [diff] [blame^] | 724 | TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl()); |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 725 | return D->isDefinition() ? D : 0; |
| 726 | } |
| 727 | |
| 728 | //===----------------------------------------------------------------------===// |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 729 | // RecordDecl Implementation |
| 730 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 731 | |
Argiris Kirtzidis | 2538a8c | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 732 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 733 | IdentifierInfo *Id) |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 734 | : TagDecl(DK, TK, DC, L, Id) { |
Ted Kremenek | 6f0a241 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 735 | HasFlexibleArrayMember = false; |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 736 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 614e8f0 | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 737 | HasObjectMember = false; |
Ted Kremenek | 6f0a241 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 738 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6f0a241 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 742 | SourceLocation L, IdentifierInfo *Id, |
| 743 | RecordDecl* PrevDecl) { |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 744 | |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 745 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id); |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 746 | C.getTypeDeclType(R, PrevDecl); |
| 747 | return R; |
Ted Kremenek | 6f0a241 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Argiris Kirtzidis | d64c111 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 750 | RecordDecl::~RecordDecl() { |
Argiris Kirtzidis | d64c111 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | void RecordDecl::Destroy(ASTContext& C) { |
Argiris Kirtzidis | d64c111 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 754 | TagDecl::Destroy(C); |
| 755 | } |
| 756 | |
Douglas Gregor | 43bfaaf | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 757 | bool RecordDecl::isInjectedClassName() const { |
| 758 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
| 759 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 760 | } |
| 761 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 762 | /// completeDefinition - Notes that the definition of this type is now |
| 763 | /// complete. |
| 764 | void RecordDecl::completeDefinition(ASTContext& C) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 765 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 766 | TagDecl::completeDefinition(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Steve Naroff | 9ac456d | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 769 | //===----------------------------------------------------------------------===// |
| 770 | // BlockDecl Implementation |
| 771 | //===----------------------------------------------------------------------===// |
| 772 | |
| 773 | BlockDecl::~BlockDecl() { |
| 774 | } |
| 775 | |
| 776 | void BlockDecl::Destroy(ASTContext& C) { |
| 777 | if (Body) |
| 778 | Body->Destroy(C); |
| 779 | |
| 780 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 781 | (*I)->Destroy(C); |
Ted Kremenek | 4a71fb1 | 2009-03-13 23:17:24 +0000 | [diff] [blame] | 782 | |
| 783 | C.Deallocate(ParamInfo); |
Steve Naroff | 9ac456d | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 784 | Decl::Destroy(C); |
| 785 | } |
Steve Naroff | 494cb0f | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 786 | |
| 787 | void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 788 | unsigned NParms) { |
| 789 | assert(ParamInfo == 0 && "Already has param info!"); |
| 790 | |
| 791 | // Zero params -> null pointer. |
| 792 | if (NParms) { |
| 793 | NumParams = NParms; |
| 794 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
| 795 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
| 796 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | unsigned BlockDecl::getNumParams() const { |
| 801 | return NumParams; |
| 802 | } |