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. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 41 | TypeLoc TypeSourceInfo::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, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 89 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 90 | StorageClass S, Expr *DefArg) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 91 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, 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, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 139 | TypeSourceInfo *TInfo, |
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 |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 143 | = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, 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, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 154 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable) { |
| 155 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, 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, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 182 | TypeSourceInfo *TInfo) { |
| 183 | return new (C) TypedefDecl(DC, L, Id, TInfo); |
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 | |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 198 | void EnumDecl::completeDefinition(ASTContext &C, |
| 199 | QualType NewType, |
| 200 | QualType NewPromotionType) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 201 | assert(!isDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 202 | IntegerType = NewType; |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 203 | PromotionType = NewPromotionType; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 204 | TagDecl::completeDefinition(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 207 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 208 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 209 | StringLiteral *Str) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 210 | return new (C) FileScopeAsmDecl(DC, L, Str); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 213 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 214 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 215 | //===----------------------------------------------------------------------===// |
| 216 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 217 | static NamedDecl::Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) { |
| 218 | assert(D->getDeclContext()->getLookupContext()->isFileContext() && |
| 219 | "Not a name having namespace scope"); |
| 220 | ASTContext &Context = D->getASTContext(); |
| 221 | |
| 222 | // C++ [basic.link]p3: |
| 223 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 224 | // is the name of |
| 225 | // - an object, reference, function or function template that is |
| 226 | // explicitly declared static; or, |
| 227 | // (This bullet corresponds to C99 6.2.2p3.) |
| 228 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 229 | // Explicitly declared static. |
| 230 | if (Var->getStorageClass() == VarDecl::Static) |
| 231 | return NamedDecl::InternalLinkage; |
| 232 | |
| 233 | // - an object or reference that is explicitly declared const |
| 234 | // and neither explicitly declared extern nor previously |
| 235 | // declared to have external linkage; or |
| 236 | // (there is no equivalent in C99) |
| 237 | if (Context.getLangOptions().CPlusPlus && |
Eli Friedman | e9d6554 | 2009-11-26 03:04:01 +0000 | [diff] [blame] | 238 | Var->getType().isConstant(Context) && |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 239 | Var->getStorageClass() != VarDecl::Extern && |
| 240 | Var->getStorageClass() != VarDecl::PrivateExtern) { |
| 241 | bool FoundExtern = false; |
| 242 | for (const VarDecl *PrevVar = Var->getPreviousDeclaration(); |
| 243 | PrevVar && !FoundExtern; |
| 244 | PrevVar = PrevVar->getPreviousDeclaration()) |
| 245 | if (PrevVar->getLinkage() == NamedDecl::ExternalLinkage) |
| 246 | FoundExtern = true; |
| 247 | |
| 248 | if (!FoundExtern) |
| 249 | return NamedDecl::InternalLinkage; |
| 250 | } |
| 251 | } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { |
| 252 | const FunctionDecl *Function = 0; |
| 253 | if (const FunctionTemplateDecl *FunTmpl |
| 254 | = dyn_cast<FunctionTemplateDecl>(D)) |
| 255 | Function = FunTmpl->getTemplatedDecl(); |
| 256 | else |
| 257 | Function = cast<FunctionDecl>(D); |
| 258 | |
| 259 | // Explicitly declared static. |
| 260 | if (Function->getStorageClass() == FunctionDecl::Static) |
| 261 | return NamedDecl::InternalLinkage; |
| 262 | } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) { |
| 263 | // - a data member of an anonymous union. |
| 264 | if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()) |
| 265 | return NamedDecl::InternalLinkage; |
| 266 | } |
| 267 | |
| 268 | // C++ [basic.link]p4: |
| 269 | |
| 270 | // A name having namespace scope has external linkage if it is the |
| 271 | // name of |
| 272 | // |
| 273 | // - an object or reference, unless it has internal linkage; or |
| 274 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 275 | if (!Context.getLangOptions().CPlusPlus && |
| 276 | (Var->getStorageClass() == VarDecl::Extern || |
| 277 | Var->getStorageClass() == VarDecl::PrivateExtern)) { |
| 278 | // C99 6.2.2p4: |
| 279 | // For an identifier declared with the storage-class specifier |
| 280 | // extern in a scope in which a prior declaration of that |
| 281 | // identifier is visible, if the prior declaration specifies |
| 282 | // internal or external linkage, the linkage of the identifier |
| 283 | // at the later declaration is the same as the linkage |
| 284 | // specified at the prior declaration. If no prior declaration |
| 285 | // is visible, or if the prior declaration specifies no |
| 286 | // linkage, then the identifier has external linkage. |
| 287 | if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) { |
| 288 | if (NamedDecl::Linkage L = PrevVar->getLinkage()) |
| 289 | return L; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // C99 6.2.2p5: |
| 294 | // If the declaration of an identifier for an object has file |
| 295 | // scope and no storage-class specifier, its linkage is |
| 296 | // external. |
| 297 | return NamedDecl::ExternalLinkage; |
| 298 | } |
| 299 | |
| 300 | // - a function, unless it has internal linkage; or |
| 301 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 302 | // C99 6.2.2p5: |
| 303 | // If the declaration of an identifier for a function has no |
| 304 | // storage-class specifier, its linkage is determined exactly |
| 305 | // as if it were declared with the storage-class specifier |
| 306 | // extern. |
| 307 | if (!Context.getLangOptions().CPlusPlus && |
| 308 | (Function->getStorageClass() == FunctionDecl::Extern || |
| 309 | Function->getStorageClass() == FunctionDecl::PrivateExtern || |
| 310 | Function->getStorageClass() == FunctionDecl::None)) { |
| 311 | // C99 6.2.2p4: |
| 312 | // For an identifier declared with the storage-class specifier |
| 313 | // extern in a scope in which a prior declaration of that |
| 314 | // identifier is visible, if the prior declaration specifies |
| 315 | // internal or external linkage, the linkage of the identifier |
| 316 | // at the later declaration is the same as the linkage |
| 317 | // specified at the prior declaration. If no prior declaration |
| 318 | // is visible, or if the prior declaration specifies no |
| 319 | // linkage, then the identifier has external linkage. |
| 320 | if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) { |
| 321 | if (NamedDecl::Linkage L = PrevFunc->getLinkage()) |
| 322 | return L; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return NamedDecl::ExternalLinkage; |
| 327 | } |
| 328 | |
| 329 | // - a named class (Clause 9), or an unnamed class defined in a |
| 330 | // typedef declaration in which the class has the typedef name |
| 331 | // for linkage purposes (7.1.3); or |
| 332 | // - a named enumeration (7.2), or an unnamed enumeration |
| 333 | // defined in a typedef declaration in which the enumeration |
| 334 | // has the typedef name for linkage purposes (7.1.3); or |
| 335 | if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) |
| 336 | if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) |
| 337 | return NamedDecl::ExternalLinkage; |
| 338 | |
| 339 | // - an enumerator belonging to an enumeration with external linkage; |
| 340 | if (isa<EnumConstantDecl>(D)) |
| 341 | if (cast<NamedDecl>(D->getDeclContext())->getLinkage() |
| 342 | == NamedDecl::ExternalLinkage) |
| 343 | return NamedDecl::ExternalLinkage; |
| 344 | |
| 345 | // - a template, unless it is a function template that has |
| 346 | // internal linkage (Clause 14); |
| 347 | if (isa<TemplateDecl>(D)) |
| 348 | return NamedDecl::ExternalLinkage; |
| 349 | |
| 350 | // - a namespace (7.3), unless it is declared within an unnamed |
| 351 | // namespace. |
| 352 | if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) |
| 353 | return NamedDecl::ExternalLinkage; |
| 354 | |
| 355 | return NamedDecl::NoLinkage; |
| 356 | } |
| 357 | |
| 358 | NamedDecl::Linkage NamedDecl::getLinkage() const { |
| 359 | // Handle linkage for namespace-scope names. |
| 360 | if (getDeclContext()->getLookupContext()->isFileContext()) |
| 361 | if (Linkage L = getLinkageForNamespaceScopeDecl(this)) |
| 362 | return L; |
| 363 | |
| 364 | // C++ [basic.link]p5: |
| 365 | // In addition, a member function, static data member, a named |
| 366 | // class or enumeration of class scope, or an unnamed class or |
| 367 | // enumeration defined in a class-scope typedef declaration such |
| 368 | // that the class or enumeration has the typedef name for linkage |
| 369 | // purposes (7.1.3), has external linkage if the name of the class |
| 370 | // has external linkage. |
| 371 | if (getDeclContext()->isRecord() && |
| 372 | (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) || |
| 373 | (isa<TagDecl>(this) && |
| 374 | (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl()))) && |
| 375 | cast<RecordDecl>(getDeclContext())->getLinkage() == ExternalLinkage) |
| 376 | return ExternalLinkage; |
| 377 | |
| 378 | // C++ [basic.link]p6: |
| 379 | // The name of a function declared in block scope and the name of |
| 380 | // an object declared by a block scope extern declaration have |
| 381 | // linkage. If there is a visible declaration of an entity with |
| 382 | // linkage having the same name and type, ignoring entities |
| 383 | // declared outside the innermost enclosing namespace scope, the |
| 384 | // block scope declaration declares that same entity and receives |
| 385 | // the linkage of the previous declaration. If there is more than |
| 386 | // one such matching entity, the program is ill-formed. Otherwise, |
| 387 | // if no matching entity is found, the block scope entity receives |
| 388 | // external linkage. |
| 389 | if (getLexicalDeclContext()->isFunctionOrMethod()) { |
| 390 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) { |
| 391 | if (Function->getPreviousDeclaration()) |
| 392 | if (Linkage L = Function->getPreviousDeclaration()->getLinkage()) |
| 393 | return L; |
| 394 | |
| 395 | return ExternalLinkage; |
| 396 | } |
| 397 | |
| 398 | if (const VarDecl *Var = dyn_cast<VarDecl>(this)) |
| 399 | if (Var->getStorageClass() == VarDecl::Extern || |
| 400 | Var->getStorageClass() == VarDecl::PrivateExtern) { |
| 401 | if (Var->getPreviousDeclaration()) |
| 402 | if (Linkage L = Var->getPreviousDeclaration()->getLinkage()) |
| 403 | return L; |
| 404 | |
| 405 | return ExternalLinkage; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // C++ [basic.link]p6: |
| 410 | // Names not covered by these rules have no linkage. |
| 411 | return NoLinkage; |
| 412 | } |
| 413 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 414 | std::string NamedDecl::getQualifiedNameAsString() const { |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 415 | return getQualifiedNameAsString(getASTContext().getLangOptions()); |
| 416 | } |
| 417 | |
| 418 | std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 419 | // FIXME: Collect contexts, then accumulate names to avoid unnecessary |
| 420 | // std::string thrashing. |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 421 | std::vector<std::string> Names; |
| 422 | std::string QualName; |
| 423 | const DeclContext *Ctx = getDeclContext(); |
| 424 | |
| 425 | if (Ctx->isFunctionOrMethod()) |
| 426 | return getNameAsString(); |
| 427 | |
| 428 | while (Ctx) { |
| 429 | if (Ctx->isFunctionOrMethod()) |
| 430 | // FIXME: That probably will happen, when D was member of local |
| 431 | // scope class/struct/union. How do we handle this case? |
| 432 | break; |
| 433 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | if (const ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 435 | = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) { |
| 436 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 437 | std::string TemplateArgsStr |
| 438 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 439 | TemplateArgs.getFlatArgumentList(), |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 440 | TemplateArgs.flat_size(), |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 441 | P); |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 442 | Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 443 | } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx)) |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 444 | Names.push_back(ND->getNameAsString()); |
| 445 | else |
| 446 | break; |
| 447 | |
| 448 | Ctx = Ctx->getParent(); |
| 449 | } |
| 450 | |
| 451 | std::vector<std::string>::reverse_iterator |
| 452 | I = Names.rbegin(), |
| 453 | End = Names.rend(); |
| 454 | |
| 455 | for (; I!=End; ++I) |
| 456 | QualName += *I + "::"; |
| 457 | |
| 458 | QualName += getNameAsString(); |
| 459 | |
| 460 | return QualName; |
| 461 | } |
| 462 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 463 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 464 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 465 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 466 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 467 | // We want to keep it, unless it nominates same namespace. |
| 468 | if (getKind() == Decl::UsingDirective) { |
| 469 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == |
| 470 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); |
| 471 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 473 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 474 | // For function declarations, we keep track of redeclarations. |
| 475 | return FD->getPreviousDeclaration() == OldD; |
| 476 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 477 | // For function templates, the underlying function declarations are linked. |
| 478 | if (const FunctionTemplateDecl *FunctionTemplate |
| 479 | = dyn_cast<FunctionTemplateDecl>(this)) |
| 480 | if (const FunctionTemplateDecl *OldFunctionTemplate |
| 481 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 482 | return FunctionTemplate->getTemplatedDecl() |
| 483 | ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 485 | // For method declarations, we keep track of redeclarations. |
| 486 | if (isa<ObjCMethodDecl>(this)) |
| 487 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 489 | if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) |
| 490 | return true; |
| 491 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 492 | if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) |
| 493 | return cast<UsingShadowDecl>(this)->getTargetDecl() == |
| 494 | cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 495 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 496 | // For non-function declarations, if the declarations are of the |
| 497 | // same kind then this must be a redeclaration, or semantic analysis |
| 498 | // would not have given us the new declaration. |
| 499 | return this->getKind() == OldD->getKind(); |
| 500 | } |
| 501 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 502 | bool NamedDecl::hasLinkage() const { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 503 | return getLinkage() != NoLinkage; |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 504 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 505 | |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 506 | NamedDecl *NamedDecl::getUnderlyingDecl() { |
| 507 | NamedDecl *ND = this; |
| 508 | while (true) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 509 | if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 510 | ND = UD->getTargetDecl(); |
| 511 | else if (ObjCCompatibleAliasDecl *AD |
| 512 | = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 513 | return AD->getClassInterface(); |
| 514 | else |
| 515 | return ND; |
| 516 | } |
| 517 | } |
| 518 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 519 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 520 | // DeclaratorDecl Implementation |
| 521 | //===----------------------------------------------------------------------===// |
| 522 | |
| 523 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 524 | if (DeclInfo) { |
| 525 | TypeLoc TL = DeclInfo->getTypeLoc(); |
| 526 | while (true) { |
| 527 | TypeLoc NextTL = TL.getNextTypeLoc(); |
| 528 | if (!NextTL) |
| 529 | return TL.getSourceRange().getBegin(); |
| 530 | TL = NextTL; |
| 531 | } |
| 532 | } |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 533 | return SourceLocation(); |
| 534 | } |
| 535 | |
| 536 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 537 | // VarDecl Implementation |
| 538 | //===----------------------------------------------------------------------===// |
| 539 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 540 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 541 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 542 | StorageClass S) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 543 | return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | void VarDecl::Destroy(ASTContext& C) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 547 | Expr *Init = getInit(); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 548 | if (Init) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 549 | Init->Destroy(C); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 550 | if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) { |
| 551 | Eval->~EvaluatedStmt(); |
| 552 | C.Deallocate(Eval); |
| 553 | } |
| 554 | } |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 555 | this->~VarDecl(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 556 | C.Deallocate((void *)this); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | VarDecl::~VarDecl() { |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 562 | SourceRange VarDecl::getSourceRange() const { |
| 563 | if (getInit()) |
| 564 | return SourceRange(getLocation(), getInit()->getLocEnd()); |
| 565 | return SourceRange(getLocation(), getLocation()); |
| 566 | } |
| 567 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 568 | bool VarDecl::isOutOfLine() const { |
| 569 | if (!isStaticDataMember()) |
| 570 | return false; |
| 571 | |
| 572 | if (Decl::isOutOfLine()) |
| 573 | return true; |
| 574 | |
| 575 | // If this static data member was instantiated from a static data member of |
| 576 | // a class template, check whether that static data member was defined |
| 577 | // out-of-line. |
| 578 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 579 | return VD->isOutOfLine(); |
| 580 | |
| 581 | return false; |
| 582 | } |
| 583 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 584 | VarDecl *VarDecl::getOutOfLineDefinition() { |
| 585 | if (!isStaticDataMember()) |
| 586 | return 0; |
| 587 | |
| 588 | for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); |
| 589 | RD != RDEnd; ++RD) { |
| 590 | if (RD->getLexicalDeclContext()->isFileContext()) |
| 591 | return *RD; |
| 592 | } |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 597 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 598 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 599 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 604 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 605 | if (MemberSpecializationInfo *MSI |
| 606 | = getASTContext().getInstantiatedFromStaticDataMember(this)) |
| 607 | return MSI->getTemplateSpecializationKind(); |
| 608 | |
| 609 | return TSK_Undeclared; |
| 610 | } |
| 611 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 612 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 613 | return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 614 | } |
| 615 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 616 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 617 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 618 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 619 | assert(MSI && "Not an instantiated static data member?"); |
| 620 | MSI->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 621 | if (TSK != TSK_ExplicitSpecialization && |
| 622 | PointOfInstantiation.isValid() && |
| 623 | MSI->getPointOfInstantiation().isInvalid()) |
| 624 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 627 | bool VarDecl::isTentativeDefinition(ASTContext &Context) const { |
| 628 | if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus) |
| 629 | return false; |
| 630 | |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 631 | const VarDecl *Def = 0; |
| 632 | return (!getDefinition(Def) && |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 633 | (getStorageClass() == None || getStorageClass() == Static)); |
| 634 | } |
| 635 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 636 | const Expr *VarDecl::getDefinition(const VarDecl *&Def) const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 637 | redecl_iterator I = redecls_begin(), E = redecls_end(); |
| 638 | while (I != E && !I->getInit()) |
| 639 | ++I; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 640 | |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 641 | if (I != E) { |
| 642 | Def = *I; |
| 643 | return I->getInit(); |
| 644 | } |
| 645 | return 0; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 648 | VarDecl *VarDecl::getCanonicalDecl() { |
Argyrios Kyrtzidis | f23e839 | 2009-07-18 08:50:13 +0000 | [diff] [blame] | 649 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 652 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 653 | // FunctionDecl Implementation |
| 654 | //===----------------------------------------------------------------------===// |
| 655 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 656 | void FunctionDecl::Destroy(ASTContext& C) { |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 657 | if (Body && Body.isOffset()) |
| 658 | Body.get(C.getExternalSource())->Destroy(C); |
Ted Kremenek | b65cf41 | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 659 | |
| 660 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 661 | (*I)->Destroy(C); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 662 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 663 | FunctionTemplateSpecializationInfo *FTSInfo |
| 664 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
| 665 | if (FTSInfo) |
| 666 | C.Deallocate(FTSInfo); |
| 667 | |
| 668 | MemberSpecializationInfo *MSInfo |
| 669 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 670 | if (MSInfo) |
| 671 | C.Deallocate(MSInfo); |
| 672 | |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 673 | C.Deallocate(ParamInfo); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 674 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 675 | Decl::Destroy(C); |
| 676 | } |
| 677 | |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 678 | void FunctionDecl::getNameForDiagnostic(std::string &S, |
| 679 | const PrintingPolicy &Policy, |
| 680 | bool Qualified) const { |
| 681 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 682 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 683 | if (TemplateArgs) |
| 684 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 685 | TemplateArgs->getFlatArgumentList(), |
| 686 | TemplateArgs->flat_size(), |
| 687 | Policy); |
| 688 | |
| 689 | } |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 690 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 691 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 692 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 693 | if (I->Body) { |
| 694 | Definition = *I; |
| 695 | return I->Body.get(getASTContext().getExternalSource()); |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 702 | void FunctionDecl::setBody(Stmt *B) { |
| 703 | Body = B; |
Argyrios Kyrtzidis | 1a5364e | 2009-06-22 17:13:31 +0000 | [diff] [blame] | 704 | if (B) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 705 | EndRangeLoc = B->getLocEnd(); |
| 706 | } |
| 707 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 708 | bool FunctionDecl::isMain() const { |
| 709 | ASTContext &Context = getASTContext(); |
John McCall | 07a5c22 | 2009-08-15 02:09:25 +0000 | [diff] [blame] | 710 | return !Context.getLangOptions().Freestanding && |
| 711 | getDeclContext()->getLookupContext()->isTranslationUnit() && |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 712 | getIdentifier() && getIdentifier()->isStr("main"); |
| 713 | } |
| 714 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 715 | bool FunctionDecl::isExternC() const { |
| 716 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 717 | // In C, any non-static, non-overloadable function has external |
| 718 | // linkage. |
| 719 | if (!Context.getLangOptions().CPlusPlus) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 720 | return getStorageClass() != Static && !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 721 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 723 | DC = DC->getParent()) { |
| 724 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 725 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | return getStorageClass() != Static && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 727 | !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 728 | |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | return false; |
| 734 | } |
| 735 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 736 | bool FunctionDecl::isGlobal() const { |
| 737 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 738 | return Method->isStatic(); |
| 739 | |
| 740 | if (getStorageClass() == Static) |
| 741 | return false; |
| 742 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 744 | DC->isNamespace(); |
| 745 | DC = DC->getParent()) { |
| 746 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 747 | if (!Namespace->getDeclName()) |
| 748 | return false; |
| 749 | break; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | return true; |
| 754 | } |
| 755 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 756 | /// \brief Returns a value indicating whether this function |
| 757 | /// corresponds to a builtin function. |
| 758 | /// |
| 759 | /// The function corresponds to a built-in function if it is |
| 760 | /// declared at translation scope or within an extern "C" block and |
| 761 | /// its name matches with the name of a builtin. The returned value |
| 762 | /// 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] | 763 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 764 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 765 | unsigned FunctionDecl::getBuiltinID() const { |
| 766 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 767 | if (!getIdentifier() || !getIdentifier()->getBuiltinID()) |
| 768 | return 0; |
| 769 | |
| 770 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| 771 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 772 | return BuiltinID; |
| 773 | |
| 774 | // This function has the name of a known C library |
| 775 | // function. Determine whether it actually refers to the C library |
| 776 | // function or whether it just has the same name. |
| 777 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 778 | // If this is a static function, it's not a builtin. |
| 779 | if (getStorageClass() == Static) |
| 780 | return 0; |
| 781 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 782 | // If this function is at translation-unit scope and we're not in |
| 783 | // C++, it refers to the C library function. |
| 784 | if (!Context.getLangOptions().CPlusPlus && |
| 785 | getDeclContext()->isTranslationUnit()) |
| 786 | return BuiltinID; |
| 787 | |
| 788 | // If the function is in an extern "C" linkage specification and is |
| 789 | // not marked "overloadable", it's the real function. |
| 790 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 791 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 792 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 793 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 794 | return BuiltinID; |
| 795 | |
| 796 | // Not a builtin |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 801 | /// getNumParams - Return the number of parameters this function must have |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 802 | /// based on its FunctionType. This is the length of the PararmInfo array |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 803 | /// after it has been created. |
| 804 | unsigned FunctionDecl::getNumParams() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 805 | const FunctionType *FT = getType()->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 806 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 807 | return 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 808 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 812 | void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 813 | unsigned NumParams) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 814 | assert(ParamInfo == 0 && "Already has param info!"); |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 815 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 817 | // Zero params -> null pointer. |
| 818 | if (NumParams) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 819 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 820 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 821 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 822 | |
Argyrios Kyrtzidis | 96888cc | 2009-06-23 00:42:00 +0000 | [diff] [blame] | 823 | // Update source range. The check below allows us to set EndRangeLoc before |
| 824 | // setting the parameters. |
Argyrios Kyrtzidis | cb5f8f5 | 2009-06-23 00:42:15 +0000 | [diff] [blame] | 825 | if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 826 | EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 830 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 831 | /// needed to call this function. This may be fewer than the number of |
| 832 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 833 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 834 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 835 | unsigned NumRequiredArgs = getNumParams(); |
| 836 | while (NumRequiredArgs > 0 |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 837 | && getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 838 | --NumRequiredArgs; |
| 839 | |
| 840 | return NumRequiredArgs; |
| 841 | } |
| 842 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 843 | bool FunctionDecl::isInlined() const { |
Anders Carlsson | 48eda2c | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 844 | // FIXME: This is not enough. Consider: |
| 845 | // |
| 846 | // inline void f(); |
| 847 | // void f() { } |
| 848 | // |
| 849 | // f is inlined, but does not have inline specified. |
| 850 | // To fix this we should add an 'inline' flag to FunctionDecl. |
| 851 | if (isInlineSpecified()) |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 852 | return true; |
Anders Carlsson | 48eda2c | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 853 | |
| 854 | if (isa<CXXMethodDecl>(this)) { |
| 855 | if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified()) |
| 856 | return true; |
| 857 | } |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 858 | |
| 859 | switch (getTemplateSpecializationKind()) { |
| 860 | case TSK_Undeclared: |
| 861 | case TSK_ExplicitSpecialization: |
| 862 | return false; |
| 863 | |
| 864 | case TSK_ImplicitInstantiation: |
| 865 | case TSK_ExplicitInstantiationDeclaration: |
| 866 | case TSK_ExplicitInstantiationDefinition: |
| 867 | // Handle below. |
| 868 | break; |
| 869 | } |
| 870 | |
| 871 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
| 872 | Stmt *Pattern = 0; |
| 873 | if (PatternDecl) |
| 874 | Pattern = PatternDecl->getBody(PatternDecl); |
| 875 | |
| 876 | if (Pattern && PatternDecl) |
| 877 | return PatternDecl->isInlined(); |
| 878 | |
| 879 | return false; |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 882 | /// \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] | 883 | /// definition will be externally visible. |
| 884 | /// |
| 885 | /// Inline function definitions are always available for inlining optimizations. |
| 886 | /// However, depending on the language dialect, declaration specifiers, and |
| 887 | /// attributes, the definition of an inline function may or may not be |
| 888 | /// "externally" visible to other translation units in the program. |
| 889 | /// |
| 890 | /// In C99, inline definitions are not externally visible by default. However, |
| 891 | /// if even one of the globa-scope declarations is marked "extern inline", the |
| 892 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 893 | /// |
| 894 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 895 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 896 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 897 | /// an externally visible symbol, but "extern inline" will not create an |
| 898 | /// externally visible symbol. |
| 899 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
| 900 | assert(isThisDeclarationADefinition() && "Must have the function definition"); |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 901 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 902 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 903 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 904 | if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) { |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 905 | // GNU inline semantics. Based on a number of examples, we came up with the |
| 906 | // following heuristic: if the "inline" keyword is present on a |
| 907 | // declaration of the function but "extern" is not present on that |
| 908 | // declaration, then the symbol is externally visible. Otherwise, the GNU |
| 909 | // "extern inline" semantics applies and the symbol is not externally |
| 910 | // visible. |
| 911 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 912 | Redecl != RedeclEnd; |
| 913 | ++Redecl) { |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 914 | if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 915 | return true; |
| 916 | } |
| 917 | |
| 918 | // GNU "extern inline" semantics; no externally visible symbol. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 919 | return false; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | // C99 6.7.4p6: |
| 923 | // [...] If all of the file scope declarations for a function in a |
| 924 | // translation unit include the inline function specifier without extern, |
| 925 | // then the definition in that translation unit is an inline definition. |
| 926 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 927 | Redecl != RedeclEnd; |
| 928 | ++Redecl) { |
| 929 | // Only consider file-scope declarations in this test. |
| 930 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 931 | continue; |
| 932 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 933 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 934 | return true; // Not an inline definition |
| 935 | } |
| 936 | |
| 937 | // C99 6.7.4p6: |
| 938 | // An inline definition does not provide an external definition for the |
| 939 | // function, and does not forbid an external definition in another |
| 940 | // translation unit. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 941 | return false; |
| 942 | } |
| 943 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 944 | void |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 945 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 946 | redeclarable_base::setPreviousDeclaration(PrevDecl); |
Argyrios Kyrtzidis | f23e839 | 2009-07-18 08:50:13 +0000 | [diff] [blame] | 947 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 948 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | FunctionTemplateDecl *PrevFunTmpl |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 950 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 951 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 952 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 953 | } |
| 954 | } |
| 955 | |
Mike Stump | 740256b | 2009-09-29 00:50:50 +0000 | [diff] [blame] | 956 | const FunctionDecl *FunctionDecl::getCanonicalDecl() const { |
| 957 | return getFirstDeclaration(); |
| 958 | } |
| 959 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 960 | FunctionDecl *FunctionDecl::getCanonicalDecl() { |
Argyrios Kyrtzidis | f23e839 | 2009-07-18 08:50:13 +0000 | [diff] [blame] | 961 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 964 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 965 | /// function represents, if any. |
| 966 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 967 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 968 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 969 | else |
| 970 | return OO_None; |
| 971 | } |
| 972 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 973 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 974 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 975 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| 976 | |
| 977 | return 0; |
| 978 | } |
| 979 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 980 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 981 | return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 982 | } |
| 983 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 984 | void |
| 985 | FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD, |
| 986 | TemplateSpecializationKind TSK) { |
| 987 | assert(TemplateOrSpecialization.isNull() && |
| 988 | "Member function is already a specialization"); |
| 989 | MemberSpecializationInfo *Info |
| 990 | = new (getASTContext()) MemberSpecializationInfo(FD, TSK); |
| 991 | TemplateOrSpecialization = Info; |
| 992 | } |
| 993 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 994 | bool FunctionDecl::isImplicitlyInstantiable() const { |
| 995 | // If this function already has a definition or is invalid, it can't be |
| 996 | // implicitly instantiated. |
| 997 | if (isInvalidDecl() || getBody()) |
| 998 | return false; |
| 999 | |
| 1000 | switch (getTemplateSpecializationKind()) { |
| 1001 | case TSK_Undeclared: |
| 1002 | case TSK_ExplicitSpecialization: |
| 1003 | case TSK_ExplicitInstantiationDefinition: |
| 1004 | return false; |
| 1005 | |
| 1006 | case TSK_ImplicitInstantiation: |
| 1007 | return true; |
| 1008 | |
| 1009 | case TSK_ExplicitInstantiationDeclaration: |
| 1010 | // Handled below. |
| 1011 | break; |
| 1012 | } |
| 1013 | |
| 1014 | // Find the actual template from which we will instantiate. |
| 1015 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
| 1016 | Stmt *Pattern = 0; |
| 1017 | if (PatternDecl) |
| 1018 | Pattern = PatternDecl->getBody(PatternDecl); |
| 1019 | |
| 1020 | // C++0x [temp.explicit]p9: |
| 1021 | // Except for inline functions, other explicit instantiation declarations |
| 1022 | // have the effect of suppressing the implicit instantiation of the entity |
| 1023 | // to which they refer. |
| 1024 | if (!Pattern || !PatternDecl) |
| 1025 | return true; |
| 1026 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1027 | return PatternDecl->isInlined(); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
| 1031 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 1032 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 1033 | // If we have hit a point where the user provided a specialization of |
| 1034 | // this template, we're done looking. |
| 1035 | if (Primary->isMemberSpecialization()) |
| 1036 | break; |
| 1037 | |
| 1038 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 1039 | } |
| 1040 | |
| 1041 | return Primary->getTemplatedDecl(); |
| 1042 | } |
| 1043 | |
| 1044 | return getInstantiatedFromMemberFunction(); |
| 1045 | } |
| 1046 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1047 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1049 | = TemplateOrSpecialization |
| 1050 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1051 | return Info->Template.getPointer(); |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1052 | } |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | const TemplateArgumentList * |
| 1057 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1059 | = TemplateOrSpecialization |
| 1060 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1061 | return Info->TemplateArguments; |
| 1062 | } |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | void |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1067 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context, |
| 1068 | FunctionTemplateDecl *Template, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1069 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1070 | void *InsertPos, |
| 1071 | TemplateSpecializationKind TSK) { |
| 1072 | assert(TSK != TSK_Undeclared && |
| 1073 | "Must specify the type of function template specialization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1074 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1075 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1076 | if (!Info) |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1077 | Info = new (Context) FunctionTemplateSpecializationInfo; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1079 | Info->Function = this; |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1080 | Info->Template.setPointer(Template); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1081 | Info->Template.setInt(TSK - 1); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1082 | Info->TemplateArguments = TemplateArgs; |
| 1083 | TemplateOrSpecialization = Info; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1085 | // Insert this function template specialization into the set of known |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1086 | // function template specializations. |
| 1087 | if (InsertPos) |
| 1088 | Template->getSpecializations().InsertNode(Info, InsertPos); |
| 1089 | else { |
| 1090 | // Try to insert the new node. If there is an existing node, remove it |
| 1091 | // first. |
| 1092 | FunctionTemplateSpecializationInfo *Existing |
| 1093 | = Template->getSpecializations().GetOrInsertNode(Info); |
| 1094 | if (Existing) { |
| 1095 | Template->getSpecializations().RemoveNode(Existing); |
| 1096 | Template->getSpecializations().GetOrInsertNode(Info); |
| 1097 | } |
| 1098 | } |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1101 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | // For a function template specialization, query the specialization |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1103 | // information object. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1104 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1105 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1106 | if (FTSInfo) |
| 1107 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1109 | MemberSpecializationInfo *MSInfo |
| 1110 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 1111 | if (MSInfo) |
| 1112 | return MSInfo->getTemplateSpecializationKind(); |
| 1113 | |
| 1114 | return TSK_Undeclared; |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 | void |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1118 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 1119 | SourceLocation PointOfInstantiation) { |
| 1120 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 1121 | = TemplateOrSpecialization.dyn_cast< |
| 1122 | FunctionTemplateSpecializationInfo*>()) { |
| 1123 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 1124 | if (TSK != TSK_ExplicitSpecialization && |
| 1125 | PointOfInstantiation.isValid() && |
| 1126 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 1127 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1128 | } else if (MemberSpecializationInfo *MSInfo |
| 1129 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 1130 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1131 | if (TSK != TSK_ExplicitSpecialization && |
| 1132 | PointOfInstantiation.isValid() && |
| 1133 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 1134 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1135 | } else |
| 1136 | assert(false && "Function cannot have a template specialization kind"); |
| 1137 | } |
| 1138 | |
| 1139 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1140 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 1141 | = TemplateOrSpecialization.dyn_cast< |
| 1142 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1143 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1144 | else if (MemberSpecializationInfo *MSInfo |
| 1145 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1146 | return MSInfo->getPointOfInstantiation(); |
| 1147 | |
| 1148 | return SourceLocation(); |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1151 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1152 | if (Decl::isOutOfLine()) |
| 1153 | return true; |
| 1154 | |
| 1155 | // If this function was instantiated from a member function of a |
| 1156 | // class template, check whether that member function was defined out-of-line. |
| 1157 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 1158 | const FunctionDecl *Definition; |
| 1159 | if (FD->getBody(Definition)) |
| 1160 | return Definition->isOutOfLine(); |
| 1161 | } |
| 1162 | |
| 1163 | // If this function was instantiated from a function template, |
| 1164 | // check whether that function template was defined out-of-line. |
| 1165 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 1166 | const FunctionDecl *Definition; |
| 1167 | if (FunTmpl->getTemplatedDecl()->getBody(Definition)) |
| 1168 | return Definition->isOutOfLine(); |
| 1169 | } |
| 1170 | |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1174 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1175 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1176 | //===----------------------------------------------------------------------===// |
| 1177 | |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 1178 | SourceRange TagDecl::getSourceRange() const { |
| 1179 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 1180 | return SourceRange(TagKeywordLoc, E); |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 1183 | TagDecl* TagDecl::getCanonicalDecl() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1184 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1187 | void TagDecl::startDefinition() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1188 | if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { |
| 1189 | TagT->decl.setPointer(this); |
| 1190 | TagT->decl.setInt(1); |
| 1191 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | void TagDecl::completeDefinition() { |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1195 | IsDefinition = true; |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1196 | if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { |
| 1197 | assert(TagT->decl.getPointer() == this && |
| 1198 | "Attempt to redefine a tag definition?"); |
| 1199 | TagT->decl.setInt(0); |
| 1200 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1203 | TagDecl* TagDecl::getDefinition(ASTContext& C) const { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1204 | if (isDefinition()) |
| 1205 | return const_cast<TagDecl *>(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | |
| 1207 | for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1208 | R != REnd; ++R) |
| 1209 | if (R->isDefinition()) |
| 1210 | return *R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1212 | return 0; |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1215 | TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) { |
| 1216 | switch (TypeSpec) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame^] | 1217 | default: llvm_unreachable("unexpected type specifier"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1218 | case DeclSpec::TST_struct: return TK_struct; |
| 1219 | case DeclSpec::TST_class: return TK_class; |
| 1220 | case DeclSpec::TST_union: return TK_union; |
| 1221 | case DeclSpec::TST_enum: return TK_enum; |
| 1222 | } |
| 1223 | } |
| 1224 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1225 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1226 | // RecordDecl Implementation |
| 1227 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1228 | |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 1229 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1230 | IdentifierInfo *Id, RecordDecl *PrevDecl, |
| 1231 | SourceLocation TKL) |
| 1232 | : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1233 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1234 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1235 | HasObjectMember = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1236 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1240 | SourceLocation L, IdentifierInfo *Id, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 1241 | SourceLocation TKL, RecordDecl* PrevDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1243 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1244 | C.getTypeDeclType(R, PrevDecl); |
| 1245 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 1248 | RecordDecl::~RecordDecl() { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | void RecordDecl::Destroy(ASTContext& C) { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 1252 | TagDecl::Destroy(C); |
| 1253 | } |
| 1254 | |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 1255 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1256 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 1257 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 1258 | } |
| 1259 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1260 | /// completeDefinition - Notes that the definition of this type is now |
| 1261 | /// complete. |
| 1262 | void RecordDecl::completeDefinition(ASTContext& C) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1263 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1264 | TagDecl::completeDefinition(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1267 | //===----------------------------------------------------------------------===// |
| 1268 | // BlockDecl Implementation |
| 1269 | //===----------------------------------------------------------------------===// |
| 1270 | |
| 1271 | BlockDecl::~BlockDecl() { |
| 1272 | } |
| 1273 | |
| 1274 | void BlockDecl::Destroy(ASTContext& C) { |
| 1275 | if (Body) |
| 1276 | Body->Destroy(C); |
| 1277 | |
| 1278 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 1279 | (*I)->Destroy(C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1280 | |
| 1281 | C.Deallocate(ParamInfo); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1282 | Decl::Destroy(C); |
| 1283 | } |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1284 | |
| 1285 | void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 1286 | unsigned NParms) { |
| 1287 | assert(ParamInfo == 0 && "Already has param info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1289 | // Zero params -> null pointer. |
| 1290 | if (NParms) { |
| 1291 | NumParams = NParms; |
| 1292 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
| 1293 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
| 1294 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | unsigned BlockDecl::getNumParams() const { |
| 1299 | return NumParams; |
| 1300 | } |