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" |
Anders Carlsson | 337cba4 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 23 | #include "clang/AST/PrettyPrinter.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 24 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 25 | #include "clang/Basic/IdentifierTable.h" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Specifiers.h" |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
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 | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 32 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 35 | /// \brief Get the most restrictive linkage for the types in the given |
| 36 | /// template parameter list. |
| 37 | static Linkage |
| 38 | getLinkageForTemplateParameterList(const TemplateParameterList *Params) { |
| 39 | Linkage L = ExternalLinkage; |
| 40 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 41 | PEnd = Params->end(); |
| 42 | P != PEnd; ++P) { |
| 43 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) |
| 44 | if (!NTTP->getType()->isDependentType()) { |
| 45 | L = minLinkage(L, NTTP->getType()->getLinkage()); |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | if (TemplateTemplateParmDecl *TTP |
| 50 | = dyn_cast<TemplateTemplateParmDecl>(*P)) { |
| 51 | L = minLinkage(L, |
| 52 | getLinkageForTemplateParameterList(TTP->getTemplateParameters())); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return L; |
| 57 | } |
| 58 | |
| 59 | /// \brief Get the most restrictive linkage for the types and |
| 60 | /// declarations in the given template argument list. |
| 61 | static Linkage getLinkageForTemplateArgumentList(const TemplateArgument *Args, |
| 62 | unsigned NumArgs) { |
| 63 | Linkage L = ExternalLinkage; |
| 64 | |
| 65 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 66 | switch (Args[I].getKind()) { |
| 67 | case TemplateArgument::Null: |
| 68 | case TemplateArgument::Integral: |
| 69 | case TemplateArgument::Expression: |
| 70 | break; |
| 71 | |
| 72 | case TemplateArgument::Type: |
| 73 | L = minLinkage(L, Args[I].getAsType()->getLinkage()); |
| 74 | break; |
| 75 | |
| 76 | case TemplateArgument::Declaration: |
| 77 | if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl())) |
| 78 | L = minLinkage(L, ND->getLinkage()); |
| 79 | if (ValueDecl *VD = dyn_cast<ValueDecl>(Args[I].getAsDecl())) |
| 80 | L = minLinkage(L, VD->getType()->getLinkage()); |
| 81 | break; |
| 82 | |
| 83 | case TemplateArgument::Template: |
| 84 | if (TemplateDecl *Template |
| 85 | = Args[I].getAsTemplate().getAsTemplateDecl()) |
| 86 | L = minLinkage(L, Template->getLinkage()); |
| 87 | break; |
| 88 | |
| 89 | case TemplateArgument::Pack: |
| 90 | L = minLinkage(L, |
| 91 | getLinkageForTemplateArgumentList(Args[I].pack_begin(), |
| 92 | Args[I].pack_size())); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return L; |
| 98 | } |
| 99 | |
| 100 | static Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 101 | assert(D->getDeclContext()->getLookupContext()->isFileContext() && |
| 102 | "Not a name having namespace scope"); |
| 103 | ASTContext &Context = D->getASTContext(); |
| 104 | |
| 105 | // C++ [basic.link]p3: |
| 106 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 107 | // is the name of |
| 108 | // - an object, reference, function or function template that is |
| 109 | // explicitly declared static; or, |
| 110 | // (This bullet corresponds to C99 6.2.2p3.) |
| 111 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 112 | // Explicitly declared static. |
| 113 | if (Var->getStorageClass() == VarDecl::Static) |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 114 | return InternalLinkage; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 115 | |
| 116 | // - an object or reference that is explicitly declared const |
| 117 | // and neither explicitly declared extern nor previously |
| 118 | // declared to have external linkage; or |
| 119 | // (there is no equivalent in C99) |
| 120 | if (Context.getLangOptions().CPlusPlus && |
Eli Friedman | e9d6554 | 2009-11-26 03:04:01 +0000 | [diff] [blame] | 121 | Var->getType().isConstant(Context) && |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 122 | Var->getStorageClass() != VarDecl::Extern && |
| 123 | Var->getStorageClass() != VarDecl::PrivateExtern) { |
| 124 | bool FoundExtern = false; |
| 125 | for (const VarDecl *PrevVar = Var->getPreviousDeclaration(); |
| 126 | PrevVar && !FoundExtern; |
| 127 | PrevVar = PrevVar->getPreviousDeclaration()) |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 128 | if (isExternalLinkage(PrevVar->getLinkage())) |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 129 | FoundExtern = true; |
| 130 | |
| 131 | if (!FoundExtern) |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 132 | return InternalLinkage; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 133 | } |
| 134 | } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 135 | // C++ [temp]p4: |
| 136 | // A non-member function template can have internal linkage; any |
| 137 | // other template name shall have external linkage. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 138 | const FunctionDecl *Function = 0; |
| 139 | if (const FunctionTemplateDecl *FunTmpl |
| 140 | = dyn_cast<FunctionTemplateDecl>(D)) |
| 141 | Function = FunTmpl->getTemplatedDecl(); |
| 142 | else |
| 143 | Function = cast<FunctionDecl>(D); |
| 144 | |
| 145 | // Explicitly declared static. |
| 146 | if (Function->getStorageClass() == FunctionDecl::Static) |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 147 | return InternalLinkage; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 148 | } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) { |
| 149 | // - a data member of an anonymous union. |
| 150 | if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()) |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 151 | return InternalLinkage; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // C++ [basic.link]p4: |
| 155 | |
| 156 | // A name having namespace scope has external linkage if it is the |
| 157 | // name of |
| 158 | // |
| 159 | // - an object or reference, unless it has internal linkage; or |
| 160 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 161 | if (!Context.getLangOptions().CPlusPlus && |
| 162 | (Var->getStorageClass() == VarDecl::Extern || |
| 163 | Var->getStorageClass() == VarDecl::PrivateExtern)) { |
| 164 | // C99 6.2.2p4: |
| 165 | // For an identifier declared with the storage-class specifier |
| 166 | // extern in a scope in which a prior declaration of that |
| 167 | // identifier is visible, if the prior declaration specifies |
| 168 | // internal or external linkage, the linkage of the identifier |
| 169 | // at the later declaration is the same as the linkage |
| 170 | // specified at the prior declaration. If no prior declaration |
| 171 | // is visible, or if the prior declaration specifies no |
| 172 | // linkage, then the identifier has external linkage. |
| 173 | if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 174 | if (Linkage L = PrevVar->getLinkage()) |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 175 | return L; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // C99 6.2.2p5: |
| 180 | // If the declaration of an identifier for an object has file |
| 181 | // scope and no storage-class specifier, its linkage is |
| 182 | // external. |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 183 | if (Var->isInAnonymousNamespace()) |
| 184 | return UniqueExternalLinkage; |
| 185 | |
| 186 | return ExternalLinkage; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // - a function, unless it has internal linkage; or |
| 190 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 191 | // C99 6.2.2p5: |
| 192 | // If the declaration of an identifier for a function has no |
| 193 | // storage-class specifier, its linkage is determined exactly |
| 194 | // as if it were declared with the storage-class specifier |
| 195 | // extern. |
| 196 | if (!Context.getLangOptions().CPlusPlus && |
| 197 | (Function->getStorageClass() == FunctionDecl::Extern || |
| 198 | Function->getStorageClass() == FunctionDecl::PrivateExtern || |
| 199 | Function->getStorageClass() == FunctionDecl::None)) { |
| 200 | // C99 6.2.2p4: |
| 201 | // For an identifier declared with the storage-class specifier |
| 202 | // extern in a scope in which a prior declaration of that |
| 203 | // identifier is visible, if the prior declaration specifies |
| 204 | // internal or external linkage, the linkage of the identifier |
| 205 | // at the later declaration is the same as the linkage |
| 206 | // specified at the prior declaration. If no prior declaration |
| 207 | // is visible, or if the prior declaration specifies no |
| 208 | // linkage, then the identifier has external linkage. |
| 209 | if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 210 | if (Linkage L = PrevFunc->getLinkage()) |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 211 | return L; |
| 212 | } |
| 213 | } |
| 214 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 215 | if (Function->isInAnonymousNamespace()) |
| 216 | return UniqueExternalLinkage; |
| 217 | |
| 218 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 219 | = Function->getTemplateSpecializationInfo()) { |
| 220 | Linkage L = SpecInfo->getTemplate()->getLinkage(); |
| 221 | const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments; |
| 222 | L = minLinkage(L, |
| 223 | getLinkageForTemplateArgumentList( |
| 224 | TemplateArgs.getFlatArgumentList(), |
| 225 | TemplateArgs.flat_size())); |
| 226 | return L; |
| 227 | } |
| 228 | |
| 229 | return ExternalLinkage; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // - a named class (Clause 9), or an unnamed class defined in a |
| 233 | // typedef declaration in which the class has the typedef name |
| 234 | // for linkage purposes (7.1.3); or |
| 235 | // - a named enumeration (7.2), or an unnamed enumeration |
| 236 | // defined in a typedef declaration in which the enumeration |
| 237 | // has the typedef name for linkage purposes (7.1.3); or |
| 238 | if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 239 | if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) { |
| 240 | if (Tag->isInAnonymousNamespace()) |
| 241 | return UniqueExternalLinkage; |
| 242 | |
| 243 | // If this is a class template specialization, consider the |
| 244 | // linkage of the template and template arguments. |
| 245 | if (const ClassTemplateSpecializationDecl *Spec |
| 246 | = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
| 247 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 248 | Linkage L = getLinkageForTemplateArgumentList( |
| 249 | TemplateArgs.getFlatArgumentList(), |
| 250 | TemplateArgs.flat_size()); |
| 251 | return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage()); |
| 252 | } |
| 253 | |
| 254 | return ExternalLinkage; |
| 255 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 256 | |
| 257 | // - an enumerator belonging to an enumeration with external linkage; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 258 | if (isa<EnumConstantDecl>(D)) { |
| 259 | Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage(); |
| 260 | if (isExternalLinkage(L)) |
| 261 | return L; |
| 262 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 263 | |
| 264 | // - a template, unless it is a function template that has |
| 265 | // internal linkage (Clause 14); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 266 | if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 267 | if (D->isInAnonymousNamespace()) |
| 268 | return UniqueExternalLinkage; |
| 269 | |
| 270 | return getLinkageForTemplateParameterList( |
| 271 | Template->getTemplateParameters()); |
| 272 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 273 | |
| 274 | // - a namespace (7.3), unless it is declared within an unnamed |
| 275 | // namespace. |
| 276 | if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 277 | return ExternalLinkage; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 278 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 279 | return NoLinkage; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 282 | Linkage NamedDecl::getLinkage() const { |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 283 | |
| 284 | // Objective-C: treat all Objective-C declarations as having external |
| 285 | // linkage. |
| 286 | switch (getKind()) { |
| 287 | default: |
| 288 | break; |
| 289 | case Decl::ObjCAtDefsField: |
| 290 | case Decl::ObjCCategory: |
| 291 | case Decl::ObjCCategoryImpl: |
| 292 | case Decl::ObjCClass: |
| 293 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 294 | case Decl::ObjCForwardProtocol: |
| 295 | case Decl::ObjCImplementation: |
| 296 | case Decl::ObjCInterface: |
| 297 | case Decl::ObjCIvar: |
| 298 | case Decl::ObjCMethod: |
| 299 | case Decl::ObjCProperty: |
| 300 | case Decl::ObjCPropertyImpl: |
| 301 | case Decl::ObjCProtocol: |
| 302 | return ExternalLinkage; |
| 303 | } |
| 304 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 305 | // Handle linkage for namespace-scope names. |
| 306 | if (getDeclContext()->getLookupContext()->isFileContext()) |
| 307 | if (Linkage L = getLinkageForNamespaceScopeDecl(this)) |
| 308 | return L; |
| 309 | |
| 310 | // C++ [basic.link]p5: |
| 311 | // In addition, a member function, static data member, a named |
| 312 | // class or enumeration of class scope, or an unnamed class or |
| 313 | // enumeration defined in a class-scope typedef declaration such |
| 314 | // that the class or enumeration has the typedef name for linkage |
| 315 | // purposes (7.1.3), has external linkage if the name of the class |
| 316 | // has external linkage. |
| 317 | if (getDeclContext()->isRecord() && |
| 318 | (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) || |
| 319 | (isa<TagDecl>(this) && |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 320 | (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) { |
| 321 | Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage(); |
| 322 | if (isExternalLinkage(L)) |
| 323 | return L; |
| 324 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 325 | |
| 326 | // C++ [basic.link]p6: |
| 327 | // The name of a function declared in block scope and the name of |
| 328 | // an object declared by a block scope extern declaration have |
| 329 | // linkage. If there is a visible declaration of an entity with |
| 330 | // linkage having the same name and type, ignoring entities |
| 331 | // declared outside the innermost enclosing namespace scope, the |
| 332 | // block scope declaration declares that same entity and receives |
| 333 | // the linkage of the previous declaration. If there is more than |
| 334 | // one such matching entity, the program is ill-formed. Otherwise, |
| 335 | // if no matching entity is found, the block scope entity receives |
| 336 | // external linkage. |
| 337 | if (getLexicalDeclContext()->isFunctionOrMethod()) { |
| 338 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) { |
| 339 | if (Function->getPreviousDeclaration()) |
| 340 | if (Linkage L = Function->getPreviousDeclaration()->getLinkage()) |
| 341 | return L; |
| 342 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 343 | if (Function->isInAnonymousNamespace()) |
| 344 | return UniqueExternalLinkage; |
| 345 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 346 | return ExternalLinkage; |
| 347 | } |
| 348 | |
| 349 | if (const VarDecl *Var = dyn_cast<VarDecl>(this)) |
| 350 | if (Var->getStorageClass() == VarDecl::Extern || |
| 351 | Var->getStorageClass() == VarDecl::PrivateExtern) { |
| 352 | if (Var->getPreviousDeclaration()) |
| 353 | if (Linkage L = Var->getPreviousDeclaration()->getLinkage()) |
| 354 | return L; |
| 355 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 356 | if (Var->isInAnonymousNamespace()) |
| 357 | return UniqueExternalLinkage; |
| 358 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 359 | return ExternalLinkage; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // C++ [basic.link]p6: |
| 364 | // Names not covered by these rules have no linkage. |
| 365 | return NoLinkage; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 366 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 367 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 368 | std::string NamedDecl::getQualifiedNameAsString() const { |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 369 | return getQualifiedNameAsString(getASTContext().getLangOptions()); |
| 370 | } |
| 371 | |
| 372 | std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 373 | const DeclContext *Ctx = getDeclContext(); |
| 374 | |
| 375 | if (Ctx->isFunctionOrMethod()) |
| 376 | return getNameAsString(); |
| 377 | |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 378 | typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy; |
| 379 | ContextsTy Contexts; |
| 380 | |
| 381 | // Collect contexts. |
| 382 | while (Ctx && isa<NamedDecl>(Ctx)) { |
| 383 | Contexts.push_back(Ctx); |
| 384 | Ctx = Ctx->getParent(); |
| 385 | }; |
| 386 | |
| 387 | std::string QualName; |
| 388 | llvm::raw_string_ostream OS(QualName); |
| 389 | |
| 390 | for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); |
| 391 | I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | if (const ClassTemplateSpecializationDecl *Spec |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 393 | = dyn_cast<ClassTemplateSpecializationDecl>(*I)) { |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 394 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 395 | std::string TemplateArgsStr |
| 396 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 397 | TemplateArgs.getFlatArgumentList(), |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 398 | TemplateArgs.flat_size(), |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 399 | P); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 400 | OS << Spec->getName() << TemplateArgsStr; |
| 401 | } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) { |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 402 | if (ND->isAnonymousNamespace()) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 403 | OS << "<anonymous namespace>"; |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 404 | else |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 405 | OS << ND; |
| 406 | } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) { |
| 407 | if (!RD->getIdentifier()) |
| 408 | OS << "<anonymous " << RD->getKindName() << '>'; |
| 409 | else |
| 410 | OS << RD; |
| 411 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 412 | const FunctionProtoType *FT = 0; |
| 413 | if (FD->hasWrittenPrototype()) |
| 414 | FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>()); |
| 415 | |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 416 | OS << FD << '('; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 417 | if (FT) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 418 | unsigned NumParams = FD->getNumParams(); |
| 419 | for (unsigned i = 0; i < NumParams; ++i) { |
| 420 | if (i) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 421 | OS << ", "; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 422 | std::string Param; |
| 423 | FD->getParamDecl(i)->getType().getAsStringInternal(Param, P); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 424 | OS << Param; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | if (FT->isVariadic()) { |
| 428 | if (NumParams > 0) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 429 | OS << ", "; |
| 430 | OS << "..."; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 433 | OS << ')'; |
| 434 | } else { |
| 435 | OS << cast<NamedDecl>(*I); |
| 436 | } |
| 437 | OS << "::"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 438 | } |
| 439 | |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 440 | if (getDeclName()) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 441 | OS << this; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 442 | else |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 443 | OS << "<anonymous>"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 444 | |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 445 | return OS.str(); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 448 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 449 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 450 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 451 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 452 | // We want to keep it, unless it nominates same namespace. |
| 453 | if (getKind() == Decl::UsingDirective) { |
| 454 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == |
| 455 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); |
| 456 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 458 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 459 | // For function declarations, we keep track of redeclarations. |
| 460 | return FD->getPreviousDeclaration() == OldD; |
| 461 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 462 | // For function templates, the underlying function declarations are linked. |
| 463 | if (const FunctionTemplateDecl *FunctionTemplate |
| 464 | = dyn_cast<FunctionTemplateDecl>(this)) |
| 465 | if (const FunctionTemplateDecl *OldFunctionTemplate |
| 466 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 467 | return FunctionTemplate->getTemplatedDecl() |
| 468 | ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 470 | // For method declarations, we keep track of redeclarations. |
| 471 | if (isa<ObjCMethodDecl>(this)) |
| 472 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 474 | if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) |
| 475 | return true; |
| 476 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 477 | if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) |
| 478 | return cast<UsingShadowDecl>(this)->getTargetDecl() == |
| 479 | cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 480 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 481 | // For non-function declarations, if the declarations are of the |
| 482 | // same kind then this must be a redeclaration, or semantic analysis |
| 483 | // would not have given us the new declaration. |
| 484 | return this->getKind() == OldD->getKind(); |
| 485 | } |
| 486 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 487 | bool NamedDecl::hasLinkage() const { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 488 | return getLinkage() != NoLinkage; |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 489 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 490 | |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 491 | NamedDecl *NamedDecl::getUnderlyingDecl() { |
| 492 | NamedDecl *ND = this; |
| 493 | while (true) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 494 | if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 495 | ND = UD->getTargetDecl(); |
| 496 | else if (ObjCCompatibleAliasDecl *AD |
| 497 | = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 498 | return AD->getClassInterface(); |
| 499 | else |
| 500 | return ND; |
| 501 | } |
| 502 | } |
| 503 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 504 | bool NamedDecl::isCXXInstanceMember() const { |
| 505 | assert(isCXXClassMember() && |
| 506 | "checking whether non-member is instance member"); |
| 507 | |
| 508 | const NamedDecl *D = this; |
| 509 | if (isa<UsingShadowDecl>(D)) |
| 510 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 511 | |
| 512 | if (isa<FieldDecl>(D)) |
| 513 | return true; |
| 514 | if (isa<CXXMethodDecl>(D)) |
| 515 | return cast<CXXMethodDecl>(D)->isInstance(); |
| 516 | if (isa<FunctionTemplateDecl>(D)) |
| 517 | return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D) |
| 518 | ->getTemplatedDecl())->isInstance(); |
| 519 | return false; |
| 520 | } |
| 521 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 522 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 523 | // DeclaratorDecl Implementation |
| 524 | //===----------------------------------------------------------------------===// |
| 525 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 526 | DeclaratorDecl::~DeclaratorDecl() {} |
| 527 | void DeclaratorDecl::Destroy(ASTContext &C) { |
| 528 | if (hasExtInfo()) |
| 529 | C.Deallocate(getExtInfo()); |
| 530 | ValueDecl::Destroy(C); |
| 531 | } |
| 532 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 533 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame^] | 534 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 535 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 536 | return SourceLocation(); |
| 537 | } |
| 538 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 539 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier, |
| 540 | SourceRange QualifierRange) { |
| 541 | if (Qualifier) { |
| 542 | // Make sure the extended decl info is allocated. |
| 543 | if (!hasExtInfo()) { |
| 544 | // Save (non-extended) type source info pointer. |
| 545 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 546 | // Allocate external info struct. |
| 547 | DeclInfo = new (getASTContext()) ExtInfo; |
| 548 | // Restore savedTInfo into (extended) decl info. |
| 549 | getExtInfo()->TInfo = savedTInfo; |
| 550 | } |
| 551 | // Set qualifier info. |
| 552 | getExtInfo()->NNS = Qualifier; |
| 553 | getExtInfo()->NNSRange = QualifierRange; |
| 554 | } |
| 555 | else { |
| 556 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| 557 | assert(QualifierRange.isInvalid()); |
| 558 | if (hasExtInfo()) { |
| 559 | // Save type source info pointer. |
| 560 | TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; |
| 561 | // Deallocate the extended decl info. |
| 562 | getASTContext().Deallocate(getExtInfo()); |
| 563 | // Restore savedTInfo into (non-extended) decl info. |
| 564 | DeclInfo = savedTInfo; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 569 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 570 | // VarDecl Implementation |
| 571 | //===----------------------------------------------------------------------===// |
| 572 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 573 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 574 | switch (SC) { |
| 575 | case VarDecl::None: break; |
| 576 | case VarDecl::Auto: return "auto"; break; |
| 577 | case VarDecl::Extern: return "extern"; break; |
| 578 | case VarDecl::PrivateExtern: return "__private_extern__"; break; |
| 579 | case VarDecl::Register: return "register"; break; |
| 580 | case VarDecl::Static: return "static"; break; |
| 581 | } |
| 582 | |
| 583 | assert(0 && "Invalid storage class"); |
| 584 | return 0; |
| 585 | } |
| 586 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 587 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 588 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 589 | StorageClass S, StorageClass SCAsWritten) { |
| 590 | return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | void VarDecl::Destroy(ASTContext& C) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 594 | Expr *Init = getInit(); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 595 | if (Init) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 596 | Init->Destroy(C); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 597 | if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) { |
| 598 | Eval->~EvaluatedStmt(); |
| 599 | C.Deallocate(Eval); |
| 600 | } |
| 601 | } |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 602 | this->~VarDecl(); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 603 | DeclaratorDecl::Destroy(C); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | VarDecl::~VarDecl() { |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 609 | SourceRange VarDecl::getSourceRange() const { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 610 | SourceLocation Start = getTypeSpecStartLoc(); |
| 611 | if (Start.isInvalid()) |
| 612 | Start = getLocation(); |
| 613 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 614 | if (getInit()) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 615 | return SourceRange(Start, getInit()->getLocEnd()); |
| 616 | return SourceRange(Start, getLocation()); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 619 | bool VarDecl::isExternC() const { |
| 620 | ASTContext &Context = getASTContext(); |
| 621 | if (!Context.getLangOptions().CPlusPlus) |
| 622 | return (getDeclContext()->isTranslationUnit() && |
| 623 | getStorageClass() != Static) || |
| 624 | (getDeclContext()->isFunctionOrMethod() && hasExternalStorage()); |
| 625 | |
| 626 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
| 627 | DC = DC->getParent()) { |
| 628 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 629 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
| 630 | return getStorageClass() != Static; |
| 631 | |
| 632 | break; |
| 633 | } |
| 634 | |
| 635 | if (DC->isFunctionOrMethod()) |
| 636 | return false; |
| 637 | } |
| 638 | |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | VarDecl *VarDecl::getCanonicalDecl() { |
| 643 | return getFirstDeclaration(); |
| 644 | } |
| 645 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 646 | VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const { |
| 647 | // C++ [basic.def]p2: |
| 648 | // A declaration is a definition unless [...] it contains the 'extern' |
| 649 | // specifier or a linkage-specification and neither an initializer [...], |
| 650 | // it declares a static data member in a class declaration [...]. |
| 651 | // C++ [temp.expl.spec]p15: |
| 652 | // An explicit specialization of a static data member of a template is a |
| 653 | // definition if the declaration includes an initializer; otherwise, it is |
| 654 | // a declaration. |
| 655 | if (isStaticDataMember()) { |
| 656 | if (isOutOfLine() && (hasInit() || |
| 657 | getTemplateSpecializationKind() != TSK_ExplicitSpecialization)) |
| 658 | return Definition; |
| 659 | else |
| 660 | return DeclarationOnly; |
| 661 | } |
| 662 | // C99 6.7p5: |
| 663 | // A definition of an identifier is a declaration for that identifier that |
| 664 | // [...] causes storage to be reserved for that object. |
| 665 | // Note: that applies for all non-file-scope objects. |
| 666 | // C99 6.9.2p1: |
| 667 | // If the declaration of an identifier for an object has file scope and an |
| 668 | // initializer, the declaration is an external definition for the identifier |
| 669 | if (hasInit()) |
| 670 | return Definition; |
| 671 | // AST for 'extern "C" int foo;' is annotated with 'extern'. |
| 672 | if (hasExternalStorage()) |
| 673 | return DeclarationOnly; |
| 674 | |
| 675 | // C99 6.9.2p2: |
| 676 | // A declaration of an object that has file scope without an initializer, |
| 677 | // and without a storage class specifier or the scs 'static', constitutes |
| 678 | // a tentative definition. |
| 679 | // No such thing in C++. |
| 680 | if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl()) |
| 681 | return TentativeDefinition; |
| 682 | |
| 683 | // What's left is (in C, block-scope) declarations without initializers or |
| 684 | // external storage. These are definitions. |
| 685 | return Definition; |
| 686 | } |
| 687 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 688 | VarDecl *VarDecl::getActingDefinition() { |
| 689 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 690 | if (Kind != TentativeDefinition) |
| 691 | return 0; |
| 692 | |
| 693 | VarDecl *LastTentative = false; |
| 694 | VarDecl *First = getFirstDeclaration(); |
| 695 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 696 | I != E; ++I) { |
| 697 | Kind = (*I)->isThisDeclarationADefinition(); |
| 698 | if (Kind == Definition) |
| 699 | return 0; |
| 700 | else if (Kind == TentativeDefinition) |
| 701 | LastTentative = *I; |
| 702 | } |
| 703 | return LastTentative; |
| 704 | } |
| 705 | |
| 706 | bool VarDecl::isTentativeDefinitionNow() const { |
| 707 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 708 | if (Kind != TentativeDefinition) |
| 709 | return false; |
| 710 | |
| 711 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 712 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 713 | return false; |
| 714 | } |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 715 | return true; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 718 | VarDecl *VarDecl::getDefinition() { |
Sebastian Redl | e2c52d2 | 2010-02-02 17:55:12 +0000 | [diff] [blame] | 719 | VarDecl *First = getFirstDeclaration(); |
| 720 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 721 | I != E; ++I) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 722 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 723 | return *I; |
| 724 | } |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 729 | redecl_iterator I = redecls_begin(), E = redecls_end(); |
| 730 | while (I != E && !I->getInit()) |
| 731 | ++I; |
| 732 | |
| 733 | if (I != E) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 734 | D = *I; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 735 | return I->getInit(); |
| 736 | } |
| 737 | return 0; |
| 738 | } |
| 739 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 740 | bool VarDecl::isOutOfLine() const { |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 741 | if (Decl::isOutOfLine()) |
| 742 | return true; |
Chandler Carruth | 8761d68 | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 743 | |
| 744 | if (!isStaticDataMember()) |
| 745 | return false; |
| 746 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 747 | // If this static data member was instantiated from a static data member of |
| 748 | // a class template, check whether that static data member was defined |
| 749 | // out-of-line. |
| 750 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 751 | return VD->isOutOfLine(); |
| 752 | |
| 753 | return false; |
| 754 | } |
| 755 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 756 | VarDecl *VarDecl::getOutOfLineDefinition() { |
| 757 | if (!isStaticDataMember()) |
| 758 | return 0; |
| 759 | |
| 760 | for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); |
| 761 | RD != RDEnd; ++RD) { |
| 762 | if (RD->getLexicalDeclContext()->isFileContext()) |
| 763 | return *RD; |
| 764 | } |
| 765 | |
| 766 | return 0; |
| 767 | } |
| 768 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 769 | void VarDecl::setInit(Expr *I) { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 770 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| 771 | Eval->~EvaluatedStmt(); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 772 | getASTContext().Deallocate(Eval); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | Init = I; |
| 776 | } |
| 777 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 778 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 779 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 780 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 785 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 786 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 787 | return MSI->getTemplateSpecializationKind(); |
| 788 | |
| 789 | return TSK_Undeclared; |
| 790 | } |
| 791 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 792 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 793 | return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 794 | } |
| 795 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 796 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 797 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 798 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 799 | assert(MSI && "Not an instantiated static data member?"); |
| 800 | MSI->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 801 | if (TSK != TSK_ExplicitSpecialization && |
| 802 | PointOfInstantiation.isValid() && |
| 803 | MSI->getPointOfInstantiation().isInvalid()) |
| 804 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 807 | //===----------------------------------------------------------------------===// |
| 808 | // ParmVarDecl Implementation |
| 809 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 810 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 811 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
| 812 | SourceLocation L, IdentifierInfo *Id, |
| 813 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 814 | StorageClass S, StorageClass SCAsWritten, |
| 815 | Expr *DefArg) { |
| 816 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, |
| 817 | S, SCAsWritten, DefArg); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 820 | Expr *ParmVarDecl::getDefaultArg() { |
| 821 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 822 | assert(!hasUninstantiatedDefaultArg() && |
| 823 | "Default argument is not yet instantiated!"); |
| 824 | |
| 825 | Expr *Arg = getInit(); |
| 826 | if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg)) |
| 827 | return E->getSubExpr(); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 828 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 829 | return Arg; |
| 830 | } |
| 831 | |
| 832 | unsigned ParmVarDecl::getNumDefaultArgTemporaries() const { |
| 833 | if (const CXXExprWithTemporaries *E = |
| 834 | dyn_cast<CXXExprWithTemporaries>(getInit())) |
| 835 | return E->getNumTemporaries(); |
| 836 | |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 837 | return 0; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 840 | CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) { |
| 841 | assert(getNumDefaultArgTemporaries() && |
| 842 | "Default arguments does not have any temporaries!"); |
| 843 | |
| 844 | CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit()); |
| 845 | return E->getTemporary(i); |
| 846 | } |
| 847 | |
| 848 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 849 | if (const Expr *E = getInit()) |
| 850 | return E->getSourceRange(); |
| 851 | |
| 852 | if (hasUninstantiatedDefaultArg()) |
| 853 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 854 | |
| 855 | return SourceRange(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 858 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 859 | // FunctionDecl Implementation |
| 860 | //===----------------------------------------------------------------------===// |
| 861 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 862 | void FunctionDecl::Destroy(ASTContext& C) { |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 863 | if (Body && Body.isOffset()) |
| 864 | Body.get(C.getExternalSource())->Destroy(C); |
Ted Kremenek | b65cf41 | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 865 | |
| 866 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 867 | (*I)->Destroy(C); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 868 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 869 | FunctionTemplateSpecializationInfo *FTSInfo |
| 870 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
| 871 | if (FTSInfo) |
| 872 | C.Deallocate(FTSInfo); |
| 873 | |
| 874 | MemberSpecializationInfo *MSInfo |
| 875 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 876 | if (MSInfo) |
| 877 | C.Deallocate(MSInfo); |
| 878 | |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 879 | C.Deallocate(ParamInfo); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 880 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 881 | DeclaratorDecl::Destroy(C); |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 882 | } |
| 883 | |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 884 | void FunctionDecl::getNameForDiagnostic(std::string &S, |
| 885 | const PrintingPolicy &Policy, |
| 886 | bool Qualified) const { |
| 887 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 888 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 889 | if (TemplateArgs) |
| 890 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 891 | TemplateArgs->getFlatArgumentList(), |
| 892 | TemplateArgs->flat_size(), |
| 893 | Policy); |
| 894 | |
| 895 | } |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 896 | |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 897 | bool FunctionDecl::isVariadic() const { |
| 898 | if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>()) |
| 899 | return FT->isVariadic(); |
| 900 | return false; |
| 901 | } |
| 902 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 903 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 904 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 905 | if (I->Body) { |
| 906 | Definition = *I; |
| 907 | return I->Body.get(getASTContext().getExternalSource()); |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
| 911 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 914 | void FunctionDecl::setBody(Stmt *B) { |
| 915 | Body = B; |
Argyrios Kyrtzidis | 1a5364e | 2009-06-22 17:13:31 +0000 | [diff] [blame] | 916 | if (B) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 917 | EndRangeLoc = B->getLocEnd(); |
| 918 | } |
| 919 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 920 | bool FunctionDecl::isMain() const { |
| 921 | ASTContext &Context = getASTContext(); |
John McCall | 07a5c22 | 2009-08-15 02:09:25 +0000 | [diff] [blame] | 922 | return !Context.getLangOptions().Freestanding && |
| 923 | getDeclContext()->getLookupContext()->isTranslationUnit() && |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 924 | getIdentifier() && getIdentifier()->isStr("main"); |
| 925 | } |
| 926 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 927 | bool FunctionDecl::isExternC() const { |
| 928 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 929 | // In C, any non-static, non-overloadable function has external |
| 930 | // linkage. |
| 931 | if (!Context.getLangOptions().CPlusPlus) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 932 | return getStorageClass() != Static && !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 933 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 935 | DC = DC->getParent()) { |
| 936 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 937 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | return getStorageClass() != Static && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 939 | !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 940 | |
| 941 | break; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | return false; |
| 946 | } |
| 947 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 948 | bool FunctionDecl::isGlobal() const { |
| 949 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 950 | return Method->isStatic(); |
| 951 | |
| 952 | if (getStorageClass() == Static) |
| 953 | return false; |
| 954 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 956 | DC->isNamespace(); |
| 957 | DC = DC->getParent()) { |
| 958 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 959 | if (!Namespace->getDeclName()) |
| 960 | return false; |
| 961 | break; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | return true; |
| 966 | } |
| 967 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 968 | void |
| 969 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| 970 | redeclarable_base::setPreviousDeclaration(PrevDecl); |
| 971 | |
| 972 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 973 | FunctionTemplateDecl *PrevFunTmpl |
| 974 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 975 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 976 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | const FunctionDecl *FunctionDecl::getCanonicalDecl() const { |
| 981 | return getFirstDeclaration(); |
| 982 | } |
| 983 | |
| 984 | FunctionDecl *FunctionDecl::getCanonicalDecl() { |
| 985 | return getFirstDeclaration(); |
| 986 | } |
| 987 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 988 | /// \brief Returns a value indicating whether this function |
| 989 | /// corresponds to a builtin function. |
| 990 | /// |
| 991 | /// The function corresponds to a built-in function if it is |
| 992 | /// declared at translation scope or within an extern "C" block and |
| 993 | /// its name matches with the name of a builtin. The returned value |
| 994 | /// 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] | 995 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 996 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 997 | unsigned FunctionDecl::getBuiltinID() const { |
| 998 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 999 | if (!getIdentifier() || !getIdentifier()->getBuiltinID()) |
| 1000 | return 0; |
| 1001 | |
| 1002 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| 1003 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 1004 | return BuiltinID; |
| 1005 | |
| 1006 | // This function has the name of a known C library |
| 1007 | // function. Determine whether it actually refers to the C library |
| 1008 | // function or whether it just has the same name. |
| 1009 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1010 | // If this is a static function, it's not a builtin. |
| 1011 | if (getStorageClass() == Static) |
| 1012 | return 0; |
| 1013 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1014 | // If this function is at translation-unit scope and we're not in |
| 1015 | // C++, it refers to the C library function. |
| 1016 | if (!Context.getLangOptions().CPlusPlus && |
| 1017 | getDeclContext()->isTranslationUnit()) |
| 1018 | return BuiltinID; |
| 1019 | |
| 1020 | // If the function is in an extern "C" linkage specification and is |
| 1021 | // not marked "overloadable", it's the real function. |
| 1022 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1023 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1024 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1025 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1026 | return BuiltinID; |
| 1027 | |
| 1028 | // Not a builtin |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1033 | /// getNumParams - Return the number of parameters this function must have |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 1034 | /// based on its FunctionType. This is the length of the PararmInfo array |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1035 | /// after it has been created. |
| 1036 | unsigned FunctionDecl::getNumParams() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1037 | const FunctionType *FT = getType()->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1038 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 1039 | return 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1040 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1044 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1045 | assert(ParamInfo == 0 && "Already has param info!"); |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 1046 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1048 | // Zero params -> null pointer. |
| 1049 | if (NumParams) { |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1050 | void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 1051 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1052 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1053 | |
Argyrios Kyrtzidis | 96888cc | 2009-06-23 00:42:00 +0000 | [diff] [blame] | 1054 | // Update source range. The check below allows us to set EndRangeLoc before |
| 1055 | // setting the parameters. |
Argyrios Kyrtzidis | cb5f8f5 | 2009-06-23 00:42:15 +0000 | [diff] [blame] | 1056 | if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1057 | EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1061 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 1062 | /// needed to call this function. This may be fewer than the number of |
| 1063 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 1064 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1065 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 1066 | unsigned NumRequiredArgs = getNumParams(); |
| 1067 | while (NumRequiredArgs > 0 |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 1068 | && getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1069 | --NumRequiredArgs; |
| 1070 | |
| 1071 | return NumRequiredArgs; |
| 1072 | } |
| 1073 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1074 | bool FunctionDecl::isInlined() const { |
Anders Carlsson | 48eda2c | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 1075 | // FIXME: This is not enough. Consider: |
| 1076 | // |
| 1077 | // inline void f(); |
| 1078 | // void f() { } |
| 1079 | // |
| 1080 | // f is inlined, but does not have inline specified. |
| 1081 | // To fix this we should add an 'inline' flag to FunctionDecl. |
| 1082 | if (isInlineSpecified()) |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1083 | return true; |
Anders Carlsson | 48eda2c | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 1084 | |
| 1085 | if (isa<CXXMethodDecl>(this)) { |
| 1086 | if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified()) |
| 1087 | return true; |
| 1088 | } |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1089 | |
| 1090 | switch (getTemplateSpecializationKind()) { |
| 1091 | case TSK_Undeclared: |
| 1092 | case TSK_ExplicitSpecialization: |
| 1093 | return false; |
| 1094 | |
| 1095 | case TSK_ImplicitInstantiation: |
| 1096 | case TSK_ExplicitInstantiationDeclaration: |
| 1097 | case TSK_ExplicitInstantiationDefinition: |
| 1098 | // Handle below. |
| 1099 | break; |
| 1100 | } |
| 1101 | |
| 1102 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
| 1103 | Stmt *Pattern = 0; |
| 1104 | if (PatternDecl) |
| 1105 | Pattern = PatternDecl->getBody(PatternDecl); |
| 1106 | |
| 1107 | if (Pattern && PatternDecl) |
| 1108 | return PatternDecl->isInlined(); |
| 1109 | |
| 1110 | return false; |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1113 | /// \brief For an inline function definition in C or C++, determine whether the |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1114 | /// definition will be externally visible. |
| 1115 | /// |
| 1116 | /// Inline function definitions are always available for inlining optimizations. |
| 1117 | /// However, depending on the language dialect, declaration specifiers, and |
| 1118 | /// attributes, the definition of an inline function may or may not be |
| 1119 | /// "externally" visible to other translation units in the program. |
| 1120 | /// |
| 1121 | /// In C99, inline definitions are not externally visible by default. However, |
Mike Stump | 1e5fd7f | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 1122 | /// if even one of the global-scope declarations is marked "extern inline", the |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1123 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 1124 | /// |
| 1125 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 1126 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 1127 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 1128 | /// an externally visible symbol, but "extern inline" will not create an |
| 1129 | /// externally visible symbol. |
| 1130 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
| 1131 | assert(isThisDeclarationADefinition() && "Must have the function definition"); |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1132 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1133 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1134 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1135 | if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) { |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1136 | // GNU inline semantics. Based on a number of examples, we came up with the |
| 1137 | // following heuristic: if the "inline" keyword is present on a |
| 1138 | // declaration of the function but "extern" is not present on that |
| 1139 | // declaration, then the symbol is externally visible. Otherwise, the GNU |
| 1140 | // "extern inline" semantics applies and the symbol is not externally |
| 1141 | // visible. |
| 1142 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 1143 | Redecl != RedeclEnd; |
| 1144 | ++Redecl) { |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1145 | if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1146 | return true; |
| 1147 | } |
| 1148 | |
| 1149 | // GNU "extern inline" semantics; no externally visible symbol. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 1150 | return false; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | // C99 6.7.4p6: |
| 1154 | // [...] If all of the file scope declarations for a function in a |
| 1155 | // translation unit include the inline function specifier without extern, |
| 1156 | // then the definition in that translation unit is an inline definition. |
| 1157 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 1158 | Redecl != RedeclEnd; |
| 1159 | ++Redecl) { |
| 1160 | // Only consider file-scope declarations in this test. |
| 1161 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 1162 | continue; |
| 1163 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1164 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1165 | return true; // Not an inline definition |
| 1166 | } |
| 1167 | |
| 1168 | // C99 6.7.4p6: |
| 1169 | // An inline definition does not provide an external definition for the |
| 1170 | // function, and does not forbid an external definition in another |
| 1171 | // translation unit. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 1172 | return false; |
| 1173 | } |
| 1174 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1175 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 1176 | /// function represents, if any. |
| 1177 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1178 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 1179 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1180 | else |
| 1181 | return OO_None; |
| 1182 | } |
| 1183 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 1184 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 1185 | /// represents, if any. |
| 1186 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 1187 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 1188 | return getDeclName().getCXXLiteralIdentifier(); |
| 1189 | else |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1193 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1194 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1195 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| 1196 | |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1200 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 1201 | return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 1202 | } |
| 1203 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1204 | void |
| 1205 | FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD, |
| 1206 | TemplateSpecializationKind TSK) { |
| 1207 | assert(TemplateOrSpecialization.isNull() && |
| 1208 | "Member function is already a specialization"); |
| 1209 | MemberSpecializationInfo *Info |
| 1210 | = new (getASTContext()) MemberSpecializationInfo(FD, TSK); |
| 1211 | TemplateOrSpecialization = Info; |
| 1212 | } |
| 1213 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1214 | bool FunctionDecl::isImplicitlyInstantiable() const { |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 1215 | // If the function is invalid, it can't be implicitly instantiated. |
| 1216 | if (isInvalidDecl()) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1217 | return false; |
| 1218 | |
| 1219 | switch (getTemplateSpecializationKind()) { |
| 1220 | case TSK_Undeclared: |
| 1221 | case TSK_ExplicitSpecialization: |
| 1222 | case TSK_ExplicitInstantiationDefinition: |
| 1223 | return false; |
| 1224 | |
| 1225 | case TSK_ImplicitInstantiation: |
| 1226 | return true; |
| 1227 | |
| 1228 | case TSK_ExplicitInstantiationDeclaration: |
| 1229 | // Handled below. |
| 1230 | break; |
| 1231 | } |
| 1232 | |
| 1233 | // Find the actual template from which we will instantiate. |
| 1234 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
| 1235 | Stmt *Pattern = 0; |
| 1236 | if (PatternDecl) |
| 1237 | Pattern = PatternDecl->getBody(PatternDecl); |
| 1238 | |
| 1239 | // C++0x [temp.explicit]p9: |
| 1240 | // Except for inline functions, other explicit instantiation declarations |
| 1241 | // have the effect of suppressing the implicit instantiation of the entity |
| 1242 | // to which they refer. |
| 1243 | if (!Pattern || !PatternDecl) |
| 1244 | return true; |
| 1245 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1246 | return PatternDecl->isInlined(); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
| 1250 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 1251 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 1252 | // If we have hit a point where the user provided a specialization of |
| 1253 | // this template, we're done looking. |
| 1254 | if (Primary->isMemberSpecialization()) |
| 1255 | break; |
| 1256 | |
| 1257 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 1258 | } |
| 1259 | |
| 1260 | return Primary->getTemplatedDecl(); |
| 1261 | } |
| 1262 | |
| 1263 | return getInstantiatedFromMemberFunction(); |
| 1264 | } |
| 1265 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1266 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1268 | = TemplateOrSpecialization |
| 1269 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1270 | return Info->Template.getPointer(); |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1271 | } |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
| 1275 | const TemplateArgumentList * |
| 1276 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1277 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1278 | = TemplateOrSpecialization |
| 1279 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1280 | return Info->TemplateArguments; |
| 1281 | } |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 1285 | const TemplateArgumentListInfo * |
| 1286 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 1287 | if (FunctionTemplateSpecializationInfo *Info |
| 1288 | = TemplateOrSpecialization |
| 1289 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 1290 | return Info->TemplateArgumentsAsWritten; |
| 1291 | } |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | void |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1296 | FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1297 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1298 | void *InsertPos, |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 1299 | TemplateSpecializationKind TSK, |
| 1300 | const TemplateArgumentListInfo *TemplateArgsAsWritten) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1301 | assert(TSK != TSK_Undeclared && |
| 1302 | "Must specify the type of function template specialization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1303 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1304 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1305 | if (!Info) |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1306 | Info = new (getASTContext()) FunctionTemplateSpecializationInfo; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1308 | Info->Function = this; |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1309 | Info->Template.setPointer(Template); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1310 | Info->Template.setInt(TSK - 1); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1311 | Info->TemplateArguments = TemplateArgs; |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 1312 | Info->TemplateArgumentsAsWritten = TemplateArgsAsWritten; |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1313 | TemplateOrSpecialization = Info; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1315 | // Insert this function template specialization into the set of known |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1316 | // function template specializations. |
| 1317 | if (InsertPos) |
| 1318 | Template->getSpecializations().InsertNode(Info, InsertPos); |
| 1319 | else { |
| 1320 | // Try to insert the new node. If there is an existing node, remove it |
| 1321 | // first. |
| 1322 | FunctionTemplateSpecializationInfo *Existing |
| 1323 | = Template->getSpecializations().GetOrInsertNode(Info); |
| 1324 | if (Existing) { |
| 1325 | Template->getSpecializations().RemoveNode(Existing); |
| 1326 | Template->getSpecializations().GetOrInsertNode(Info); |
| 1327 | } |
| 1328 | } |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1331 | void |
| 1332 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 1333 | const UnresolvedSetImpl &Templates, |
| 1334 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1335 | assert(TemplateOrSpecialization.isNull()); |
| 1336 | size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo); |
| 1337 | Size += Templates.size() * sizeof(FunctionTemplateDecl*); |
John McCall | 21c0160 | 2010-04-13 22:18:28 +0000 | [diff] [blame] | 1338 | Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1339 | void *Buffer = Context.Allocate(Size); |
| 1340 | DependentFunctionTemplateSpecializationInfo *Info = |
| 1341 | new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates, |
| 1342 | TemplateArgs); |
| 1343 | TemplateOrSpecialization = Info; |
| 1344 | } |
| 1345 | |
| 1346 | DependentFunctionTemplateSpecializationInfo:: |
| 1347 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 1348 | const TemplateArgumentListInfo &TArgs) |
| 1349 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
| 1350 | |
| 1351 | d.NumTemplates = Ts.size(); |
| 1352 | d.NumArgs = TArgs.size(); |
| 1353 | |
| 1354 | FunctionTemplateDecl **TsArray = |
| 1355 | const_cast<FunctionTemplateDecl**>(getTemplates()); |
| 1356 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 1357 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 1358 | |
| 1359 | TemplateArgumentLoc *ArgsArray = |
| 1360 | const_cast<TemplateArgumentLoc*>(getTemplateArgs()); |
| 1361 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 1362 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 1363 | } |
| 1364 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1365 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | // For a function template specialization, query the specialization |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1367 | // information object. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1368 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1369 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1370 | if (FTSInfo) |
| 1371 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1373 | MemberSpecializationInfo *MSInfo |
| 1374 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 1375 | if (MSInfo) |
| 1376 | return MSInfo->getTemplateSpecializationKind(); |
| 1377 | |
| 1378 | return TSK_Undeclared; |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1381 | void |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1382 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 1383 | SourceLocation PointOfInstantiation) { |
| 1384 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 1385 | = TemplateOrSpecialization.dyn_cast< |
| 1386 | FunctionTemplateSpecializationInfo*>()) { |
| 1387 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 1388 | if (TSK != TSK_ExplicitSpecialization && |
| 1389 | PointOfInstantiation.isValid() && |
| 1390 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 1391 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1392 | } else if (MemberSpecializationInfo *MSInfo |
| 1393 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 1394 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1395 | if (TSK != TSK_ExplicitSpecialization && |
| 1396 | PointOfInstantiation.isValid() && |
| 1397 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 1398 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1399 | } else |
| 1400 | assert(false && "Function cannot have a template specialization kind"); |
| 1401 | } |
| 1402 | |
| 1403 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1404 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 1405 | = TemplateOrSpecialization.dyn_cast< |
| 1406 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1407 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1408 | else if (MemberSpecializationInfo *MSInfo |
| 1409 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1410 | return MSInfo->getPointOfInstantiation(); |
| 1411 | |
| 1412 | return SourceLocation(); |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1415 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1416 | if (Decl::isOutOfLine()) |
| 1417 | return true; |
| 1418 | |
| 1419 | // If this function was instantiated from a member function of a |
| 1420 | // class template, check whether that member function was defined out-of-line. |
| 1421 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 1422 | const FunctionDecl *Definition; |
| 1423 | if (FD->getBody(Definition)) |
| 1424 | return Definition->isOutOfLine(); |
| 1425 | } |
| 1426 | |
| 1427 | // If this function was instantiated from a function template, |
| 1428 | // check whether that function template was defined out-of-line. |
| 1429 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 1430 | const FunctionDecl *Definition; |
| 1431 | if (FunTmpl->getTemplatedDecl()->getBody(Definition)) |
| 1432 | return Definition->isOutOfLine(); |
| 1433 | } |
| 1434 | |
| 1435 | return false; |
| 1436 | } |
| 1437 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1438 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1439 | // FieldDecl Implementation |
| 1440 | //===----------------------------------------------------------------------===// |
| 1441 | |
| 1442 | FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 1443 | IdentifierInfo *Id, QualType T, |
| 1444 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable) { |
| 1445 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable); |
| 1446 | } |
| 1447 | |
| 1448 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 1449 | if (!isImplicit() || getDeclName()) |
| 1450 | return false; |
| 1451 | |
| 1452 | if (const RecordType *Record = getType()->getAs<RecordType>()) |
| 1453 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 1454 | |
| 1455 | return false; |
| 1456 | } |
| 1457 | |
| 1458 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1459 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1460 | //===----------------------------------------------------------------------===// |
| 1461 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1462 | void TagDecl::Destroy(ASTContext &C) { |
| 1463 | if (hasExtInfo()) |
| 1464 | C.Deallocate(getExtInfo()); |
| 1465 | TypeDecl::Destroy(C); |
| 1466 | } |
| 1467 | |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 1468 | SourceRange TagDecl::getSourceRange() const { |
| 1469 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 1470 | return SourceRange(TagKeywordLoc, E); |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 1473 | TagDecl* TagDecl::getCanonicalDecl() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1474 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1477 | void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) { |
| 1478 | TypedefDeclOrQualifier = TDD; |
| 1479 | if (TypeForDecl) |
| 1480 | TypeForDecl->ClearLinkageCache(); |
| 1481 | } |
| 1482 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1483 | void TagDecl::startDefinition() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1484 | if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { |
| 1485 | TagT->decl.setPointer(this); |
| 1486 | TagT->decl.setInt(1); |
Douglas Gregor | 9ffce21 | 2010-04-30 04:39:27 +0000 | [diff] [blame] | 1487 | } else if (InjectedClassNameType *Injected |
| 1488 | = const_cast<InjectedClassNameType *>( |
| 1489 | TypeForDecl->getAs<InjectedClassNameType>())) { |
| 1490 | Injected->Decl = cast<CXXRecordDecl>(this); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1491 | } |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1492 | |
| 1493 | if (isa<CXXRecordDecl>(this)) { |
| 1494 | CXXRecordDecl *D = cast<CXXRecordDecl>(this); |
| 1495 | struct CXXRecordDecl::DefinitionData *Data = |
| 1496 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
John McCall | 2243288 | 2010-03-26 21:56:38 +0000 | [diff] [blame] | 1497 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 1498 | cast<CXXRecordDecl>(*I)->DefinitionData = Data; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1499 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | void TagDecl::completeDefinition() { |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 1503 | assert((!isa<CXXRecordDecl>(this) || |
| 1504 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 1505 | "definition completed but not started"); |
| 1506 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1507 | IsDefinition = true; |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1508 | if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { |
| 1509 | assert(TagT->decl.getPointer() == this && |
| 1510 | "Attempt to redefine a tag definition?"); |
| 1511 | TagT->decl.setInt(0); |
Douglas Gregor | 9ffce21 | 2010-04-30 04:39:27 +0000 | [diff] [blame] | 1512 | } else if (InjectedClassNameType *Injected |
| 1513 | = const_cast<InjectedClassNameType *>( |
| 1514 | TypeForDecl->getAs<InjectedClassNameType>())) { |
| 1515 | assert(Injected->Decl == this && |
| 1516 | "Attempt to redefine a class template definition?"); |
Chandler Carruth | a842697 | 2010-05-06 05:28:42 +0000 | [diff] [blame] | 1517 | (void)Injected; |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1518 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1521 | TagDecl* TagDecl::getDefinition() const { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1522 | if (isDefinition()) |
| 1523 | return const_cast<TagDecl *>(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | |
| 1525 | for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1526 | R != REnd; ++R) |
| 1527 | if (R->isDefinition()) |
| 1528 | return *R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1530 | return 0; |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1533 | void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier, |
| 1534 | SourceRange QualifierRange) { |
| 1535 | if (Qualifier) { |
| 1536 | // Make sure the extended qualifier info is allocated. |
| 1537 | if (!hasExtInfo()) |
| 1538 | TypedefDeclOrQualifier = new (getASTContext()) ExtInfo; |
| 1539 | // Set qualifier info. |
| 1540 | getExtInfo()->NNS = Qualifier; |
| 1541 | getExtInfo()->NNSRange = QualifierRange; |
| 1542 | } |
| 1543 | else { |
| 1544 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| 1545 | assert(QualifierRange.isInvalid()); |
| 1546 | if (hasExtInfo()) { |
| 1547 | getASTContext().Deallocate(getExtInfo()); |
| 1548 | TypedefDeclOrQualifier = (TypedefDecl*) 0; |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1553 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1554 | // EnumDecl Implementation |
| 1555 | //===----------------------------------------------------------------------===// |
| 1556 | |
| 1557 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 1558 | IdentifierInfo *Id, SourceLocation TKL, |
| 1559 | EnumDecl *PrevDecl) { |
| 1560 | EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL); |
| 1561 | C.getTypeDeclType(Enum, PrevDecl); |
| 1562 | return Enum; |
| 1563 | } |
| 1564 | |
| 1565 | void EnumDecl::Destroy(ASTContext& C) { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1566 | TagDecl::Destroy(C); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1569 | void EnumDecl::completeDefinition(QualType NewType, |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 1570 | QualType NewPromotionType, |
| 1571 | unsigned NumPositiveBits, |
| 1572 | unsigned NumNegativeBits) { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1573 | assert(!isDefinition() && "Cannot redefine enums!"); |
| 1574 | IntegerType = NewType; |
| 1575 | PromotionType = NewPromotionType; |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 1576 | setNumPositiveBits(NumPositiveBits); |
| 1577 | setNumNegativeBits(NumNegativeBits); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1578 | TagDecl::completeDefinition(); |
| 1579 | } |
| 1580 | |
| 1581 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1582 | // RecordDecl Implementation |
| 1583 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1584 | |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 1585 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1586 | IdentifierInfo *Id, RecordDecl *PrevDecl, |
| 1587 | SourceLocation TKL) |
| 1588 | : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1589 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1590 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1591 | HasObjectMember = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1592 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1596 | SourceLocation L, IdentifierInfo *Id, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 1597 | SourceLocation TKL, RecordDecl* PrevDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1599 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1600 | C.getTypeDeclType(R, PrevDecl); |
| 1601 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 1604 | RecordDecl::~RecordDecl() { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | void RecordDecl::Destroy(ASTContext& C) { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 1608 | TagDecl::Destroy(C); |
| 1609 | } |
| 1610 | |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 1611 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1612 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 1613 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 1614 | } |
| 1615 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1616 | /// completeDefinition - Notes that the definition of this type is now |
| 1617 | /// complete. |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1618 | void RecordDecl::completeDefinition() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1619 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1620 | TagDecl::completeDefinition(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
John McCall | bc365c5 | 2010-05-21 01:17:40 +0000 | [diff] [blame] | 1623 | ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() { |
| 1624 | // Force the decl chain to come into existence properly. |
| 1625 | if (!getNextDeclInContext()) getParent()->decls_begin(); |
| 1626 | |
| 1627 | assert(isAnonymousStructOrUnion()); |
| 1628 | ValueDecl *D = cast<ValueDecl>(getNextDeclInContext()); |
| 1629 | assert(D->getType()->isRecordType()); |
| 1630 | assert(D->getType()->getAs<RecordType>()->getDecl() == this); |
| 1631 | return D; |
| 1632 | } |
| 1633 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1634 | //===----------------------------------------------------------------------===// |
| 1635 | // BlockDecl Implementation |
| 1636 | //===----------------------------------------------------------------------===// |
| 1637 | |
| 1638 | BlockDecl::~BlockDecl() { |
| 1639 | } |
| 1640 | |
| 1641 | void BlockDecl::Destroy(ASTContext& C) { |
| 1642 | if (Body) |
| 1643 | Body->Destroy(C); |
| 1644 | |
| 1645 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 1646 | (*I)->Destroy(C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1647 | |
| 1648 | C.Deallocate(ParamInfo); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1649 | Decl::Destroy(C); |
| 1650 | } |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1651 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1652 | void BlockDecl::setParams(ParmVarDecl **NewParamInfo, |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1653 | unsigned NParms) { |
| 1654 | assert(ParamInfo == 0 && "Already has param info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1656 | // Zero params -> null pointer. |
| 1657 | if (NParms) { |
| 1658 | NumParams = NParms; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1659 | void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1660 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
| 1661 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | unsigned BlockDecl::getNumParams() const { |
| 1666 | return NumParams; |
| 1667 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1668 | |
| 1669 | |
| 1670 | //===----------------------------------------------------------------------===// |
| 1671 | // Other Decl Allocation/Deallocation Method Implementations |
| 1672 | //===----------------------------------------------------------------------===// |
| 1673 | |
| 1674 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 1675 | return new (C) TranslationUnitDecl(C); |
| 1676 | } |
| 1677 | |
| 1678 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 1679 | SourceLocation L, IdentifierInfo *Id) { |
| 1680 | return new (C) NamespaceDecl(DC, L, Id); |
| 1681 | } |
| 1682 | |
| 1683 | void NamespaceDecl::Destroy(ASTContext& C) { |
| 1684 | // NamespaceDecl uses "NextDeclarator" to chain namespace declarations |
| 1685 | // together. They are all top-level Decls. |
| 1686 | |
| 1687 | this->~NamespaceDecl(); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1688 | Decl::Destroy(C); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | |
| 1692 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
| 1693 | SourceLocation L, IdentifierInfo *Id, QualType T) { |
| 1694 | return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); |
| 1695 | } |
| 1696 | |
| 1697 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
| 1698 | SourceLocation L, |
| 1699 | DeclarationName N, QualType T, |
| 1700 | TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1701 | StorageClass S, StorageClass SCAsWritten, |
| 1702 | bool isInline, bool hasWrittenPrototype) { |
| 1703 | FunctionDecl *New = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, |
| 1704 | S, SCAsWritten, isInline); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1705 | New->HasWrittenPrototype = hasWrittenPrototype; |
| 1706 | return New; |
| 1707 | } |
| 1708 | |
| 1709 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| 1710 | return new (C) BlockDecl(DC, L); |
| 1711 | } |
| 1712 | |
| 1713 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 1714 | SourceLocation L, |
| 1715 | IdentifierInfo *Id, QualType T, |
| 1716 | Expr *E, const llvm::APSInt &V) { |
| 1717 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
| 1718 | } |
| 1719 | |
| 1720 | void EnumConstantDecl::Destroy(ASTContext& C) { |
| 1721 | if (Init) Init->Destroy(C); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1722 | ValueDecl::Destroy(C); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
| 1726 | SourceLocation L, IdentifierInfo *Id, |
| 1727 | TypeSourceInfo *TInfo) { |
| 1728 | return new (C) TypedefDecl(DC, L, Id, TInfo); |
| 1729 | } |
| 1730 | |
| 1731 | // Anchor TypedefDecl's vtable here. |
| 1732 | TypedefDecl::~TypedefDecl() {} |
| 1733 | |
| 1734 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
| 1735 | SourceLocation L, |
| 1736 | StringLiteral *Str) { |
| 1737 | return new (C) FileScopeAsmDecl(DC, L, Str); |
| 1738 | } |