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" |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Stmt.h" |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 22 | #include "clang/AST/PrettyPrinter.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 24 | #include "clang/Basic/IdentifierTable.h" |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 25 | #include "clang/Parse/DeclSpec.h" |
| 26 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 27 | #include <vector> |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 28 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 31 | void Attr::Destroy(ASTContext &C) { |
| 32 | if (Next) { |
| 33 | Next->Destroy(C); |
| 34 | Next = 0; |
| 35 | } |
| 36 | this->~Attr(); |
| 37 | C.Deallocate((void*)this); |
| 38 | } |
| 39 | |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 40 | /// \brief Return the TypeLoc wrapper for the type source info. |
| 41 | TypeLoc DeclaratorInfo::getTypeLoc() const { |
Argyrios Kyrtzidis | b735471 | 2009-09-29 19:40:20 +0000 | [diff] [blame] | 42 | return TypeLoc(Ty, (void*)(this + 1)); |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 43 | } |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 44 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 46 | // Decl Allocation/Deallocation Method Implementations |
| 47 | //===----------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 49 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 50 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 51 | return new (C) TranslationUnitDecl(C); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 54 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 55 | SourceLocation L, IdentifierInfo *Id) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 56 | return new (C) NamespaceDecl(DC, L, Id); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 59 | void NamespaceDecl::Destroy(ASTContext& C) { |
| 60 | // NamespaceDecl uses "NextDeclarator" to chain namespace declarations |
| 61 | // together. They are all top-level Decls. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | ebf27b1 | 2008-05-24 15:09:56 +0000 | [diff] [blame] | 63 | this->~NamespaceDecl(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 64 | C.Deallocate((void *)this); |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 68 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 69 | SourceLocation L, IdentifierInfo *Id, QualType T) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 70 | return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Daniel Dunbar | b286a78 | 2009-04-14 02:08:49 +0000 | [diff] [blame] | 73 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 74 | switch (SC) { |
| 75 | case VarDecl::None: break; |
| 76 | case VarDecl::Auto: return "auto"; break; |
| 77 | case VarDecl::Extern: return "extern"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | case VarDecl::PrivateExtern: return "__private_extern__"; break; |
Daniel Dunbar | b286a78 | 2009-04-14 02:08:49 +0000 | [diff] [blame] | 79 | case VarDecl::Register: return "register"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | case VarDecl::Static: return "static"; break; |
Daniel Dunbar | b286a78 | 2009-04-14 02:08:49 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | assert(0 && "Invalid storage class"); |
| 84 | return 0; |
| 85 | } |
| 86 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 87 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 88 | SourceLocation L, IdentifierInfo *Id, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 89 | QualType T, DeclaratorInfo *DInfo, |
| 90 | StorageClass S, Expr *DefArg) { |
| 91 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, DInfo, S, DefArg); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 94 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 95 | if (const Expr *E = getInit()) |
| 96 | return E->getSourceRange(); |
| 97 | |
| 98 | if (const Expr *E = getUninstantiatedDefaultArg()) |
| 99 | return E->getSourceRange(); |
| 100 | |
| 101 | return SourceRange(); |
| 102 | } |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 103 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 104 | void VarDecl::setInit(ASTContext &C, Expr *I) { |
| 105 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| 106 | Eval->~EvaluatedStmt(); |
| 107 | C.Deallocate(Eval); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 110 | Init = I; |
| 111 | } |
| 112 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 113 | bool VarDecl::isExternC() const { |
| 114 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 115 | if (!Context.getLangOptions().CPlusPlus) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | return (getDeclContext()->isTranslationUnit() && |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 117 | getStorageClass() != Static) || |
| 118 | (getDeclContext()->isFunctionOrMethod() && hasExternalStorage()); |
| 119 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 121 | DC = DC->getParent()) { |
| 122 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 123 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
| 124 | return getStorageClass() != Static; |
| 125 | |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | if (DC->isFunctionOrMethod()) |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | return false; |
| 134 | } |
| 135 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 136 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | SourceLocation L, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 138 | DeclarationName N, QualType T, |
| 139 | DeclaratorInfo *DInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | StorageClass S, bool isInline, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 141 | bool hasWrittenPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | FunctionDecl *New |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 143 | = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline); |
Anders Carlsson | a75e853 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 144 | New->HasWrittenPrototype = hasWrittenPrototype; |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 145 | return New; |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 148 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 149 | return new (C) BlockDecl(DC, L); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 152 | FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 153 | IdentifierInfo *Id, QualType T, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 154 | DeclaratorInfo *DInfo, Expr *BW, bool Mutable) { |
| 155 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 158 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 159 | if (!isImplicit() || getDeclName()) |
| 160 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 162 | if (const RecordType *Record = getType()->getAs<RecordType>()) |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 163 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 164 | |
| 165 | return false; |
| 166 | } |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 168 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 169 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 170 | IdentifierInfo *Id, QualType T, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 171 | Expr *E, const llvm::APSInt &V) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 172 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Ted Kremenek | d1ac17a | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 175 | void EnumConstantDecl::Destroy(ASTContext& C) { |
| 176 | if (Init) Init->Destroy(C); |
| 177 | Decl::Destroy(C); |
| 178 | } |
| 179 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 180 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 181 | SourceLocation L, IdentifierInfo *Id, |
| 182 | DeclaratorInfo *DInfo) { |
| 183 | return new (C) TypedefDecl(DC, L, Id, DInfo); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 186 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 187 | IdentifierInfo *Id, SourceLocation TKL, |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 188 | EnumDecl *PrevDecl) { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 189 | EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL); |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 190 | C.getTypeDeclType(Enum, PrevDecl); |
| 191 | return Enum; |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Ted Kremenek | df91eca | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 194 | void EnumDecl::Destroy(ASTContext& C) { |
Ted Kremenek | df91eca | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 195 | Decl::Destroy(C); |
| 196 | } |
| 197 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 198 | void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) { |
| 199 | assert(!isDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 200 | IntegerType = NewType; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 201 | TagDecl::completeDefinition(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 204 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 205 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 206 | StringLiteral *Str) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 207 | return new (C) FileScopeAsmDecl(DC, L, Str); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 210 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 211 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 212 | //===----------------------------------------------------------------------===// |
| 213 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 214 | std::string NamedDecl::getQualifiedNameAsString() const { |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 215 | return getQualifiedNameAsString(getASTContext().getLangOptions()); |
| 216 | } |
| 217 | |
| 218 | std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 219 | // FIXME: Collect contexts, then accumulate names to avoid unnecessary |
| 220 | // std::string thrashing. |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 221 | std::vector<std::string> Names; |
| 222 | std::string QualName; |
| 223 | const DeclContext *Ctx = getDeclContext(); |
| 224 | |
| 225 | if (Ctx->isFunctionOrMethod()) |
| 226 | return getNameAsString(); |
| 227 | |
| 228 | while (Ctx) { |
| 229 | if (Ctx->isFunctionOrMethod()) |
| 230 | // FIXME: That probably will happen, when D was member of local |
| 231 | // scope class/struct/union. How do we handle this case? |
| 232 | break; |
| 233 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | if (const ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 235 | = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) { |
| 236 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 237 | std::string TemplateArgsStr |
| 238 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 239 | TemplateArgs.getFlatArgumentList(), |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 240 | TemplateArgs.flat_size(), |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 241 | P); |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 242 | Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 243 | } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx)) |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 244 | Names.push_back(ND->getNameAsString()); |
| 245 | else |
| 246 | break; |
| 247 | |
| 248 | Ctx = Ctx->getParent(); |
| 249 | } |
| 250 | |
| 251 | std::vector<std::string>::reverse_iterator |
| 252 | I = Names.rbegin(), |
| 253 | End = Names.rend(); |
| 254 | |
| 255 | for (; I!=End; ++I) |
| 256 | QualName += *I + "::"; |
| 257 | |
| 258 | QualName += getNameAsString(); |
| 259 | |
| 260 | return QualName; |
| 261 | } |
| 262 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 263 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 264 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 265 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 266 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 267 | // We want to keep it, unless it nominates same namespace. |
| 268 | if (getKind() == Decl::UsingDirective) { |
| 269 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == |
| 270 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); |
| 271 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 273 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 274 | // For function declarations, we keep track of redeclarations. |
| 275 | return FD->getPreviousDeclaration() == OldD; |
| 276 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 277 | // For function templates, the underlying function declarations are linked. |
| 278 | if (const FunctionTemplateDecl *FunctionTemplate |
| 279 | = dyn_cast<FunctionTemplateDecl>(this)) |
| 280 | if (const FunctionTemplateDecl *OldFunctionTemplate |
| 281 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 282 | return FunctionTemplate->getTemplatedDecl() |
| 283 | ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 285 | // For method declarations, we keep track of redeclarations. |
| 286 | if (isa<ObjCMethodDecl>(this)) |
| 287 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 288 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 289 | if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) |
| 290 | return true; |
| 291 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 292 | if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) |
| 293 | return cast<UsingShadowDecl>(this)->getTargetDecl() == |
| 294 | cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 295 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 296 | // For non-function declarations, if the declarations are of the |
| 297 | // same kind then this must be a redeclaration, or semantic analysis |
| 298 | // would not have given us the new declaration. |
| 299 | return this->getKind() == OldD->getKind(); |
| 300 | } |
| 301 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 302 | bool NamedDecl::hasLinkage() const { |
| 303 | if (const VarDecl *VD = dyn_cast<VarDecl>(this)) |
| 304 | return VD->hasExternalStorage() || VD->isFileVarDecl(); |
| 305 | |
| 306 | if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this)) |
| 307 | return true; |
| 308 | |
| 309 | return false; |
| 310 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 311 | |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 312 | NamedDecl *NamedDecl::getUnderlyingDecl() { |
| 313 | NamedDecl *ND = this; |
| 314 | while (true) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 315 | if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 316 | ND = UD->getTargetDecl(); |
| 317 | else if (ObjCCompatibleAliasDecl *AD |
| 318 | = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 319 | return AD->getClassInterface(); |
| 320 | else |
| 321 | return ND; |
| 322 | } |
| 323 | } |
| 324 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 325 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 326 | // DeclaratorDecl Implementation |
| 327 | //===----------------------------------------------------------------------===// |
| 328 | |
| 329 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 330 | if (DeclInfo) { |
| 331 | TypeLoc TL = DeclInfo->getTypeLoc(); |
| 332 | while (true) { |
| 333 | TypeLoc NextTL = TL.getNextTypeLoc(); |
| 334 | if (!NextTL) |
| 335 | return TL.getSourceRange().getBegin(); |
| 336 | TL = NextTL; |
| 337 | } |
| 338 | } |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 339 | return SourceLocation(); |
| 340 | } |
| 341 | |
| 342 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 343 | // VarDecl Implementation |
| 344 | //===----------------------------------------------------------------------===// |
| 345 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 346 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 347 | IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 348 | StorageClass S) { |
| 349 | return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void VarDecl::Destroy(ASTContext& C) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 353 | Expr *Init = getInit(); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 354 | if (Init) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 355 | Init->Destroy(C); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 356 | if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) { |
| 357 | Eval->~EvaluatedStmt(); |
| 358 | C.Deallocate(Eval); |
| 359 | } |
| 360 | } |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 361 | this->~VarDecl(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 362 | C.Deallocate((void *)this); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | VarDecl::~VarDecl() { |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 368 | SourceRange VarDecl::getSourceRange() const { |
| 369 | if (getInit()) |
| 370 | return SourceRange(getLocation(), getInit()->getLocEnd()); |
| 371 | return SourceRange(getLocation(), getLocation()); |
| 372 | } |
| 373 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 374 | bool VarDecl::isOutOfLine() const { |
| 375 | if (!isStaticDataMember()) |
| 376 | return false; |
| 377 | |
| 378 | if (Decl::isOutOfLine()) |
| 379 | return true; |
| 380 | |
| 381 | // If this static data member was instantiated from a static data member of |
| 382 | // a class template, check whether that static data member was defined |
| 383 | // out-of-line. |
| 384 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 385 | return VD->isOutOfLine(); |
| 386 | |
| 387 | return false; |
| 388 | } |
| 389 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 390 | VarDecl *VarDecl::getOutOfLineDefinition() { |
| 391 | if (!isStaticDataMember()) |
| 392 | return 0; |
| 393 | |
| 394 | for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); |
| 395 | RD != RDEnd; ++RD) { |
| 396 | if (RD->getLexicalDeclContext()->isFileContext()) |
| 397 | return *RD; |
| 398 | } |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 403 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 404 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 405 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 410 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 411 | if (MemberSpecializationInfo *MSI |
| 412 | = getASTContext().getInstantiatedFromStaticDataMember(this)) |
| 413 | return MSI->getTemplateSpecializationKind(); |
| 414 | |
| 415 | return TSK_Undeclared; |
| 416 | } |
| 417 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 418 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 419 | return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 420 | } |
| 421 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 422 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 423 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 424 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 425 | assert(MSI && "Not an instantiated static data member?"); |
| 426 | MSI->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 427 | if (TSK != TSK_ExplicitSpecialization && |
| 428 | PointOfInstantiation.isValid() && |
| 429 | MSI->getPointOfInstantiation().isInvalid()) |
| 430 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 433 | bool VarDecl::isTentativeDefinition(ASTContext &Context) const { |
| 434 | if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus) |
| 435 | return false; |
| 436 | |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 437 | const VarDecl *Def = 0; |
| 438 | return (!getDefinition(Def) && |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 439 | (getStorageClass() == None || getStorageClass() == Static)); |
| 440 | } |
| 441 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 442 | const Expr *VarDecl::getDefinition(const VarDecl *&Def) const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 443 | redecl_iterator I = redecls_begin(), E = redecls_end(); |
| 444 | while (I != E && !I->getInit()) |
| 445 | ++I; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 446 | |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 447 | if (I != E) { |
| 448 | Def = *I; |
| 449 | return I->getInit(); |
| 450 | } |
| 451 | return 0; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 454 | VarDecl *VarDecl::getCanonicalDecl() { |
Argyrios Kyrtzidis | f23e839 | 2009-07-18 08:50:13 +0000 | [diff] [blame] | 455 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 458 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 459 | // FunctionDecl Implementation |
| 460 | //===----------------------------------------------------------------------===// |
| 461 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 462 | void FunctionDecl::Destroy(ASTContext& C) { |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 463 | if (Body && Body.isOffset()) |
| 464 | Body.get(C.getExternalSource())->Destroy(C); |
Ted Kremenek | b65cf41 | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 465 | |
| 466 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 467 | (*I)->Destroy(C); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 468 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 469 | FunctionTemplateSpecializationInfo *FTSInfo |
| 470 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
| 471 | if (FTSInfo) |
| 472 | C.Deallocate(FTSInfo); |
| 473 | |
| 474 | MemberSpecializationInfo *MSInfo |
| 475 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 476 | if (MSInfo) |
| 477 | C.Deallocate(MSInfo); |
| 478 | |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 479 | C.Deallocate(ParamInfo); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 480 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 481 | Decl::Destroy(C); |
| 482 | } |
| 483 | |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 484 | void FunctionDecl::getNameForDiagnostic(std::string &S, |
| 485 | const PrintingPolicy &Policy, |
| 486 | bool Qualified) const { |
| 487 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 488 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 489 | if (TemplateArgs) |
| 490 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 491 | TemplateArgs->getFlatArgumentList(), |
| 492 | TemplateArgs->flat_size(), |
| 493 | Policy); |
| 494 | |
| 495 | } |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 496 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 497 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 498 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 499 | if (I->Body) { |
| 500 | Definition = *I; |
| 501 | return I->Body.get(getASTContext().getExternalSource()); |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 508 | void FunctionDecl::setBody(Stmt *B) { |
| 509 | Body = B; |
Argyrios Kyrtzidis | 1a5364e | 2009-06-22 17:13:31 +0000 | [diff] [blame] | 510 | if (B) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 511 | EndRangeLoc = B->getLocEnd(); |
| 512 | } |
| 513 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 514 | bool FunctionDecl::isMain() const { |
| 515 | ASTContext &Context = getASTContext(); |
John McCall | 07a5c22 | 2009-08-15 02:09:25 +0000 | [diff] [blame] | 516 | return !Context.getLangOptions().Freestanding && |
| 517 | getDeclContext()->getLookupContext()->isTranslationUnit() && |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 518 | getIdentifier() && getIdentifier()->isStr("main"); |
| 519 | } |
| 520 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 521 | bool FunctionDecl::isExternC() const { |
| 522 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 523 | // In C, any non-static, non-overloadable function has external |
| 524 | // linkage. |
| 525 | if (!Context.getLangOptions().CPlusPlus) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 526 | return getStorageClass() != Static && !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 527 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 529 | DC = DC->getParent()) { |
| 530 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 531 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | return getStorageClass() != Static && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 533 | !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 534 | |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | return false; |
| 540 | } |
| 541 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 542 | bool FunctionDecl::isGlobal() const { |
| 543 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 544 | return Method->isStatic(); |
| 545 | |
| 546 | if (getStorageClass() == Static) |
| 547 | return false; |
| 548 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 550 | DC->isNamespace(); |
| 551 | DC = DC->getParent()) { |
| 552 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 553 | if (!Namespace->getDeclName()) |
| 554 | return false; |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | return true; |
| 560 | } |
| 561 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 562 | /// \brief Returns a value indicating whether this function |
| 563 | /// corresponds to a builtin function. |
| 564 | /// |
| 565 | /// The function corresponds to a built-in function if it is |
| 566 | /// declared at translation scope or within an extern "C" block and |
| 567 | /// its name matches with the name of a builtin. The returned value |
| 568 | /// will be 0 for functions that do not correspond to a builtin, a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 570 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 571 | unsigned FunctionDecl::getBuiltinID() const { |
| 572 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 573 | if (!getIdentifier() || !getIdentifier()->getBuiltinID()) |
| 574 | return 0; |
| 575 | |
| 576 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| 577 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 578 | return BuiltinID; |
| 579 | |
| 580 | // This function has the name of a known C library |
| 581 | // function. Determine whether it actually refers to the C library |
| 582 | // function or whether it just has the same name. |
| 583 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 584 | // If this is a static function, it's not a builtin. |
| 585 | if (getStorageClass() == Static) |
| 586 | return 0; |
| 587 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 588 | // If this function is at translation-unit scope and we're not in |
| 589 | // C++, it refers to the C library function. |
| 590 | if (!Context.getLangOptions().CPlusPlus && |
| 591 | getDeclContext()->isTranslationUnit()) |
| 592 | return BuiltinID; |
| 593 | |
| 594 | // If the function is in an extern "C" linkage specification and is |
| 595 | // not marked "overloadable", it's the real function. |
| 596 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 598 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 599 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 600 | return BuiltinID; |
| 601 | |
| 602 | // Not a builtin |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 607 | /// getNumParams - Return the number of parameters this function must have |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 608 | /// based on its FunctionType. This is the length of the PararmInfo array |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 609 | /// after it has been created. |
| 610 | unsigned FunctionDecl::getNumParams() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 611 | const FunctionType *FT = getType()->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 612 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 613 | return 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 614 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 618 | void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 619 | unsigned NumParams) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 620 | assert(ParamInfo == 0 && "Already has param info!"); |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 621 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 623 | // Zero params -> null pointer. |
| 624 | if (NumParams) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 625 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 626 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 627 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 628 | |
Argyrios Kyrtzidis | 96888cc | 2009-06-23 00:42:00 +0000 | [diff] [blame] | 629 | // Update source range. The check below allows us to set EndRangeLoc before |
| 630 | // setting the parameters. |
Argyrios Kyrtzidis | cb5f8f5 | 2009-06-23 00:42:15 +0000 | [diff] [blame] | 631 | if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 632 | EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 636 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 637 | /// needed to call this function. This may be fewer than the number of |
| 638 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 639 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 640 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 641 | unsigned NumRequiredArgs = getNumParams(); |
| 642 | while (NumRequiredArgs > 0 |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 643 | && getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 644 | --NumRequiredArgs; |
| 645 | |
| 646 | return NumRequiredArgs; |
| 647 | } |
| 648 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 649 | bool FunctionDecl::isInlined() const { |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 650 | if (isInlineSpecified() || (isa<CXXMethodDecl>(this) && !isOutOfLine())) |
| 651 | return true; |
| 652 | |
| 653 | switch (getTemplateSpecializationKind()) { |
| 654 | case TSK_Undeclared: |
| 655 | case TSK_ExplicitSpecialization: |
| 656 | return false; |
| 657 | |
| 658 | case TSK_ImplicitInstantiation: |
| 659 | case TSK_ExplicitInstantiationDeclaration: |
| 660 | case TSK_ExplicitInstantiationDefinition: |
| 661 | // Handle below. |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
| 666 | Stmt *Pattern = 0; |
| 667 | if (PatternDecl) |
| 668 | Pattern = PatternDecl->getBody(PatternDecl); |
| 669 | |
| 670 | if (Pattern && PatternDecl) |
| 671 | return PatternDecl->isInlined(); |
| 672 | |
| 673 | return false; |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 676 | /// \brief For an inline function definition in C or C++, determine whether the |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 677 | /// definition will be externally visible. |
| 678 | /// |
| 679 | /// Inline function definitions are always available for inlining optimizations. |
| 680 | /// However, depending on the language dialect, declaration specifiers, and |
| 681 | /// attributes, the definition of an inline function may or may not be |
| 682 | /// "externally" visible to other translation units in the program. |
| 683 | /// |
| 684 | /// In C99, inline definitions are not externally visible by default. However, |
| 685 | /// if even one of the globa-scope declarations is marked "extern inline", the |
| 686 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 687 | /// |
| 688 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 689 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 690 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 691 | /// an externally visible symbol, but "extern inline" will not create an |
| 692 | /// externally visible symbol. |
| 693 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
| 694 | assert(isThisDeclarationADefinition() && "Must have the function definition"); |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 695 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 696 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 697 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 698 | if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) { |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 699 | // GNU inline semantics. Based on a number of examples, we came up with the |
| 700 | // following heuristic: if the "inline" keyword is present on a |
| 701 | // declaration of the function but "extern" is not present on that |
| 702 | // declaration, then the symbol is externally visible. Otherwise, the GNU |
| 703 | // "extern inline" semantics applies and the symbol is not externally |
| 704 | // visible. |
| 705 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 706 | Redecl != RedeclEnd; |
| 707 | ++Redecl) { |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 708 | if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 709 | return true; |
| 710 | } |
| 711 | |
| 712 | // GNU "extern inline" semantics; no externally visible symbol. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 713 | return false; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | // C99 6.7.4p6: |
| 717 | // [...] If all of the file scope declarations for a function in a |
| 718 | // translation unit include the inline function specifier without extern, |
| 719 | // then the definition in that translation unit is an inline definition. |
| 720 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 721 | Redecl != RedeclEnd; |
| 722 | ++Redecl) { |
| 723 | // Only consider file-scope declarations in this test. |
| 724 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 725 | continue; |
| 726 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 727 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 728 | return true; // Not an inline definition |
| 729 | } |
| 730 | |
| 731 | // C99 6.7.4p6: |
| 732 | // An inline definition does not provide an external definition for the |
| 733 | // function, and does not forbid an external definition in another |
| 734 | // translation unit. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 735 | return false; |
| 736 | } |
| 737 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 738 | void |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 739 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 740 | redeclarable_base::setPreviousDeclaration(PrevDecl); |
Argyrios Kyrtzidis | f23e839 | 2009-07-18 08:50:13 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 742 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | FunctionTemplateDecl *PrevFunTmpl |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 744 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 745 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 746 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 747 | } |
| 748 | } |
| 749 | |
Mike Stump | 740256b | 2009-09-29 00:50:50 +0000 | [diff] [blame] | 750 | const FunctionDecl *FunctionDecl::getCanonicalDecl() const { |
| 751 | return getFirstDeclaration(); |
| 752 | } |
| 753 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 754 | FunctionDecl *FunctionDecl::getCanonicalDecl() { |
Argyrios Kyrtzidis | f23e839 | 2009-07-18 08:50:13 +0000 | [diff] [blame] | 755 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 758 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 759 | /// function represents, if any. |
| 760 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 761 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 762 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 763 | else |
| 764 | return OO_None; |
| 765 | } |
| 766 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 767 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 768 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 769 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 774 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 775 | return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 776 | } |
| 777 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 778 | void |
| 779 | FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD, |
| 780 | TemplateSpecializationKind TSK) { |
| 781 | assert(TemplateOrSpecialization.isNull() && |
| 782 | "Member function is already a specialization"); |
| 783 | MemberSpecializationInfo *Info |
| 784 | = new (getASTContext()) MemberSpecializationInfo(FD, TSK); |
| 785 | TemplateOrSpecialization = Info; |
| 786 | } |
| 787 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 788 | bool FunctionDecl::isImplicitlyInstantiable() const { |
| 789 | // If this function already has a definition or is invalid, it can't be |
| 790 | // implicitly instantiated. |
| 791 | if (isInvalidDecl() || getBody()) |
| 792 | return false; |
| 793 | |
| 794 | switch (getTemplateSpecializationKind()) { |
| 795 | case TSK_Undeclared: |
| 796 | case TSK_ExplicitSpecialization: |
| 797 | case TSK_ExplicitInstantiationDefinition: |
| 798 | return false; |
| 799 | |
| 800 | case TSK_ImplicitInstantiation: |
| 801 | return true; |
| 802 | |
| 803 | case TSK_ExplicitInstantiationDeclaration: |
| 804 | // Handled below. |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | // Find the actual template from which we will instantiate. |
| 809 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
| 810 | Stmt *Pattern = 0; |
| 811 | if (PatternDecl) |
| 812 | Pattern = PatternDecl->getBody(PatternDecl); |
| 813 | |
| 814 | // C++0x [temp.explicit]p9: |
| 815 | // Except for inline functions, other explicit instantiation declarations |
| 816 | // have the effect of suppressing the implicit instantiation of the entity |
| 817 | // to which they refer. |
| 818 | if (!Pattern || !PatternDecl) |
| 819 | return true; |
| 820 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 821 | return PatternDecl->isInlined(); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
| 825 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 826 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 827 | // If we have hit a point where the user provided a specialization of |
| 828 | // this template, we're done looking. |
| 829 | if (Primary->isMemberSpecialization()) |
| 830 | break; |
| 831 | |
| 832 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 833 | } |
| 834 | |
| 835 | return Primary->getTemplatedDecl(); |
| 836 | } |
| 837 | |
| 838 | return getInstantiatedFromMemberFunction(); |
| 839 | } |
| 840 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 841 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 843 | = TemplateOrSpecialization |
| 844 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 845 | return Info->Template.getPointer(); |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 846 | } |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | const TemplateArgumentList * |
| 851 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 853 | = TemplateOrSpecialization |
| 854 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 855 | return Info->TemplateArguments; |
| 856 | } |
| 857 | return 0; |
| 858 | } |
| 859 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 860 | void |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 861 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context, |
| 862 | FunctionTemplateDecl *Template, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 863 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 864 | void *InsertPos, |
| 865 | TemplateSpecializationKind TSK) { |
| 866 | assert(TSK != TSK_Undeclared && |
| 867 | "Must specify the type of function template specialization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 869 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 870 | if (!Info) |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 871 | Info = new (Context) FunctionTemplateSpecializationInfo; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 872 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 873 | Info->Function = this; |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 874 | Info->Template.setPointer(Template); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 875 | Info->Template.setInt(TSK - 1); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 876 | Info->TemplateArguments = TemplateArgs; |
| 877 | TemplateOrSpecialization = Info; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 879 | // Insert this function template specialization into the set of known |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 880 | // function template specializations. |
| 881 | if (InsertPos) |
| 882 | Template->getSpecializations().InsertNode(Info, InsertPos); |
| 883 | else { |
| 884 | // Try to insert the new node. If there is an existing node, remove it |
| 885 | // first. |
| 886 | FunctionTemplateSpecializationInfo *Existing |
| 887 | = Template->getSpecializations().GetOrInsertNode(Info); |
| 888 | if (Existing) { |
| 889 | Template->getSpecializations().RemoveNode(Existing); |
| 890 | Template->getSpecializations().GetOrInsertNode(Info); |
| 891 | } |
| 892 | } |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 895 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 896 | // For a function template specialization, query the specialization |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 897 | // information object. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 898 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 899 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 900 | if (FTSInfo) |
| 901 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 903 | MemberSpecializationInfo *MSInfo |
| 904 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 905 | if (MSInfo) |
| 906 | return MSInfo->getTemplateSpecializationKind(); |
| 907 | |
| 908 | return TSK_Undeclared; |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | void |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 912 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 913 | SourceLocation PointOfInstantiation) { |
| 914 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 915 | = TemplateOrSpecialization.dyn_cast< |
| 916 | FunctionTemplateSpecializationInfo*>()) { |
| 917 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 918 | if (TSK != TSK_ExplicitSpecialization && |
| 919 | PointOfInstantiation.isValid() && |
| 920 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 921 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 922 | } else if (MemberSpecializationInfo *MSInfo |
| 923 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 924 | MSInfo->setTemplateSpecializationKind(TSK); |
| 925 | if (TSK != TSK_ExplicitSpecialization && |
| 926 | PointOfInstantiation.isValid() && |
| 927 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 928 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 929 | } else |
| 930 | assert(false && "Function cannot have a template specialization kind"); |
| 931 | } |
| 932 | |
| 933 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 934 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 935 | = TemplateOrSpecialization.dyn_cast< |
| 936 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 937 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 938 | else if (MemberSpecializationInfo *MSInfo |
| 939 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 940 | return MSInfo->getPointOfInstantiation(); |
| 941 | |
| 942 | return SourceLocation(); |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 945 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 946 | if (Decl::isOutOfLine()) |
| 947 | return true; |
| 948 | |
| 949 | // If this function was instantiated from a member function of a |
| 950 | // class template, check whether that member function was defined out-of-line. |
| 951 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 952 | const FunctionDecl *Definition; |
| 953 | if (FD->getBody(Definition)) |
| 954 | return Definition->isOutOfLine(); |
| 955 | } |
| 956 | |
| 957 | // If this function was instantiated from a function template, |
| 958 | // check whether that function template was defined out-of-line. |
| 959 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 960 | const FunctionDecl *Definition; |
| 961 | if (FunTmpl->getTemplatedDecl()->getBody(Definition)) |
| 962 | return Definition->isOutOfLine(); |
| 963 | } |
| 964 | |
| 965 | return false; |
| 966 | } |
| 967 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 968 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 969 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 970 | //===----------------------------------------------------------------------===// |
| 971 | |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 972 | SourceRange TagDecl::getSourceRange() const { |
| 973 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 974 | return SourceRange(TagKeywordLoc, E); |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 977 | TagDecl* TagDecl::getCanonicalDecl() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 978 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 981 | void TagDecl::startDefinition() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 982 | if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { |
| 983 | TagT->decl.setPointer(this); |
| 984 | TagT->decl.setInt(1); |
| 985 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | void TagDecl::completeDefinition() { |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 989 | IsDefinition = true; |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 990 | if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { |
| 991 | assert(TagT->decl.getPointer() == this && |
| 992 | "Attempt to redefine a tag definition?"); |
| 993 | TagT->decl.setInt(0); |
| 994 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 997 | TagDecl* TagDecl::getDefinition(ASTContext& C) const { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 998 | if (isDefinition()) |
| 999 | return const_cast<TagDecl *>(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1000 | |
| 1001 | for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1002 | R != REnd; ++R) |
| 1003 | if (R->isDefinition()) |
| 1004 | return *R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1006 | return 0; |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1009 | TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) { |
| 1010 | switch (TypeSpec) { |
| 1011 | default: llvm::llvm_unreachable("unexpected type specifier"); |
| 1012 | case DeclSpec::TST_struct: return TK_struct; |
| 1013 | case DeclSpec::TST_class: return TK_class; |
| 1014 | case DeclSpec::TST_union: return TK_union; |
| 1015 | case DeclSpec::TST_enum: return TK_enum; |
| 1016 | } |
| 1017 | } |
| 1018 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1019 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1020 | // RecordDecl Implementation |
| 1021 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1022 | |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 1023 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1024 | IdentifierInfo *Id, RecordDecl *PrevDecl, |
| 1025 | SourceLocation TKL) |
| 1026 | : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1027 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1028 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1029 | HasObjectMember = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1030 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1034 | SourceLocation L, IdentifierInfo *Id, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 1035 | SourceLocation TKL, RecordDecl* PrevDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1036 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1037 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1038 | C.getTypeDeclType(R, PrevDecl); |
| 1039 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 1042 | RecordDecl::~RecordDecl() { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | void RecordDecl::Destroy(ASTContext& C) { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 1046 | TagDecl::Destroy(C); |
| 1047 | } |
| 1048 | |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 1049 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 1051 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 1052 | } |
| 1053 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1054 | /// completeDefinition - Notes that the definition of this type is now |
| 1055 | /// complete. |
| 1056 | void RecordDecl::completeDefinition(ASTContext& C) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1057 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1058 | TagDecl::completeDefinition(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1061 | //===----------------------------------------------------------------------===// |
| 1062 | // BlockDecl Implementation |
| 1063 | //===----------------------------------------------------------------------===// |
| 1064 | |
| 1065 | BlockDecl::~BlockDecl() { |
| 1066 | } |
| 1067 | |
| 1068 | void BlockDecl::Destroy(ASTContext& C) { |
| 1069 | if (Body) |
| 1070 | Body->Destroy(C); |
| 1071 | |
| 1072 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 1073 | (*I)->Destroy(C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1074 | |
| 1075 | C.Deallocate(ParamInfo); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1076 | Decl::Destroy(C); |
| 1077 | } |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1078 | |
| 1079 | void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 1080 | unsigned NParms) { |
| 1081 | assert(ParamInfo == 0 && "Already has param info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1082 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1083 | // Zero params -> null pointer. |
| 1084 | if (NParms) { |
| 1085 | NumParams = NParms; |
| 1086 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
| 1087 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
| 1088 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | unsigned BlockDecl::getNumParams() const { |
| 1093 | return NumParams; |
| 1094 | } |