Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Decl.cpp - Declaration AST Node Implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Argiris Kirtzidis | e7dfca1 | 2008-06-04 13:04:04 +0000 | [diff] [blame] | 10 | // This file implements the Decl subclasses. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | d85ba92 | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/Stmt.h" |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 21 | #include <vector> |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 26 | // Decl Allocation/Deallocation Method Implementations |
| 27 | //===----------------------------------------------------------------------===// |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 28 | |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 29 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 30 | return new (C) TranslationUnitDecl(); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 33 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 34 | SourceLocation L, IdentifierInfo *Id) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 35 | return new (C) NamespaceDecl(DC, L, Id); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 38 | void NamespaceDecl::Destroy(ASTContext& C) { |
| 39 | // NamespaceDecl uses "NextDeclarator" to chain namespace declarations |
| 40 | // together. They are all top-level Decls. |
| 41 | |
Ted Kremenek | 0f43384 | 2008-05-24 15:09:56 +0000 | [diff] [blame] | 42 | this->~NamespaceDecl(); |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 43 | C.Deallocate((void *)this); |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 47 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 48 | SourceLocation L, IdentifierInfo *Id, QualType T) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 49 | return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 52 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 53 | SourceLocation L, IdentifierInfo *Id, |
| 54 | QualType T, StorageClass S, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 55 | Expr *DefArg) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 56 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg); |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | QualType ParmVarDecl::getOriginalType() const { |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 60 | if (const OriginalParmVarDecl *PVD = |
| 61 | dyn_cast<OriginalParmVarDecl>(this)) |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 62 | return PVD->OriginalType; |
| 63 | return getType(); |
Chris Lattner | 48d225c | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 66 | OriginalParmVarDecl *OriginalParmVarDecl::Create( |
Fariborz Jahanian | 160e881 | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 67 | ASTContext &C, DeclContext *DC, |
| 68 | SourceLocation L, IdentifierInfo *Id, |
| 69 | QualType T, QualType OT, StorageClass S, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 70 | Expr *DefArg) { |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 71 | return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg); |
Fariborz Jahanian | 160e881 | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 74 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 75 | SourceLocation L, |
Douglas Gregor | 6704b31 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 76 | DeclarationName N, QualType T, |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 77 | StorageClass S, bool isInline, |
Steve Naroff | 71cd776 | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 78 | SourceLocation TypeSpecStartLoc) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 79 | return new (C) FunctionDecl(Function, DC, L, N, T, S, isInline, |
Steve Naroff | 71cd776 | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 80 | TypeSpecStartLoc); |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 83 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 84 | return new (C) BlockDecl(DC, L); |
Steve Naroff | 9ac456d | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 87 | FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 88 | IdentifierInfo *Id, QualType T, Expr *BW, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 89 | bool Mutable) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 90 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable); |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Douglas Gregor | c7f0161 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 93 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 94 | if (!isImplicit() || getDeclName()) |
| 95 | return false; |
| 96 | |
| 97 | if (const RecordType *Record = getType()->getAsRecordType()) |
| 98 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 99 | |
| 100 | return false; |
| 101 | } |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 102 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 103 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 104 | SourceLocation L, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 105 | IdentifierInfo *Id, QualType T, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 106 | Expr *E, const llvm::APSInt &V) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 107 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 110 | void EnumConstantDecl::Destroy(ASTContext& C) { |
| 111 | if (Init) Init->Destroy(C); |
| 112 | Decl::Destroy(C); |
| 113 | } |
| 114 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 115 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 116 | SourceLocation L, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 117 | IdentifierInfo *Id, QualType T) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 118 | return new (C) TypedefDecl(DC, L, Id, T); |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 121 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 122 | IdentifierInfo *Id, |
Douglas Gregor | ae64489 | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 123 | EnumDecl *PrevDecl) { |
Steve Naroff | 207b9ec | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 124 | EnumDecl *Enum = new (C) EnumDecl(DC, L, Id); |
Douglas Gregor | ae64489 | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 125 | C.getTypeDeclType(Enum, PrevDecl); |
| 126 | return Enum; |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Ted Kremenek | 25d8be1 | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 129 | void EnumDecl::Destroy(ASTContext& C) { |
Ted Kremenek | 25d8be1 | 2008-09-02 20:13:32 +0000 | [diff] [blame] | 130 | Decl::Destroy(C); |
| 131 | } |
| 132 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 133 | void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) { |
| 134 | assert(!isDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 135 | IntegerType = NewType; |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 136 | TagDecl::completeDefinition(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 139 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 140 | SourceLocation L, |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 141 | StringLiteral *Str) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 142 | return new (C) FileScopeAsmDecl(DC, L, Str); |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 145 | //===----------------------------------------------------------------------===// |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 146 | // NamedDecl Implementation |
Argiris Kirtzidis | 881964b | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 147 | //===----------------------------------------------------------------------===// |
| 148 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 149 | std::string NamedDecl::getQualifiedNameAsString() const { |
| 150 | std::vector<std::string> Names; |
| 151 | std::string QualName; |
| 152 | const DeclContext *Ctx = getDeclContext(); |
| 153 | |
| 154 | if (Ctx->isFunctionOrMethod()) |
| 155 | return getNameAsString(); |
| 156 | |
| 157 | while (Ctx) { |
| 158 | if (Ctx->isFunctionOrMethod()) |
| 159 | // FIXME: That probably will happen, when D was member of local |
| 160 | // scope class/struct/union. How do we handle this case? |
| 161 | break; |
| 162 | |
| 163 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx)) |
| 164 | Names.push_back(ND->getNameAsString()); |
| 165 | else |
| 166 | break; |
| 167 | |
| 168 | Ctx = Ctx->getParent(); |
| 169 | } |
| 170 | |
| 171 | std::vector<std::string>::reverse_iterator |
| 172 | I = Names.rbegin(), |
| 173 | End = Names.rend(); |
| 174 | |
| 175 | for (; I!=End; ++I) |
| 176 | QualName += *I + "::"; |
| 177 | |
| 178 | QualName += getNameAsString(); |
| 179 | |
| 180 | return QualName; |
| 181 | } |
| 182 | |
| 183 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 184 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6e71edc | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 185 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 186 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 187 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 188 | // We want to keep it, unless it nominates same namespace. |
| 189 | if (getKind() == Decl::UsingDirective) { |
| 190 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == |
| 191 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); |
| 192 | } |
| 193 | |
Douglas Gregor | 6e71edc | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 194 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 195 | // For function declarations, we keep track of redeclarations. |
| 196 | return FD->getPreviousDeclaration() == OldD; |
| 197 | |
Steve Naroff | d85ba92 | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 198 | // For method declarations, we keep track of redeclarations. |
| 199 | if (isa<ObjCMethodDecl>(this)) |
| 200 | return false; |
| 201 | |
Douglas Gregor | 6e71edc | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 202 | // For non-function declarations, if the declarations are of the |
| 203 | // same kind then this must be a redeclaration, or semantic analysis |
| 204 | // would not have given us the new declaration. |
| 205 | return this->getKind() == OldD->getKind(); |
| 206 | } |
| 207 | |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame^] | 208 | bool NamedDecl::hasLinkage() const { |
| 209 | if (const VarDecl *VD = dyn_cast<VarDecl>(this)) |
| 210 | return VD->hasExternalStorage() || VD->isFileVarDecl(); |
| 211 | |
| 212 | if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this)) |
| 213 | return true; |
| 214 | |
| 215 | return false; |
| 216 | } |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 217 | |
Argiris Kirtzidis | 881964b | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 218 | //===----------------------------------------------------------------------===// |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 219 | // VarDecl Implementation |
| 220 | //===----------------------------------------------------------------------===// |
| 221 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 222 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 223 | IdentifierInfo *Id, QualType T, StorageClass S, |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 224 | SourceLocation TypeSpecStartLoc) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 225 | return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc); |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void VarDecl::Destroy(ASTContext& C) { |
Sebastian Redl | 2ee5561 | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 229 | Expr *Init = getInit(); |
| 230 | if (Init) |
| 231 | Init->Destroy(C); |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 232 | this->~VarDecl(); |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 233 | C.Deallocate((void *)this); |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | VarDecl::~VarDecl() { |
Nuno Lopes | c98406e | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | //===----------------------------------------------------------------------===// |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 240 | // FunctionDecl Implementation |
| 241 | //===----------------------------------------------------------------------===// |
| 242 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 243 | void FunctionDecl::Destroy(ASTContext& C) { |
Ted Kremenek | 345b93d | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 244 | if (Body) |
| 245 | Body->Destroy(C); |
| 246 | |
| 247 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 248 | (*I)->Destroy(C); |
Nuno Lopes | cb8cc5b | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 249 | |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 250 | C.Deallocate(ParamInfo); |
Nuno Lopes | cb8cc5b | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 251 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 252 | Decl::Destroy(C); |
| 253 | } |
| 254 | |
| 255 | |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 256 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
| 257 | for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) { |
| 258 | if (FD->Body) { |
| 259 | Definition = FD; |
| 260 | return FD->Body; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Douglas Gregor | af68202 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 267 | bool FunctionDecl::isMain() const { |
| 268 | return getDeclContext()->getLookupContext()->isTranslationUnit() && |
| 269 | getIdentifier() && getIdentifier()->isStr("main"); |
| 270 | } |
| 271 | |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 272 | /// \brief Returns a value indicating whether this function |
| 273 | /// corresponds to a builtin function. |
| 274 | /// |
| 275 | /// The function corresponds to a built-in function if it is |
| 276 | /// declared at translation scope or within an extern "C" block and |
| 277 | /// its name matches with the name of a builtin. The returned value |
| 278 | /// will be 0 for functions that do not correspond to a builtin, a |
| 279 | /// value of type \c Builtin::ID if in the target-independent range |
| 280 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 281 | unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const { |
| 282 | if (!getIdentifier() || !getIdentifier()->getBuiltinID()) |
| 283 | return 0; |
| 284 | |
| 285 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| 286 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 287 | return BuiltinID; |
| 288 | |
| 289 | // This function has the name of a known C library |
| 290 | // function. Determine whether it actually refers to the C library |
| 291 | // function or whether it just has the same name. |
| 292 | |
Douglas Gregor | 4d6b102 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 293 | // If this is a static function, it's not a builtin. |
| 294 | if (getStorageClass() == Static) |
| 295 | return 0; |
| 296 | |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 297 | // If this function is at translation-unit scope and we're not in |
| 298 | // C++, it refers to the C library function. |
| 299 | if (!Context.getLangOptions().CPlusPlus && |
| 300 | getDeclContext()->isTranslationUnit()) |
| 301 | return BuiltinID; |
| 302 | |
| 303 | // If the function is in an extern "C" linkage specification and is |
| 304 | // not marked "overloadable", it's the real function. |
| 305 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
| 306 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
| 307 | == LinkageSpecDecl::lang_c && |
| 308 | !getAttr<OverloadableAttr>()) |
| 309 | return BuiltinID; |
| 310 | |
| 311 | // Not a builtin |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | |
Ted Kremenek | a6ded35 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 316 | // Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams() |
| 317 | static unsigned getNumTypeParams(QualType T) { |
| 318 | const FunctionType *FT = T->getAsFunctionType(); |
Chris Lattner | e873359 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 319 | if (isa<FunctionTypeNoProto>(FT)) |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 320 | return 0; |
Chris Lattner | e873359 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 321 | return cast<FunctionTypeProto>(FT)->getNumArgs(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Ted Kremenek | a6ded35 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 324 | unsigned FunctionDecl::getNumParams() const { |
| 325 | // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar; |
| 326 | if (!ParamInfo) |
| 327 | return 0; |
| 328 | |
| 329 | return getNumTypeParams(getType()); |
| 330 | } |
| 331 | |
Ted Kremenek | 8494c96 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 332 | void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo, |
| 333 | unsigned NumParams) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 334 | assert(ParamInfo == 0 && "Already has param info!"); |
Ted Kremenek | a6ded35 | 2008-10-29 18:41:34 +0000 | [diff] [blame] | 335 | assert(NumParams == getNumTypeParams(getType()) && |
| 336 | "Parameter count mismatch!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 337 | |
| 338 | // Zero params -> null pointer. |
| 339 | if (NumParams) { |
Steve Naroff | 207b9ec | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 340 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | 8494c96 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 341 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 342 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 343 | } |
| 344 | } |
| 345 | |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 346 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 347 | /// needed to call this function. This may be fewer than the number of |
| 348 | /// function parameters, if some of the parameters have default |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 349 | /// arguments (in C++). |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 350 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 351 | unsigned NumRequiredArgs = getNumParams(); |
| 352 | while (NumRequiredArgs > 0 |
| 353 | && getParamDecl(NumRequiredArgs-1)->getDefaultArg()) |
| 354 | --NumRequiredArgs; |
| 355 | |
| 356 | return NumRequiredArgs; |
| 357 | } |
| 358 | |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 359 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 360 | /// function represents, if any. |
| 361 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | 96a32dd | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 362 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 363 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 364 | else |
| 365 | return OO_None; |
| 366 | } |
| 367 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 368 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 369 | // TagDecl Implementation |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 370 | //===----------------------------------------------------------------------===// |
| 371 | |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 372 | void TagDecl::startDefinition() { |
| 373 | cast<TagType>(TypeForDecl)->decl.setPointer(this); |
| 374 | cast<TagType>(TypeForDecl)->decl.setInt(1); |
| 375 | } |
| 376 | |
| 377 | void TagDecl::completeDefinition() { |
| 378 | assert((!TypeForDecl || |
| 379 | cast<TagType>(TypeForDecl)->decl.getPointer() == this) && |
| 380 | "Attempt to redefine a tag definition?"); |
| 381 | IsDefinition = true; |
| 382 | cast<TagType>(TypeForDecl)->decl.setPointer(this); |
| 383 | cast<TagType>(TypeForDecl)->decl.setInt(0); |
| 384 | } |
| 385 | |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 386 | TagDecl* TagDecl::getDefinition(ASTContext& C) const { |
| 387 | QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this)); |
| 388 | TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl()); |
| 389 | return D->isDefinition() ? D : 0; |
| 390 | } |
| 391 | |
| 392 | //===----------------------------------------------------------------------===// |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 393 | // RecordDecl Implementation |
| 394 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 395 | |
Argiris Kirtzidis | 2538a8c | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 396 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 397 | IdentifierInfo *Id) |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 398 | : TagDecl(DK, TK, DC, L, Id) { |
Ted Kremenek | 6f0a241 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 399 | HasFlexibleArrayMember = false; |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 400 | AnonymousStructOrUnion = false; |
Ted Kremenek | 6f0a241 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 401 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6f0a241 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 405 | SourceLocation L, IdentifierInfo *Id, |
| 406 | RecordDecl* PrevDecl) { |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 407 | |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 408 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id); |
Ted Kremenek | 46a837c | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 409 | C.getTypeDeclType(R, PrevDecl); |
| 410 | return R; |
Ted Kremenek | 6f0a241 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Argiris Kirtzidis | d64c111 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 413 | RecordDecl::~RecordDecl() { |
Argiris Kirtzidis | d64c111 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void RecordDecl::Destroy(ASTContext& C) { |
Argiris Kirtzidis | d64c111 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 417 | TagDecl::Destroy(C); |
| 418 | } |
| 419 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 420 | /// completeDefinition - Notes that the definition of this type is now |
| 421 | /// complete. |
| 422 | void RecordDecl::completeDefinition(ASTContext& C) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 423 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 424 | TagDecl::completeDefinition(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Steve Naroff | 9ac456d | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 427 | //===----------------------------------------------------------------------===// |
| 428 | // BlockDecl Implementation |
| 429 | //===----------------------------------------------------------------------===// |
| 430 | |
| 431 | BlockDecl::~BlockDecl() { |
| 432 | } |
| 433 | |
| 434 | void BlockDecl::Destroy(ASTContext& C) { |
| 435 | if (Body) |
| 436 | Body->Destroy(C); |
| 437 | |
| 438 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 439 | (*I)->Destroy(C); |
| 440 | |
| 441 | Decl::Destroy(C); |
| 442 | } |