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