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