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