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 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame^] | 569 | void |
| 570 | QualifierInfo::setTemplateParameterListsInfo(unsigned NumTPLists, |
| 571 | TemplateParameterList **TPLists) { |
| 572 | assert((NumTPLists == 0 || TPLists != 0) && |
| 573 | "Empty array of template parameters with positive size!"); |
| 574 | assert((NumTPLists == 0 || NNS) && |
| 575 | "Nonempty array of template parameters with no qualifier!"); |
| 576 | |
| 577 | // Free previous template parameters (if any). |
| 578 | if (NumTemplParamLists > 0) { |
| 579 | delete[] TemplParamLists; |
| 580 | TemplParamLists = 0; |
| 581 | NumTemplParamLists = 0; |
| 582 | } |
| 583 | // Set info on matched template parameter lists (if any). |
| 584 | if (NumTPLists > 0) { |
| 585 | TemplParamLists = new TemplateParameterList*[NumTPLists]; |
| 586 | NumTemplParamLists = NumTPLists; |
| 587 | for (unsigned i = NumTPLists; i-- > 0; ) |
| 588 | TemplParamLists[i] = TPLists[i]; |
| 589 | } |
| 590 | } |
| 591 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 592 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 593 | // VarDecl Implementation |
| 594 | //===----------------------------------------------------------------------===// |
| 595 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 596 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 597 | switch (SC) { |
| 598 | case VarDecl::None: break; |
| 599 | case VarDecl::Auto: return "auto"; break; |
| 600 | case VarDecl::Extern: return "extern"; break; |
| 601 | case VarDecl::PrivateExtern: return "__private_extern__"; break; |
| 602 | case VarDecl::Register: return "register"; break; |
| 603 | case VarDecl::Static: return "static"; break; |
| 604 | } |
| 605 | |
| 606 | assert(0 && "Invalid storage class"); |
| 607 | return 0; |
| 608 | } |
| 609 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 610 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 611 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 612 | StorageClass S, StorageClass SCAsWritten) { |
| 613 | return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | void VarDecl::Destroy(ASTContext& C) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 617 | Expr *Init = getInit(); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 618 | if (Init) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 619 | Init->Destroy(C); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 620 | if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) { |
| 621 | Eval->~EvaluatedStmt(); |
| 622 | C.Deallocate(Eval); |
| 623 | } |
| 624 | } |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 625 | this->~VarDecl(); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 626 | DeclaratorDecl::Destroy(C); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | VarDecl::~VarDecl() { |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 632 | SourceRange VarDecl::getSourceRange() const { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 633 | SourceLocation Start = getTypeSpecStartLoc(); |
| 634 | if (Start.isInvalid()) |
| 635 | Start = getLocation(); |
| 636 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 637 | if (getInit()) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 638 | return SourceRange(Start, getInit()->getLocEnd()); |
| 639 | return SourceRange(Start, getLocation()); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 642 | bool VarDecl::isExternC() const { |
| 643 | ASTContext &Context = getASTContext(); |
| 644 | if (!Context.getLangOptions().CPlusPlus) |
| 645 | return (getDeclContext()->isTranslationUnit() && |
| 646 | getStorageClass() != Static) || |
| 647 | (getDeclContext()->isFunctionOrMethod() && hasExternalStorage()); |
| 648 | |
| 649 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
| 650 | DC = DC->getParent()) { |
| 651 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 652 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
| 653 | return getStorageClass() != Static; |
| 654 | |
| 655 | break; |
| 656 | } |
| 657 | |
| 658 | if (DC->isFunctionOrMethod()) |
| 659 | return false; |
| 660 | } |
| 661 | |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | VarDecl *VarDecl::getCanonicalDecl() { |
| 666 | return getFirstDeclaration(); |
| 667 | } |
| 668 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 669 | VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const { |
| 670 | // C++ [basic.def]p2: |
| 671 | // A declaration is a definition unless [...] it contains the 'extern' |
| 672 | // specifier or a linkage-specification and neither an initializer [...], |
| 673 | // it declares a static data member in a class declaration [...]. |
| 674 | // C++ [temp.expl.spec]p15: |
| 675 | // An explicit specialization of a static data member of a template is a |
| 676 | // definition if the declaration includes an initializer; otherwise, it is |
| 677 | // a declaration. |
| 678 | if (isStaticDataMember()) { |
| 679 | if (isOutOfLine() && (hasInit() || |
| 680 | getTemplateSpecializationKind() != TSK_ExplicitSpecialization)) |
| 681 | return Definition; |
| 682 | else |
| 683 | return DeclarationOnly; |
| 684 | } |
| 685 | // C99 6.7p5: |
| 686 | // A definition of an identifier is a declaration for that identifier that |
| 687 | // [...] causes storage to be reserved for that object. |
| 688 | // Note: that applies for all non-file-scope objects. |
| 689 | // C99 6.9.2p1: |
| 690 | // If the declaration of an identifier for an object has file scope and an |
| 691 | // initializer, the declaration is an external definition for the identifier |
| 692 | if (hasInit()) |
| 693 | return Definition; |
| 694 | // AST for 'extern "C" int foo;' is annotated with 'extern'. |
| 695 | if (hasExternalStorage()) |
| 696 | return DeclarationOnly; |
| 697 | |
| 698 | // C99 6.9.2p2: |
| 699 | // A declaration of an object that has file scope without an initializer, |
| 700 | // and without a storage class specifier or the scs 'static', constitutes |
| 701 | // a tentative definition. |
| 702 | // No such thing in C++. |
| 703 | if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl()) |
| 704 | return TentativeDefinition; |
| 705 | |
| 706 | // What's left is (in C, block-scope) declarations without initializers or |
| 707 | // external storage. These are definitions. |
| 708 | return Definition; |
| 709 | } |
| 710 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 711 | VarDecl *VarDecl::getActingDefinition() { |
| 712 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 713 | if (Kind != TentativeDefinition) |
| 714 | return 0; |
| 715 | |
| 716 | VarDecl *LastTentative = false; |
| 717 | VarDecl *First = getFirstDeclaration(); |
| 718 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 719 | I != E; ++I) { |
| 720 | Kind = (*I)->isThisDeclarationADefinition(); |
| 721 | if (Kind == Definition) |
| 722 | return 0; |
| 723 | else if (Kind == TentativeDefinition) |
| 724 | LastTentative = *I; |
| 725 | } |
| 726 | return LastTentative; |
| 727 | } |
| 728 | |
| 729 | bool VarDecl::isTentativeDefinitionNow() const { |
| 730 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 731 | if (Kind != TentativeDefinition) |
| 732 | return false; |
| 733 | |
| 734 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 735 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 736 | return false; |
| 737 | } |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 738 | return true; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 741 | VarDecl *VarDecl::getDefinition() { |
Sebastian Redl | e2c52d2 | 2010-02-02 17:55:12 +0000 | [diff] [blame] | 742 | VarDecl *First = getFirstDeclaration(); |
| 743 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 744 | I != E; ++I) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 745 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 746 | return *I; |
| 747 | } |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 752 | redecl_iterator I = redecls_begin(), E = redecls_end(); |
| 753 | while (I != E && !I->getInit()) |
| 754 | ++I; |
| 755 | |
| 756 | if (I != E) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 757 | D = *I; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 758 | return I->getInit(); |
| 759 | } |
| 760 | return 0; |
| 761 | } |
| 762 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 763 | bool VarDecl::isOutOfLine() const { |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 764 | if (Decl::isOutOfLine()) |
| 765 | return true; |
Chandler Carruth | 8761d68 | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 766 | |
| 767 | if (!isStaticDataMember()) |
| 768 | return false; |
| 769 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 770 | // If this static data member was instantiated from a static data member of |
| 771 | // a class template, check whether that static data member was defined |
| 772 | // out-of-line. |
| 773 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 774 | return VD->isOutOfLine(); |
| 775 | |
| 776 | return false; |
| 777 | } |
| 778 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 779 | VarDecl *VarDecl::getOutOfLineDefinition() { |
| 780 | if (!isStaticDataMember()) |
| 781 | return 0; |
| 782 | |
| 783 | for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); |
| 784 | RD != RDEnd; ++RD) { |
| 785 | if (RD->getLexicalDeclContext()->isFileContext()) |
| 786 | return *RD; |
| 787 | } |
| 788 | |
| 789 | return 0; |
| 790 | } |
| 791 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 792 | void VarDecl::setInit(Expr *I) { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 793 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| 794 | Eval->~EvaluatedStmt(); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 795 | getASTContext().Deallocate(Eval); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | Init = I; |
| 799 | } |
| 800 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 801 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 802 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 803 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
| 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 808 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 809 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 810 | return MSI->getTemplateSpecializationKind(); |
| 811 | |
| 812 | return TSK_Undeclared; |
| 813 | } |
| 814 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 815 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 816 | return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 817 | } |
| 818 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 819 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 820 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 821 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 822 | assert(MSI && "Not an instantiated static data member?"); |
| 823 | MSI->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 824 | if (TSK != TSK_ExplicitSpecialization && |
| 825 | PointOfInstantiation.isValid() && |
| 826 | MSI->getPointOfInstantiation().isInvalid()) |
| 827 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 830 | //===----------------------------------------------------------------------===// |
| 831 | // ParmVarDecl Implementation |
| 832 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 833 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 834 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
| 835 | SourceLocation L, IdentifierInfo *Id, |
| 836 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 837 | StorageClass S, StorageClass SCAsWritten, |
| 838 | Expr *DefArg) { |
| 839 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, |
| 840 | S, SCAsWritten, DefArg); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 843 | Expr *ParmVarDecl::getDefaultArg() { |
| 844 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 845 | assert(!hasUninstantiatedDefaultArg() && |
| 846 | "Default argument is not yet instantiated!"); |
| 847 | |
| 848 | Expr *Arg = getInit(); |
| 849 | if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg)) |
| 850 | return E->getSubExpr(); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 851 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 852 | return Arg; |
| 853 | } |
| 854 | |
| 855 | unsigned ParmVarDecl::getNumDefaultArgTemporaries() const { |
| 856 | if (const CXXExprWithTemporaries *E = |
| 857 | dyn_cast<CXXExprWithTemporaries>(getInit())) |
| 858 | return E->getNumTemporaries(); |
| 859 | |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 860 | return 0; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 863 | CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) { |
| 864 | assert(getNumDefaultArgTemporaries() && |
| 865 | "Default arguments does not have any temporaries!"); |
| 866 | |
| 867 | CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit()); |
| 868 | return E->getTemporary(i); |
| 869 | } |
| 870 | |
| 871 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 872 | if (const Expr *E = getInit()) |
| 873 | return E->getSourceRange(); |
| 874 | |
| 875 | if (hasUninstantiatedDefaultArg()) |
| 876 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 877 | |
| 878 | return SourceRange(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 881 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 882 | // FunctionDecl Implementation |
| 883 | //===----------------------------------------------------------------------===// |
| 884 | |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 885 | void FunctionDecl::Destroy(ASTContext& C) { |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 886 | if (Body && Body.isOffset()) |
| 887 | Body.get(C.getExternalSource())->Destroy(C); |
Ted Kremenek | b65cf41 | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 888 | |
| 889 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 890 | (*I)->Destroy(C); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 891 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 892 | FunctionTemplateSpecializationInfo *FTSInfo |
| 893 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
| 894 | if (FTSInfo) |
| 895 | C.Deallocate(FTSInfo); |
| 896 | |
| 897 | MemberSpecializationInfo *MSInfo |
| 898 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 899 | if (MSInfo) |
| 900 | C.Deallocate(MSInfo); |
| 901 | |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 902 | C.Deallocate(ParamInfo); |
Nuno Lopes | 460b0ac | 2009-01-18 19:57:27 +0000 | [diff] [blame] | 903 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 904 | DeclaratorDecl::Destroy(C); |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 905 | } |
| 906 | |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 907 | void FunctionDecl::getNameForDiagnostic(std::string &S, |
| 908 | const PrintingPolicy &Policy, |
| 909 | bool Qualified) const { |
| 910 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 911 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 912 | if (TemplateArgs) |
| 913 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 914 | TemplateArgs->getFlatArgumentList(), |
| 915 | TemplateArgs->flat_size(), |
| 916 | Policy); |
| 917 | |
| 918 | } |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 919 | |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 920 | bool FunctionDecl::isVariadic() const { |
| 921 | if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>()) |
| 922 | return FT->isVariadic(); |
| 923 | return false; |
| 924 | } |
| 925 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 926 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 927 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 928 | if (I->Body) { |
| 929 | Definition = *I; |
| 930 | return I->Body.get(getASTContext().getExternalSource()); |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
| 934 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 937 | void FunctionDecl::setBody(Stmt *B) { |
| 938 | Body = B; |
Argyrios Kyrtzidis | 1a5364e | 2009-06-22 17:13:31 +0000 | [diff] [blame] | 939 | if (B) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 940 | EndRangeLoc = B->getLocEnd(); |
| 941 | } |
| 942 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 943 | bool FunctionDecl::isMain() const { |
| 944 | ASTContext &Context = getASTContext(); |
John McCall | 07a5c22 | 2009-08-15 02:09:25 +0000 | [diff] [blame] | 945 | return !Context.getLangOptions().Freestanding && |
| 946 | getDeclContext()->getLookupContext()->isTranslationUnit() && |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 947 | getIdentifier() && getIdentifier()->isStr("main"); |
| 948 | } |
| 949 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 950 | bool FunctionDecl::isExternC() const { |
| 951 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 952 | // In C, any non-static, non-overloadable function has external |
| 953 | // linkage. |
| 954 | if (!Context.getLangOptions().CPlusPlus) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 955 | return getStorageClass() != Static && !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 956 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 958 | DC = DC->getParent()) { |
| 959 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 960 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | return getStorageClass() != Static && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 962 | !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 963 | |
| 964 | break; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | return false; |
| 969 | } |
| 970 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 971 | bool FunctionDecl::isGlobal() const { |
| 972 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 973 | return Method->isStatic(); |
| 974 | |
| 975 | if (getStorageClass() == Static) |
| 976 | return false; |
| 977 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 979 | DC->isNamespace(); |
| 980 | DC = DC->getParent()) { |
| 981 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 982 | if (!Namespace->getDeclName()) |
| 983 | return false; |
| 984 | break; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | return true; |
| 989 | } |
| 990 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 991 | void |
| 992 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| 993 | redeclarable_base::setPreviousDeclaration(PrevDecl); |
| 994 | |
| 995 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 996 | FunctionTemplateDecl *PrevFunTmpl |
| 997 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 998 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 999 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | const FunctionDecl *FunctionDecl::getCanonicalDecl() const { |
| 1004 | return getFirstDeclaration(); |
| 1005 | } |
| 1006 | |
| 1007 | FunctionDecl *FunctionDecl::getCanonicalDecl() { |
| 1008 | return getFirstDeclaration(); |
| 1009 | } |
| 1010 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1011 | /// \brief Returns a value indicating whether this function |
| 1012 | /// corresponds to a builtin function. |
| 1013 | /// |
| 1014 | /// The function corresponds to a built-in function if it is |
| 1015 | /// declared at translation scope or within an extern "C" block and |
| 1016 | /// its name matches with the name of a builtin. The returned value |
| 1017 | /// 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] | 1018 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1019 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1020 | unsigned FunctionDecl::getBuiltinID() const { |
| 1021 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1022 | if (!getIdentifier() || !getIdentifier()->getBuiltinID()) |
| 1023 | return 0; |
| 1024 | |
| 1025 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| 1026 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 1027 | return BuiltinID; |
| 1028 | |
| 1029 | // This function has the name of a known C library |
| 1030 | // function. Determine whether it actually refers to the C library |
| 1031 | // function or whether it just has the same name. |
| 1032 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1033 | // If this is a static function, it's not a builtin. |
| 1034 | if (getStorageClass() == Static) |
| 1035 | return 0; |
| 1036 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1037 | // If this function is at translation-unit scope and we're not in |
| 1038 | // C++, it refers to the C library function. |
| 1039 | if (!Context.getLangOptions().CPlusPlus && |
| 1040 | getDeclContext()->isTranslationUnit()) |
| 1041 | return BuiltinID; |
| 1042 | |
| 1043 | // If the function is in an extern "C" linkage specification and is |
| 1044 | // not marked "overloadable", it's the real function. |
| 1045 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1047 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1048 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1049 | return BuiltinID; |
| 1050 | |
| 1051 | // Not a builtin |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1056 | /// getNumParams - Return the number of parameters this function must have |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 1057 | /// based on its FunctionType. This is the length of the PararmInfo array |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1058 | /// after it has been created. |
| 1059 | unsigned FunctionDecl::getNumParams() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1060 | const FunctionType *FT = getType()->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1061 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 1062 | return 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1063 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1067 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1068 | assert(ParamInfo == 0 && "Already has param info!"); |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 1069 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1071 | // Zero params -> null pointer. |
| 1072 | if (NumParams) { |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1073 | void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 1074 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1075 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1076 | |
Argyrios Kyrtzidis | 96888cc | 2009-06-23 00:42:00 +0000 | [diff] [blame] | 1077 | // Update source range. The check below allows us to set EndRangeLoc before |
| 1078 | // setting the parameters. |
Argyrios Kyrtzidis | cb5f8f5 | 2009-06-23 00:42:15 +0000 | [diff] [blame] | 1079 | if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1080 | EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1084 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 1085 | /// needed to call this function. This may be fewer than the number of |
| 1086 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 1087 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1088 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 1089 | unsigned NumRequiredArgs = getNumParams(); |
| 1090 | while (NumRequiredArgs > 0 |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 1091 | && getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1092 | --NumRequiredArgs; |
| 1093 | |
| 1094 | return NumRequiredArgs; |
| 1095 | } |
| 1096 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1097 | bool FunctionDecl::isInlined() const { |
Anders Carlsson | 48eda2c | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 1098 | // FIXME: This is not enough. Consider: |
| 1099 | // |
| 1100 | // inline void f(); |
| 1101 | // void f() { } |
| 1102 | // |
| 1103 | // f is inlined, but does not have inline specified. |
| 1104 | // To fix this we should add an 'inline' flag to FunctionDecl. |
| 1105 | if (isInlineSpecified()) |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1106 | return true; |
Anders Carlsson | 48eda2c | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 1107 | |
| 1108 | if (isa<CXXMethodDecl>(this)) { |
| 1109 | if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified()) |
| 1110 | return true; |
| 1111 | } |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1112 | |
| 1113 | switch (getTemplateSpecializationKind()) { |
| 1114 | case TSK_Undeclared: |
| 1115 | case TSK_ExplicitSpecialization: |
| 1116 | return false; |
| 1117 | |
| 1118 | case TSK_ImplicitInstantiation: |
| 1119 | case TSK_ExplicitInstantiationDeclaration: |
| 1120 | case TSK_ExplicitInstantiationDefinition: |
| 1121 | // Handle below. |
| 1122 | break; |
| 1123 | } |
| 1124 | |
| 1125 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
| 1126 | Stmt *Pattern = 0; |
| 1127 | if (PatternDecl) |
| 1128 | Pattern = PatternDecl->getBody(PatternDecl); |
| 1129 | |
| 1130 | if (Pattern && PatternDecl) |
| 1131 | return PatternDecl->isInlined(); |
| 1132 | |
| 1133 | return false; |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1136 | /// \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] | 1137 | /// definition will be externally visible. |
| 1138 | /// |
| 1139 | /// Inline function definitions are always available for inlining optimizations. |
| 1140 | /// However, depending on the language dialect, declaration specifiers, and |
| 1141 | /// attributes, the definition of an inline function may or may not be |
| 1142 | /// "externally" visible to other translation units in the program. |
| 1143 | /// |
| 1144 | /// In C99, inline definitions are not externally visible by default. However, |
Mike Stump | 1e5fd7f | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 1145 | /// 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] | 1146 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 1147 | /// |
| 1148 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 1149 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 1150 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 1151 | /// an externally visible symbol, but "extern inline" will not create an |
| 1152 | /// externally visible symbol. |
| 1153 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
| 1154 | assert(isThisDeclarationADefinition() && "Must have the function definition"); |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1155 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1156 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1157 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1158 | if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) { |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1159 | // GNU inline semantics. Based on a number of examples, we came up with the |
| 1160 | // following heuristic: if the "inline" keyword is present on a |
| 1161 | // declaration of the function but "extern" is not present on that |
| 1162 | // declaration, then the symbol is externally visible. Otherwise, the GNU |
| 1163 | // "extern inline" semantics applies and the symbol is not externally |
| 1164 | // visible. |
| 1165 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 1166 | Redecl != RedeclEnd; |
| 1167 | ++Redecl) { |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1168 | if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1169 | return true; |
| 1170 | } |
| 1171 | |
| 1172 | // GNU "extern inline" semantics; no externally visible symbol. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 1173 | return false; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | // C99 6.7.4p6: |
| 1177 | // [...] If all of the file scope declarations for a function in a |
| 1178 | // translation unit include the inline function specifier without extern, |
| 1179 | // then the definition in that translation unit is an inline definition. |
| 1180 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 1181 | Redecl != RedeclEnd; |
| 1182 | ++Redecl) { |
| 1183 | // Only consider file-scope declarations in this test. |
| 1184 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 1185 | continue; |
| 1186 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1187 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1188 | return true; // Not an inline definition |
| 1189 | } |
| 1190 | |
| 1191 | // C99 6.7.4p6: |
| 1192 | // An inline definition does not provide an external definition for the |
| 1193 | // function, and does not forbid an external definition in another |
| 1194 | // translation unit. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 1195 | return false; |
| 1196 | } |
| 1197 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1198 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 1199 | /// function represents, if any. |
| 1200 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1201 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 1202 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1203 | else |
| 1204 | return OO_None; |
| 1205 | } |
| 1206 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 1207 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 1208 | /// represents, if any. |
| 1209 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 1210 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 1211 | return getDeclName().getCXXLiteralIdentifier(); |
| 1212 | else |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1216 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1217 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1218 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| 1219 | |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1223 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 1224 | return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 1225 | } |
| 1226 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1227 | void |
| 1228 | FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD, |
| 1229 | TemplateSpecializationKind TSK) { |
| 1230 | assert(TemplateOrSpecialization.isNull() && |
| 1231 | "Member function is already a specialization"); |
| 1232 | MemberSpecializationInfo *Info |
| 1233 | = new (getASTContext()) MemberSpecializationInfo(FD, TSK); |
| 1234 | TemplateOrSpecialization = Info; |
| 1235 | } |
| 1236 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1237 | bool FunctionDecl::isImplicitlyInstantiable() const { |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 1238 | // If the function is invalid, it can't be implicitly instantiated. |
| 1239 | if (isInvalidDecl()) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1240 | return false; |
| 1241 | |
| 1242 | switch (getTemplateSpecializationKind()) { |
| 1243 | case TSK_Undeclared: |
| 1244 | case TSK_ExplicitSpecialization: |
| 1245 | case TSK_ExplicitInstantiationDefinition: |
| 1246 | return false; |
| 1247 | |
| 1248 | case TSK_ImplicitInstantiation: |
| 1249 | return true; |
| 1250 | |
| 1251 | case TSK_ExplicitInstantiationDeclaration: |
| 1252 | // Handled below. |
| 1253 | break; |
| 1254 | } |
| 1255 | |
| 1256 | // Find the actual template from which we will instantiate. |
| 1257 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
| 1258 | Stmt *Pattern = 0; |
| 1259 | if (PatternDecl) |
| 1260 | Pattern = PatternDecl->getBody(PatternDecl); |
| 1261 | |
| 1262 | // C++0x [temp.explicit]p9: |
| 1263 | // Except for inline functions, other explicit instantiation declarations |
| 1264 | // have the effect of suppressing the implicit instantiation of the entity |
| 1265 | // to which they refer. |
| 1266 | if (!Pattern || !PatternDecl) |
| 1267 | return true; |
| 1268 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1269 | return PatternDecl->isInlined(); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
| 1273 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 1274 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 1275 | // If we have hit a point where the user provided a specialization of |
| 1276 | // this template, we're done looking. |
| 1277 | if (Primary->isMemberSpecialization()) |
| 1278 | break; |
| 1279 | |
| 1280 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 1281 | } |
| 1282 | |
| 1283 | return Primary->getTemplatedDecl(); |
| 1284 | } |
| 1285 | |
| 1286 | return getInstantiatedFromMemberFunction(); |
| 1287 | } |
| 1288 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1289 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1291 | = TemplateOrSpecialization |
| 1292 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1293 | return Info->Template.getPointer(); |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1294 | } |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | const TemplateArgumentList * |
| 1299 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1301 | = TemplateOrSpecialization |
| 1302 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1303 | return Info->TemplateArguments; |
| 1304 | } |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 1308 | const TemplateArgumentListInfo * |
| 1309 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 1310 | if (FunctionTemplateSpecializationInfo *Info |
| 1311 | = TemplateOrSpecialization |
| 1312 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 1313 | return Info->TemplateArgumentsAsWritten; |
| 1314 | } |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1318 | void |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1319 | FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1320 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1321 | void *InsertPos, |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 1322 | TemplateSpecializationKind TSK, |
| 1323 | const TemplateArgumentListInfo *TemplateArgsAsWritten) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1324 | assert(TSK != TSK_Undeclared && |
| 1325 | "Must specify the type of function template specialization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1326 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1327 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1328 | if (!Info) |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1329 | Info = new (getASTContext()) FunctionTemplateSpecializationInfo; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1331 | Info->Function = this; |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1332 | Info->Template.setPointer(Template); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1333 | Info->Template.setInt(TSK - 1); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1334 | Info->TemplateArguments = TemplateArgs; |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 1335 | Info->TemplateArgumentsAsWritten = TemplateArgsAsWritten; |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1336 | TemplateOrSpecialization = Info; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1338 | // Insert this function template specialization into the set of known |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1339 | // function template specializations. |
| 1340 | if (InsertPos) |
| 1341 | Template->getSpecializations().InsertNode(Info, InsertPos); |
| 1342 | else { |
| 1343 | // Try to insert the new node. If there is an existing node, remove it |
| 1344 | // first. |
| 1345 | FunctionTemplateSpecializationInfo *Existing |
| 1346 | = Template->getSpecializations().GetOrInsertNode(Info); |
| 1347 | if (Existing) { |
| 1348 | Template->getSpecializations().RemoveNode(Existing); |
| 1349 | Template->getSpecializations().GetOrInsertNode(Info); |
| 1350 | } |
| 1351 | } |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1354 | void |
| 1355 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 1356 | const UnresolvedSetImpl &Templates, |
| 1357 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1358 | assert(TemplateOrSpecialization.isNull()); |
| 1359 | size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo); |
| 1360 | Size += Templates.size() * sizeof(FunctionTemplateDecl*); |
John McCall | 21c0160 | 2010-04-13 22:18:28 +0000 | [diff] [blame] | 1361 | Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1362 | void *Buffer = Context.Allocate(Size); |
| 1363 | DependentFunctionTemplateSpecializationInfo *Info = |
| 1364 | new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates, |
| 1365 | TemplateArgs); |
| 1366 | TemplateOrSpecialization = Info; |
| 1367 | } |
| 1368 | |
| 1369 | DependentFunctionTemplateSpecializationInfo:: |
| 1370 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 1371 | const TemplateArgumentListInfo &TArgs) |
| 1372 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
| 1373 | |
| 1374 | d.NumTemplates = Ts.size(); |
| 1375 | d.NumArgs = TArgs.size(); |
| 1376 | |
| 1377 | FunctionTemplateDecl **TsArray = |
| 1378 | const_cast<FunctionTemplateDecl**>(getTemplates()); |
| 1379 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 1380 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 1381 | |
| 1382 | TemplateArgumentLoc *ArgsArray = |
| 1383 | const_cast<TemplateArgumentLoc*>(getTemplateArgs()); |
| 1384 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 1385 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 1386 | } |
| 1387 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1388 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1389 | // For a function template specialization, query the specialization |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1390 | // information object. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1391 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1392 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1393 | if (FTSInfo) |
| 1394 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1396 | MemberSpecializationInfo *MSInfo |
| 1397 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 1398 | if (MSInfo) |
| 1399 | return MSInfo->getTemplateSpecializationKind(); |
| 1400 | |
| 1401 | return TSK_Undeclared; |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | void |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1405 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 1406 | SourceLocation PointOfInstantiation) { |
| 1407 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 1408 | = TemplateOrSpecialization.dyn_cast< |
| 1409 | FunctionTemplateSpecializationInfo*>()) { |
| 1410 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 1411 | if (TSK != TSK_ExplicitSpecialization && |
| 1412 | PointOfInstantiation.isValid() && |
| 1413 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 1414 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1415 | } else if (MemberSpecializationInfo *MSInfo |
| 1416 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 1417 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1418 | if (TSK != TSK_ExplicitSpecialization && |
| 1419 | PointOfInstantiation.isValid() && |
| 1420 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 1421 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1422 | } else |
| 1423 | assert(false && "Function cannot have a template specialization kind"); |
| 1424 | } |
| 1425 | |
| 1426 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1427 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 1428 | = TemplateOrSpecialization.dyn_cast< |
| 1429 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1430 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1431 | else if (MemberSpecializationInfo *MSInfo |
| 1432 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1433 | return MSInfo->getPointOfInstantiation(); |
| 1434 | |
| 1435 | return SourceLocation(); |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1438 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1439 | if (Decl::isOutOfLine()) |
| 1440 | return true; |
| 1441 | |
| 1442 | // If this function was instantiated from a member function of a |
| 1443 | // class template, check whether that member function was defined out-of-line. |
| 1444 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 1445 | const FunctionDecl *Definition; |
| 1446 | if (FD->getBody(Definition)) |
| 1447 | return Definition->isOutOfLine(); |
| 1448 | } |
| 1449 | |
| 1450 | // If this function was instantiated from a function template, |
| 1451 | // check whether that function template was defined out-of-line. |
| 1452 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 1453 | const FunctionDecl *Definition; |
| 1454 | if (FunTmpl->getTemplatedDecl()->getBody(Definition)) |
| 1455 | return Definition->isOutOfLine(); |
| 1456 | } |
| 1457 | |
| 1458 | return false; |
| 1459 | } |
| 1460 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1461 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1462 | // FieldDecl Implementation |
| 1463 | //===----------------------------------------------------------------------===// |
| 1464 | |
| 1465 | FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 1466 | IdentifierInfo *Id, QualType T, |
| 1467 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable) { |
| 1468 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable); |
| 1469 | } |
| 1470 | |
| 1471 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 1472 | if (!isImplicit() || getDeclName()) |
| 1473 | return false; |
| 1474 | |
| 1475 | if (const RecordType *Record = getType()->getAs<RecordType>()) |
| 1476 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 1477 | |
| 1478 | return false; |
| 1479 | } |
| 1480 | |
| 1481 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1482 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1483 | //===----------------------------------------------------------------------===// |
| 1484 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1485 | void TagDecl::Destroy(ASTContext &C) { |
| 1486 | if (hasExtInfo()) |
| 1487 | C.Deallocate(getExtInfo()); |
| 1488 | TypeDecl::Destroy(C); |
| 1489 | } |
| 1490 | |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 1491 | SourceRange TagDecl::getSourceRange() const { |
| 1492 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 1493 | return SourceRange(TagKeywordLoc, E); |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 1496 | TagDecl* TagDecl::getCanonicalDecl() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1497 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1500 | void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) { |
| 1501 | TypedefDeclOrQualifier = TDD; |
| 1502 | if (TypeForDecl) |
| 1503 | TypeForDecl->ClearLinkageCache(); |
| 1504 | } |
| 1505 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1506 | void TagDecl::startDefinition() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1507 | if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { |
| 1508 | TagT->decl.setPointer(this); |
| 1509 | TagT->decl.setInt(1); |
Douglas Gregor | 9ffce21 | 2010-04-30 04:39:27 +0000 | [diff] [blame] | 1510 | } else if (InjectedClassNameType *Injected |
| 1511 | = const_cast<InjectedClassNameType *>( |
| 1512 | TypeForDecl->getAs<InjectedClassNameType>())) { |
| 1513 | Injected->Decl = cast<CXXRecordDecl>(this); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1514 | } |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1515 | |
| 1516 | if (isa<CXXRecordDecl>(this)) { |
| 1517 | CXXRecordDecl *D = cast<CXXRecordDecl>(this); |
| 1518 | struct CXXRecordDecl::DefinitionData *Data = |
| 1519 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
John McCall | 2243288 | 2010-03-26 21:56:38 +0000 | [diff] [blame] | 1520 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 1521 | cast<CXXRecordDecl>(*I)->DefinitionData = Data; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1522 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | void TagDecl::completeDefinition() { |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 1526 | assert((!isa<CXXRecordDecl>(this) || |
| 1527 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 1528 | "definition completed but not started"); |
| 1529 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1530 | IsDefinition = true; |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1531 | if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) { |
| 1532 | assert(TagT->decl.getPointer() == this && |
| 1533 | "Attempt to redefine a tag definition?"); |
| 1534 | TagT->decl.setInt(0); |
Douglas Gregor | 9ffce21 | 2010-04-30 04:39:27 +0000 | [diff] [blame] | 1535 | } else if (InjectedClassNameType *Injected |
| 1536 | = const_cast<InjectedClassNameType *>( |
| 1537 | TypeForDecl->getAs<InjectedClassNameType>())) { |
| 1538 | assert(Injected->Decl == this && |
| 1539 | "Attempt to redefine a class template definition?"); |
Chandler Carruth | a842697 | 2010-05-06 05:28:42 +0000 | [diff] [blame] | 1540 | (void)Injected; |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1541 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1544 | TagDecl* TagDecl::getDefinition() const { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1545 | if (isDefinition()) |
| 1546 | return const_cast<TagDecl *>(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | |
| 1548 | for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1549 | R != REnd; ++R) |
| 1550 | if (R->isDefinition()) |
| 1551 | return *R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1553 | return 0; |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1556 | void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier, |
| 1557 | SourceRange QualifierRange) { |
| 1558 | if (Qualifier) { |
| 1559 | // Make sure the extended qualifier info is allocated. |
| 1560 | if (!hasExtInfo()) |
| 1561 | TypedefDeclOrQualifier = new (getASTContext()) ExtInfo; |
| 1562 | // Set qualifier info. |
| 1563 | getExtInfo()->NNS = Qualifier; |
| 1564 | getExtInfo()->NNSRange = QualifierRange; |
| 1565 | } |
| 1566 | else { |
| 1567 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| 1568 | assert(QualifierRange.isInvalid()); |
| 1569 | if (hasExtInfo()) { |
| 1570 | getASTContext().Deallocate(getExtInfo()); |
| 1571 | TypedefDeclOrQualifier = (TypedefDecl*) 0; |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1576 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1577 | // EnumDecl Implementation |
| 1578 | //===----------------------------------------------------------------------===// |
| 1579 | |
| 1580 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 1581 | IdentifierInfo *Id, SourceLocation TKL, |
| 1582 | EnumDecl *PrevDecl) { |
| 1583 | EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL); |
| 1584 | C.getTypeDeclType(Enum, PrevDecl); |
| 1585 | return Enum; |
| 1586 | } |
| 1587 | |
| 1588 | void EnumDecl::Destroy(ASTContext& C) { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1589 | TagDecl::Destroy(C); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1592 | void EnumDecl::completeDefinition(QualType NewType, |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 1593 | QualType NewPromotionType, |
| 1594 | unsigned NumPositiveBits, |
| 1595 | unsigned NumNegativeBits) { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1596 | assert(!isDefinition() && "Cannot redefine enums!"); |
| 1597 | IntegerType = NewType; |
| 1598 | PromotionType = NewPromotionType; |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 1599 | setNumPositiveBits(NumPositiveBits); |
| 1600 | setNumNegativeBits(NumNegativeBits); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1601 | TagDecl::completeDefinition(); |
| 1602 | } |
| 1603 | |
| 1604 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1605 | // RecordDecl Implementation |
| 1606 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1607 | |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 1608 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1609 | IdentifierInfo *Id, RecordDecl *PrevDecl, |
| 1610 | SourceLocation TKL) |
| 1611 | : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1612 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1613 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1614 | HasObjectMember = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1615 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1619 | SourceLocation L, IdentifierInfo *Id, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 1620 | SourceLocation TKL, RecordDecl* PrevDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1622 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1623 | C.getTypeDeclType(R, PrevDecl); |
| 1624 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 1627 | RecordDecl::~RecordDecl() { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | void RecordDecl::Destroy(ASTContext& C) { |
Argyrios Kyrtzidis | 997b6c6 | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 1631 | TagDecl::Destroy(C); |
| 1632 | } |
| 1633 | |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 1634 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 1636 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 1637 | } |
| 1638 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1639 | /// completeDefinition - Notes that the definition of this type is now |
| 1640 | /// complete. |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1641 | void RecordDecl::completeDefinition() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1642 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1643 | TagDecl::completeDefinition(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
John McCall | bc365c5 | 2010-05-21 01:17:40 +0000 | [diff] [blame] | 1646 | ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() { |
| 1647 | // Force the decl chain to come into existence properly. |
| 1648 | if (!getNextDeclInContext()) getParent()->decls_begin(); |
| 1649 | |
| 1650 | assert(isAnonymousStructOrUnion()); |
| 1651 | ValueDecl *D = cast<ValueDecl>(getNextDeclInContext()); |
| 1652 | assert(D->getType()->isRecordType()); |
| 1653 | assert(D->getType()->getAs<RecordType>()->getDecl() == this); |
| 1654 | return D; |
| 1655 | } |
| 1656 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1657 | //===----------------------------------------------------------------------===// |
| 1658 | // BlockDecl Implementation |
| 1659 | //===----------------------------------------------------------------------===// |
| 1660 | |
| 1661 | BlockDecl::~BlockDecl() { |
| 1662 | } |
| 1663 | |
| 1664 | void BlockDecl::Destroy(ASTContext& C) { |
| 1665 | if (Body) |
| 1666 | Body->Destroy(C); |
| 1667 | |
| 1668 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 1669 | (*I)->Destroy(C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | |
| 1671 | C.Deallocate(ParamInfo); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1672 | Decl::Destroy(C); |
| 1673 | } |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1674 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1675 | void BlockDecl::setParams(ParmVarDecl **NewParamInfo, |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1676 | unsigned NParms) { |
| 1677 | assert(ParamInfo == 0 && "Already has param info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1679 | // Zero params -> null pointer. |
| 1680 | if (NParms) { |
| 1681 | NumParams = NParms; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1682 | void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1683 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
| 1684 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | unsigned BlockDecl::getNumParams() const { |
| 1689 | return NumParams; |
| 1690 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1691 | |
| 1692 | |
| 1693 | //===----------------------------------------------------------------------===// |
| 1694 | // Other Decl Allocation/Deallocation Method Implementations |
| 1695 | //===----------------------------------------------------------------------===// |
| 1696 | |
| 1697 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 1698 | return new (C) TranslationUnitDecl(C); |
| 1699 | } |
| 1700 | |
| 1701 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 1702 | SourceLocation L, IdentifierInfo *Id) { |
| 1703 | return new (C) NamespaceDecl(DC, L, Id); |
| 1704 | } |
| 1705 | |
| 1706 | void NamespaceDecl::Destroy(ASTContext& C) { |
| 1707 | // NamespaceDecl uses "NextDeclarator" to chain namespace declarations |
| 1708 | // together. They are all top-level Decls. |
| 1709 | |
| 1710 | this->~NamespaceDecl(); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1711 | Decl::Destroy(C); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | |
| 1715 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
| 1716 | SourceLocation L, IdentifierInfo *Id, QualType T) { |
| 1717 | return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); |
| 1718 | } |
| 1719 | |
| 1720 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
| 1721 | SourceLocation L, |
| 1722 | DeclarationName N, QualType T, |
| 1723 | TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1724 | StorageClass S, StorageClass SCAsWritten, |
| 1725 | bool isInline, bool hasWrittenPrototype) { |
| 1726 | FunctionDecl *New = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, |
| 1727 | S, SCAsWritten, isInline); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1728 | New->HasWrittenPrototype = hasWrittenPrototype; |
| 1729 | return New; |
| 1730 | } |
| 1731 | |
| 1732 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| 1733 | return new (C) BlockDecl(DC, L); |
| 1734 | } |
| 1735 | |
| 1736 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 1737 | SourceLocation L, |
| 1738 | IdentifierInfo *Id, QualType T, |
| 1739 | Expr *E, const llvm::APSInt &V) { |
| 1740 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
| 1741 | } |
| 1742 | |
| 1743 | void EnumConstantDecl::Destroy(ASTContext& C) { |
| 1744 | if (Init) Init->Destroy(C); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1745 | ValueDecl::Destroy(C); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
| 1749 | SourceLocation L, IdentifierInfo *Id, |
| 1750 | TypeSourceInfo *TInfo) { |
| 1751 | return new (C) TypedefDecl(DC, L, Id, TInfo); |
| 1752 | } |
| 1753 | |
| 1754 | // Anchor TypedefDecl's vtable here. |
| 1755 | TypedefDecl::~TypedefDecl() {} |
| 1756 | |
| 1757 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
| 1758 | SourceLocation L, |
| 1759 | StringLiteral *Str) { |
| 1760 | return new (C) FileScopeAsmDecl(DC, L, Str); |
| 1761 | } |