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