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" |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Stmt.h" |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 21 | #include "clang/AST/PrettyPrinter.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 23 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 24 | #include <vector> |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 25 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
Chris Lattner | 0b2b6e1 | 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 | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 39 | // Decl Allocation/Deallocation Method Implementations |
| 40 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 42 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 43 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 44 | return new (C) TranslationUnitDecl(C); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 47 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 48 | SourceLocation L, IdentifierInfo *Id) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 49 | return new (C) NamespaceDecl(DC, L, Id); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Ted Kremenek | d1ac17a | 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 | ebf27b1 | 2008-05-24 15:09:56 +0000 | [diff] [blame] | 56 | this->~NamespaceDecl(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 57 | C.Deallocate((void *)this); |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 61 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 62 | SourceLocation L, IdentifierInfo *Id, QualType T) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 63 | return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Daniel Dunbar | b286a78 | 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 | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 80 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 81 | SourceLocation L, IdentifierInfo *Id, |
| 82 | QualType T, StorageClass S, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 83 | Expr *DefArg) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 84 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | QualType ParmVarDecl::getOriginalType() const { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 88 | if (const OriginalParmVarDecl *PVD = |
| 89 | dyn_cast<OriginalParmVarDecl>(this)) |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 90 | return PVD->OriginalType; |
| 91 | return getType(); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Douglas Gregor | 78d1583 | 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 | 6393519 | 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 | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 125 | OriginalParmVarDecl *OriginalParmVarDecl::Create( |
Fariborz Jahanian | 73da9e4 | 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 | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 129 | Expr *DefArg) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 130 | return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg); |
Fariborz Jahanian | 73da9e4 | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 133 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 134 | SourceLocation L, |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 135 | DeclarationName N, QualType T, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 136 | StorageClass S, bool isInline, |
Anders Carlsson | a75e853 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 137 | bool hasWrittenPrototype, |
Steve Naroff | 0eb07bf | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 138 | SourceLocation TypeSpecStartLoc) { |
Douglas Gregor | 2224f84 | 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 | a75e853 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 142 | New->HasWrittenPrototype = hasWrittenPrototype; |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 143 | return New; |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 146 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 147 | return new (C) BlockDecl(DC, L); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Douglas Gregor | 44b4321 | 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, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 152 | bool Mutable) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 153 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 156 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 157 | if (!isImplicit() || getDeclName()) |
| 158 | return false; |
| 159 | |
| 160 | if (const RecordType *Record = getType()->getAsRecordType()) |
| 161 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 162 | |
| 163 | return false; |
| 164 | } |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 166 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 167 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 168 | IdentifierInfo *Id, QualType T, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 169 | Expr *E, const llvm::APSInt &V) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 170 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ted Kremenek | d1ac17a | 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 | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 178 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 179 | SourceLocation L, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 180 | IdentifierInfo *Id, QualType T) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 181 | return new (C) TypedefDecl(DC, L, Id, T); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 184 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 185 | IdentifierInfo *Id, |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 186 | EnumDecl *PrevDecl) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 187 | EnumDecl *Enum = new (C) EnumDecl(DC, L, Id); |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 188 | C.getTypeDeclType(Enum, PrevDecl); |
| 189 | return Enum; |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Ted Kremenek | df91eca | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 192 | void EnumDecl::Destroy(ASTContext& C) { |
Ted Kremenek | df91eca | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 193 | Decl::Destroy(C); |
| 194 | } |
| 195 | |
Douglas Gregor | 44b4321 | 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 | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 198 | IntegerType = NewType; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 199 | TagDecl::completeDefinition(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 202 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 203 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 204 | StringLiteral *Str) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 205 | return new (C) FileScopeAsmDecl(DC, L, Str); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 208 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 209 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 210 | //===----------------------------------------------------------------------===// |
| 211 | |
Douglas Gregor | 47b9a1c | 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 | f3e7ce4 | 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 | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 229 | PrintingPolicy Policy(getASTContext().getLangOptions()); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 230 | std::string TemplateArgsStr |
| 231 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 232 | TemplateArgs.getFlatArgumentList(), |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 233 | TemplateArgs.flat_size(), |
| 234 | Policy); |
Douglas Gregor | f3e7ce4 | 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 | 47b9a1c | 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 | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 257 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 258 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 259 | |
Douglas Gregor | 2a3009a | 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 | 6ed40e3 | 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 | e53060f | 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 | 0de21fd | 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 | 6ed40e3 | 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 | d6f7e9d | 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 | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 298 | |
Anders Carlsson | e136e0e | 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 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 312 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 313 | // VarDecl Implementation |
| 314 | //===----------------------------------------------------------------------===// |
| 315 | |
Douglas Gregor | 4afa39d | 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 | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 318 | SourceLocation TypeSpecStartLoc) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 319 | return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void VarDecl::Destroy(ASTContext& C) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 323 | Expr *Init = getInit(); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 324 | if (Init) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 325 | Init->Destroy(C); |
Douglas Gregor | 78d1583 | 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 | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 331 | this->~VarDecl(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 332 | C.Deallocate((void *)this); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | VarDecl::~VarDecl() { |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Argyrios Kyrtzidis | 55d608c | 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 | 275a369 | 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 | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 348 | const VarDecl *Def = 0; |
| 349 | return (!getDefinition(Def) && |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 350 | (getStorageClass() == None || getStorageClass() == Static)); |
| 351 | } |
| 352 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 353 | const Expr *VarDecl::getDefinition(const VarDecl *&Def) const { |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 354 | Def = this; |
| 355 | while (Def && !Def->getInit()) |
| 356 | Def = Def->getPreviousDeclaration(); |
| 357 | |
| 358 | return Def? Def->getInit() : 0; |
| 359 | } |
| 360 | |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 361 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 362 | // FunctionDecl Implementation |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 365 | void FunctionDecl::Destroy(ASTContext& C) { |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 366 | if (Body && Body.isOffset()) |
| 367 | Body.get(C.getExternalSource())->Destroy(C); |
Ted Kremenek | b65cf41 | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 368 | |
| 369 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 370 | (*I)->Destroy(C); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 371 | |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 372 | C.Deallocate(ParamInfo); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 373 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 374 | Decl::Destroy(C); |
| 375 | } |
| 376 | |
| 377 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 378 | Stmt *FunctionDecl::getBody(ASTContext &Context, |
| 379 | const FunctionDecl *&Definition) const { |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 380 | for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) { |
| 381 | if (FD->Body) { |
| 382 | Definition = FD; |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 383 | return FD->Body.get(Context.getExternalSource()); |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
| 387 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 390 | Stmt *FunctionDecl::getBodyIfAvailable() const { |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 391 | for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) { |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 392 | if (FD->Body && !FD->Body.isOffset()) { |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 393 | return FD->Body.get(0); |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 394 | } |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 400 | void FunctionDecl::setBody(Stmt *B) { |
| 401 | Body = B; |
Argyrios Kyrtzidis | 1a5364e | 2009-06-22 17:13:31 +0000 | [diff] [blame] | 402 | if (B) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 403 | EndRangeLoc = B->getLocEnd(); |
| 404 | } |
| 405 | |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 406 | bool FunctionDecl::isMain() const { |
| 407 | return getDeclContext()->getLookupContext()->isTranslationUnit() && |
| 408 | getIdentifier() && getIdentifier()->isStr("main"); |
| 409 | } |
| 410 | |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 411 | bool FunctionDecl::isExternC(ASTContext &Context) const { |
| 412 | // In C, any non-static, non-overloadable function has external |
| 413 | // linkage. |
| 414 | if (!Context.getLangOptions().CPlusPlus) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 415 | return getStorageClass() != Static && !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 416 | |
| 417 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
| 418 | DC = DC->getParent()) { |
| 419 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 420 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 421 | return getStorageClass() != Static && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 422 | !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 423 | |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return false; |
| 429 | } |
| 430 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 431 | bool FunctionDecl::isGlobal() const { |
| 432 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 433 | return Method->isStatic(); |
| 434 | |
| 435 | if (getStorageClass() == Static) |
| 436 | return false; |
| 437 | |
| 438 | for (const DeclContext *DC = getDeclContext(); |
| 439 | DC->isNamespace(); |
| 440 | DC = DC->getParent()) { |
| 441 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 442 | if (!Namespace->getDeclName()) |
| 443 | return false; |
| 444 | break; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | return true; |
| 449 | } |
| 450 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 451 | /// \brief Returns a value indicating whether this function |
| 452 | /// corresponds to a builtin function. |
| 453 | /// |
| 454 | /// The function corresponds to a built-in function if it is |
| 455 | /// declared at translation scope or within an extern "C" block and |
| 456 | /// its name matches with the name of a builtin. The returned value |
| 457 | /// will be 0 for functions that do not correspond to a builtin, a |
| 458 | /// value of type \c Builtin::ID if in the target-independent range |
| 459 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 460 | unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const { |
| 461 | if (!getIdentifier() || !getIdentifier()->getBuiltinID()) |
| 462 | return 0; |
| 463 | |
| 464 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| 465 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 466 | return BuiltinID; |
| 467 | |
| 468 | // This function has the name of a known C library |
| 469 | // function. Determine whether it actually refers to the C library |
| 470 | // function or whether it just has the same name. |
| 471 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 472 | // If this is a static function, it's not a builtin. |
| 473 | if (getStorageClass() == Static) |
| 474 | return 0; |
| 475 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 476 | // If this function is at translation-unit scope and we're not in |
| 477 | // C++, it refers to the C library function. |
| 478 | if (!Context.getLangOptions().CPlusPlus && |
| 479 | getDeclContext()->isTranslationUnit()) |
| 480 | return BuiltinID; |
| 481 | |
| 482 | // If the function is in an extern "C" linkage specification and is |
| 483 | // not marked "overloadable", it's the real function. |
| 484 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
| 485 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
| 486 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 487 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 488 | return BuiltinID; |
| 489 | |
| 490 | // Not a builtin |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 495 | /// getNumParams - Return the number of parameters this function must have |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 496 | /// based on its FunctionType. This is the length of the PararmInfo array |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 497 | /// after it has been created. |
| 498 | unsigned FunctionDecl::getNumParams() const { |
Chris Lattner | 11ddb7d | 2009-04-25 05:56:45 +0000 | [diff] [blame] | 499 | const FunctionType *FT = getType()->getAsFunctionType(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 500 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 501 | return 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 502 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Chris Lattner | 11ddb7d | 2009-04-25 05:56:45 +0000 | [diff] [blame] | 503 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 506 | void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 507 | unsigned NumParams) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 508 | assert(ParamInfo == 0 && "Already has param info!"); |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 509 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 510 | |
| 511 | // Zero params -> null pointer. |
| 512 | if (NumParams) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 513 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 514 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 515 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 516 | |
Argyrios Kyrtzidis | 96888cc | 2009-06-23 00:42:00 +0000 | [diff] [blame] | 517 | // Update source range. The check below allows us to set EndRangeLoc before |
| 518 | // setting the parameters. |
Argyrios Kyrtzidis | cb5f8f5 | 2009-06-23 00:42:15 +0000 | [diff] [blame] | 519 | if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 520 | EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 524 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 525 | /// needed to call this function. This may be fewer than the number of |
| 526 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 527 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 528 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 529 | unsigned NumRequiredArgs = getNumParams(); |
| 530 | while (NumRequiredArgs > 0 |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 531 | && getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 532 | --NumRequiredArgs; |
| 533 | |
| 534 | return NumRequiredArgs; |
| 535 | } |
| 536 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 537 | bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 538 | if (!isInline() || !hasAttr<GNUInlineAttr>()) |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 539 | return false; |
| 540 | |
| 541 | for (const FunctionDecl *FD = getPreviousDeclaration(); FD; |
| 542 | FD = FD->getPreviousDeclaration()) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 543 | if (FD->isInline() && !FD->hasAttr<GNUInlineAttr>()) |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 544 | return false; |
| 545 | } |
| 546 | |
| 547 | return true; |
| 548 | } |
| 549 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 550 | bool FunctionDecl::isExternGNUInline(ASTContext &Context) const { |
| 551 | if (!hasActiveGNUInlineAttribute(Context)) |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 552 | return false; |
| 553 | |
| 554 | for (const FunctionDecl *FD = this; FD; FD = FD->getPreviousDeclaration()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 555 | if (FD->getStorageClass() == Extern && FD->hasAttr<GNUInlineAttr>()) |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 556 | return true; |
| 557 | |
| 558 | return false; |
| 559 | } |
| 560 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 561 | void |
| 562 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| 563 | PreviousDeclaration = PrevDecl; |
| 564 | |
| 565 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 566 | FunctionTemplateDecl *PrevFunTmpl |
| 567 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 568 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 569 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 570 | } |
| 571 | } |
| 572 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 573 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 574 | /// function represents, if any. |
| 575 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 576 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 577 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 578 | else |
| 579 | return OO_None; |
| 580 | } |
| 581 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 582 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
| 583 | if (FunctionTemplateSpecializationInfo *Info |
| 584 | = TemplateOrSpecialization |
| 585 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 586 | return Info->Template.getPointer(); |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 587 | } |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | const TemplateArgumentList * |
| 592 | FunctionDecl::getTemplateSpecializationArgs() const { |
| 593 | if (FunctionTemplateSpecializationInfo *Info |
| 594 | = TemplateOrSpecialization |
| 595 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 596 | return Info->TemplateArguments; |
| 597 | } |
| 598 | return 0; |
| 599 | } |
| 600 | |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 601 | void |
| 602 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context, |
| 603 | FunctionTemplateDecl *Template, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 604 | const TemplateArgumentList *TemplateArgs, |
| 605 | void *InsertPos) { |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 606 | FunctionTemplateSpecializationInfo *Info |
| 607 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 608 | if (!Info) |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 609 | Info = new (Context) FunctionTemplateSpecializationInfo; |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 610 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 611 | Info->Function = this; |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 612 | Info->Template.setPointer(Template); |
| 613 | Info->Template.setInt(0); // Implicit instantiation, unless told otherwise |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 614 | Info->TemplateArguments = TemplateArgs; |
| 615 | TemplateOrSpecialization = Info; |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 616 | |
| 617 | // Insert this function template specialization into the set of known |
| 618 | // function template specialiations. |
| 619 | Template->getSpecializations().InsertNode(Info, InsertPos); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 622 | bool FunctionDecl::isExplicitSpecialization() const { |
| 623 | // FIXME: check this property for explicit specializations of member |
| 624 | // functions of class templates. |
| 625 | FunctionTemplateSpecializationInfo *Info |
| 626 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
| 627 | if (!Info) |
| 628 | return false; |
| 629 | |
| 630 | return Info->isExplicitSpecialization(); |
| 631 | } |
| 632 | |
| 633 | void FunctionDecl::setExplicitSpecialization(bool ES) { |
| 634 | // FIXME: set this property for explicit specializations of member functions |
| 635 | // of class templates. |
| 636 | FunctionTemplateSpecializationInfo *Info |
| 637 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
| 638 | if (Info) |
| 639 | Info->setExplicitSpecialization(ES); |
| 640 | } |
| 641 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 642 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 643 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 644 | //===----------------------------------------------------------------------===// |
| 645 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 646 | void TagDecl::startDefinition() { |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 647 | TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType()); |
| 648 | TagT->decl.setPointer(this); |
| 649 | TagT->getAsTagType()->decl.setInt(1); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | void TagDecl::completeDefinition() { |
| 653 | assert((!TypeForDecl || |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 654 | TypeForDecl->getAsTagType()->decl.getPointer() == this) && |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 655 | "Attempt to redefine a tag definition?"); |
| 656 | IsDefinition = true; |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 657 | TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType()); |
| 658 | TagT->decl.setPointer(this); |
| 659 | TagT->decl.setInt(0); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 662 | TagDecl* TagDecl::getDefinition(ASTContext& C) const { |
| 663 | QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this)); |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 664 | TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl()); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 665 | return D->isDefinition() ? D : 0; |
| 666 | } |
| 667 | |
| 668 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 669 | // RecordDecl Implementation |
| 670 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 671 | |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 672 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 673 | IdentifierInfo *Id) |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 674 | : TagDecl(DK, TK, DC, L, Id) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 675 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 676 | AnonymousStructOrUnion = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 677 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 681 | SourceLocation L, IdentifierInfo *Id, |
| 682 | RecordDecl* PrevDecl) { |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 683 | |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 684 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 685 | C.getTypeDeclType(R, PrevDecl); |
| 686 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 689 | RecordDecl::~RecordDecl() { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | void RecordDecl::Destroy(ASTContext& C) { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 693 | TagDecl::Destroy(C); |
| 694 | } |
| 695 | |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 696 | bool RecordDecl::isInjectedClassName() const { |
| 697 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
| 698 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 699 | } |
| 700 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 701 | /// completeDefinition - Notes that the definition of this type is now |
| 702 | /// complete. |
| 703 | void RecordDecl::completeDefinition(ASTContext& C) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 704 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 705 | TagDecl::completeDefinition(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 708 | //===----------------------------------------------------------------------===// |
| 709 | // BlockDecl Implementation |
| 710 | //===----------------------------------------------------------------------===// |
| 711 | |
| 712 | BlockDecl::~BlockDecl() { |
| 713 | } |
| 714 | |
| 715 | void BlockDecl::Destroy(ASTContext& C) { |
| 716 | if (Body) |
| 717 | Body->Destroy(C); |
| 718 | |
| 719 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 720 | (*I)->Destroy(C); |
Ted Kremenek | 879d27a | 2009-03-13 23:17:24 +0000 | [diff] [blame] | 721 | |
| 722 | C.Deallocate(ParamInfo); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 723 | Decl::Destroy(C); |
| 724 | } |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 725 | |
| 726 | void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 727 | unsigned NParms) { |
| 728 | assert(ParamInfo == 0 && "Already has param info!"); |
| 729 | |
| 730 | // Zero params -> null pointer. |
| 731 | if (NParms) { |
| 732 | NumParams = NParms; |
| 733 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
| 734 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
| 735 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | unsigned BlockDecl::getNumParams() const { |
| 740 | return NumParams; |
| 741 | } |