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