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" |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 24 | #include "clang/AST/ASTMutationListener.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 26 | #include "clang/Basic/IdentifierTable.h" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 27 | #include "clang/Basic/Specifiers.h" |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 29 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 33 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 36 | static const VisibilityAttr *GetExplicitVisibility(const Decl *d) { |
| 37 | // Use the most recent declaration of a variable. |
| 38 | if (const VarDecl *var = dyn_cast<VarDecl>(d)) |
| 39 | return var->getMostRecentDeclaration()->getAttr<VisibilityAttr>(); |
| 40 | |
| 41 | // Use the most recent declaration of a function, and also handle |
| 42 | // function template specializations. |
| 43 | if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(d)) { |
| 44 | if (const VisibilityAttr *attr |
| 45 | = fn->getMostRecentDeclaration()->getAttr<VisibilityAttr>()) |
| 46 | return attr; |
| 47 | |
| 48 | // If the function is a specialization of a template with an |
| 49 | // explicit visibility attribute, use that. |
| 50 | if (FunctionTemplateSpecializationInfo *templateInfo |
| 51 | = fn->getTemplateSpecializationInfo()) |
| 52 | return templateInfo->getTemplate()->getTemplatedDecl() |
| 53 | ->getAttr<VisibilityAttr>(); |
| 54 | |
| 55 | return 0; |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 56 | } |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 57 | |
| 58 | // Otherwise, just check the declaration itself first. |
| 59 | if (const VisibilityAttr *attr = d->getAttr<VisibilityAttr>()) |
| 60 | return attr; |
| 61 | |
| 62 | // If there wasn't explicit visibility there, and this is a |
| 63 | // specialization of a class template, check for visibility |
| 64 | // on the pattern. |
| 65 | if (const ClassTemplateSpecializationDecl *spec |
| 66 | = dyn_cast<ClassTemplateSpecializationDecl>(d)) |
| 67 | return spec->getSpecializedTemplate()->getTemplatedDecl() |
| 68 | ->getAttr<VisibilityAttr>(); |
| 69 | |
| 70 | return 0; |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 71 | } |
| 72 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 73 | static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) { |
| 74 | switch (A->getVisibility()) { |
| 75 | case VisibilityAttr::Default: |
| 76 | return DefaultVisibility; |
| 77 | case VisibilityAttr::Hidden: |
| 78 | return HiddenVisibility; |
| 79 | case VisibilityAttr::Protected: |
| 80 | return ProtectedVisibility; |
| 81 | } |
| 82 | return DefaultVisibility; |
| 83 | } |
| 84 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 85 | typedef NamedDecl::LinkageInfo LinkageInfo; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 86 | typedef std::pair<Linkage,Visibility> LVPair; |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 87 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 88 | static LVPair merge(LVPair L, LVPair R) { |
| 89 | return LVPair(minLinkage(L.first, R.first), |
| 90 | minVisibility(L.second, R.second)); |
| 91 | } |
| 92 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 93 | static LVPair merge(LVPair L, LinkageInfo R) { |
| 94 | return LVPair(minLinkage(L.first, R.linkage()), |
| 95 | minVisibility(L.second, R.visibility())); |
| 96 | } |
| 97 | |
Benjamin Kramer | 752c2e9 | 2010-11-05 19:56:37 +0000 | [diff] [blame] | 98 | namespace { |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 99 | /// Flags controlling the computation of linkage and visibility. |
| 100 | struct LVFlags { |
| 101 | bool ConsiderGlobalVisibility; |
| 102 | bool ConsiderVisibilityAttributes; |
| 103 | |
| 104 | LVFlags() : ConsiderGlobalVisibility(true), |
| 105 | ConsiderVisibilityAttributes(true) { |
| 106 | } |
| 107 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 108 | /// \brief Returns a set of flags that is only useful for computing the |
| 109 | /// linkage, not the visibility, of a declaration. |
| 110 | static LVFlags CreateOnlyDeclLinkage() { |
| 111 | LVFlags F; |
| 112 | F.ConsiderGlobalVisibility = false; |
| 113 | F.ConsiderVisibilityAttributes = false; |
| 114 | return F; |
| 115 | } |
| 116 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 117 | /// Returns a set of flags, otherwise based on these, which ignores |
| 118 | /// off all sources of visibility except template arguments. |
| 119 | LVFlags onlyTemplateVisibility() const { |
| 120 | LVFlags F = *this; |
| 121 | F.ConsiderGlobalVisibility = false; |
| 122 | F.ConsiderVisibilityAttributes = false; |
| 123 | return F; |
| 124 | } |
Douglas Gregor | 89d63e5 | 2010-12-06 18:50:56 +0000 | [diff] [blame] | 125 | }; |
Benjamin Kramer | 752c2e9 | 2010-11-05 19:56:37 +0000 | [diff] [blame] | 126 | } // end anonymous namespace |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 127 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 128 | /// \brief Get the most restrictive linkage for the types in the given |
| 129 | /// template parameter list. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 130 | static LVPair |
| 131 | getLVForTemplateParameterList(const TemplateParameterList *Params) { |
| 132 | LVPair LV(ExternalLinkage, DefaultVisibility); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 133 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 134 | PEnd = Params->end(); |
| 135 | P != PEnd; ++P) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 136 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 137 | if (NTTP->isExpandedParameterPack()) { |
| 138 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 139 | QualType T = NTTP->getExpansionType(I); |
| 140 | if (!T->isDependentType()) |
| 141 | LV = merge(LV, T->getLinkageAndVisibility()); |
| 142 | } |
| 143 | continue; |
| 144 | } |
| 145 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 146 | if (!NTTP->getType()->isDependentType()) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 147 | LV = merge(LV, NTTP->getType()->getLinkageAndVisibility()); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 148 | continue; |
| 149 | } |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 150 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 151 | |
| 152 | if (TemplateTemplateParmDecl *TTP |
| 153 | = dyn_cast<TemplateTemplateParmDecl>(*P)) { |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 154 | LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 158 | return LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 161 | /// getLVForDecl - Get the linkage and visibility for the given declaration. |
| 162 | static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F); |
| 163 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 164 | /// \brief Get the most restrictive linkage for the types and |
| 165 | /// declarations in the given template argument list. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 166 | static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args, |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 167 | unsigned NumArgs, |
| 168 | LVFlags &F) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 169 | LVPair LV(ExternalLinkage, DefaultVisibility); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 170 | |
| 171 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 172 | switch (Args[I].getKind()) { |
| 173 | case TemplateArgument::Null: |
| 174 | case TemplateArgument::Integral: |
| 175 | case TemplateArgument::Expression: |
| 176 | break; |
| 177 | |
| 178 | case TemplateArgument::Type: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 179 | LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility()); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 180 | break; |
| 181 | |
| 182 | case TemplateArgument::Declaration: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 183 | // The decl can validly be null as the representation of nullptr |
| 184 | // arguments, valid only in C++0x. |
| 185 | if (Decl *D = Args[I].getAsDecl()) { |
Douglas Gregor | 89d63e5 | 2010-12-06 18:50:56 +0000 | [diff] [blame] | 186 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 187 | LV = merge(LV, getLVForDecl(ND, F)); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 188 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 189 | break; |
| 190 | |
| 191 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 192 | case TemplateArgument::TemplateExpansion: |
| 193 | if (TemplateDecl *Template |
| 194 | = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl()) |
Douglas Gregor | 89d63e5 | 2010-12-06 18:50:56 +0000 | [diff] [blame] | 195 | LV = merge(LV, getLVForDecl(Template, F)); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 196 | break; |
| 197 | |
| 198 | case TemplateArgument::Pack: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 199 | LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(), |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 200 | Args[I].pack_size(), |
| 201 | F)); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 206 | return LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 207 | } |
| 208 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 209 | static LVPair |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 210 | getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, |
| 211 | LVFlags &F) { |
| 212 | return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 213 | } |
| 214 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 215 | static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 216 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 217 | "Not a name having namespace scope"); |
| 218 | ASTContext &Context = D->getASTContext(); |
| 219 | |
| 220 | // C++ [basic.link]p3: |
| 221 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 222 | // is the name of |
| 223 | // - an object, reference, function or function template that is |
| 224 | // explicitly declared static; or, |
| 225 | // (This bullet corresponds to C99 6.2.2p3.) |
| 226 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 227 | // Explicitly declared static. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 228 | if (Var->getStorageClass() == SC_Static) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 229 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 230 | |
| 231 | // - an object or reference that is explicitly declared const |
| 232 | // and neither explicitly declared extern nor previously |
| 233 | // declared to have external linkage; or |
| 234 | // (there is no equivalent in C99) |
| 235 | if (Context.getLangOptions().CPlusPlus && |
Eli Friedman | e9d6554 | 2009-11-26 03:04:01 +0000 | [diff] [blame] | 236 | Var->getType().isConstant(Context) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 237 | Var->getStorageClass() != SC_Extern && |
| 238 | Var->getStorageClass() != SC_PrivateExtern) { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 239 | bool FoundExtern = false; |
| 240 | for (const VarDecl *PrevVar = Var->getPreviousDeclaration(); |
| 241 | PrevVar && !FoundExtern; |
| 242 | PrevVar = PrevVar->getPreviousDeclaration()) |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 243 | if (isExternalLinkage(PrevVar->getLinkage())) |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 244 | FoundExtern = true; |
| 245 | |
| 246 | if (!FoundExtern) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 247 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 248 | } |
| 249 | } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 250 | // C++ [temp]p4: |
| 251 | // A non-member function template can have internal linkage; any |
| 252 | // other template name shall have external linkage. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 253 | const FunctionDecl *Function = 0; |
| 254 | if (const FunctionTemplateDecl *FunTmpl |
| 255 | = dyn_cast<FunctionTemplateDecl>(D)) |
| 256 | Function = FunTmpl->getTemplatedDecl(); |
| 257 | else |
| 258 | Function = cast<FunctionDecl>(D); |
| 259 | |
| 260 | // Explicitly declared static. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 261 | if (Function->getStorageClass() == SC_Static) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 262 | return LinkageInfo(InternalLinkage, DefaultVisibility, false); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 263 | } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) { |
| 264 | // - a data member of an anonymous union. |
| 265 | if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 266 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Chandler Carruth | 094b643 | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 269 | if (D->isInAnonymousNamespace()) { |
| 270 | const VarDecl *Var = dyn_cast<VarDecl>(D); |
| 271 | const FunctionDecl *Func = dyn_cast<FunctionDecl>(D); |
| 272 | if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC())) |
| 273 | return LinkageInfo::uniqueExternal(); |
| 274 | } |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 275 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 276 | // Set up the defaults. |
| 277 | |
| 278 | // C99 6.2.2p5: |
| 279 | // If the declaration of an identifier for an object has file |
| 280 | // scope and no storage-class specifier, its linkage is |
| 281 | // external. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 282 | LinkageInfo LV; |
| 283 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 284 | if (F.ConsiderVisibilityAttributes) { |
| 285 | if (const VisibilityAttr *VA = GetExplicitVisibility(D)) { |
| 286 | LV.setVisibility(GetVisibilityFromAttr(VA), true); |
| 287 | F.ConsiderGlobalVisibility = false; |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 288 | } else { |
| 289 | // If we're declared in a namespace with a visibility attribute, |
| 290 | // use that namespace's visibility, but don't call it explicit. |
| 291 | for (const DeclContext *DC = D->getDeclContext(); |
| 292 | !isa<TranslationUnitDecl>(DC); |
| 293 | DC = DC->getParent()) { |
| 294 | if (!isa<NamespaceDecl>(DC)) continue; |
| 295 | if (const VisibilityAttr *VA = |
| 296 | cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) { |
| 297 | LV.setVisibility(GetVisibilityFromAttr(VA), false); |
| 298 | F.ConsiderGlobalVisibility = false; |
| 299 | break; |
| 300 | } |
| 301 | } |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 302 | } |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 303 | } |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 304 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 305 | // C++ [basic.link]p4: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 307 | // A name having namespace scope has external linkage if it is the |
| 308 | // name of |
| 309 | // |
| 310 | // - an object or reference, unless it has internal linkage; or |
| 311 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 312 | // GCC applies the following optimization to variables and static |
| 313 | // data members, but not to functions: |
| 314 | // |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 315 | // Modify the variable's LV by the LV of its type unless this is |
| 316 | // C or extern "C". This follows from [basic.link]p9: |
| 317 | // A type without linkage shall not be used as the type of a |
| 318 | // variable or function with external linkage unless |
| 319 | // - the entity has C language linkage, or |
| 320 | // - the entity is declared within an unnamed namespace, or |
| 321 | // - the entity is not used or is defined in the same |
| 322 | // translation unit. |
| 323 | // and [basic.link]p10: |
| 324 | // ...the types specified by all declarations referring to a |
| 325 | // given variable or function shall be identical... |
| 326 | // C does not have an equivalent rule. |
| 327 | // |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 328 | // Ignore this if we've got an explicit attribute; the user |
| 329 | // probably knows what they're doing. |
| 330 | // |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 331 | // Note that we don't want to make the variable non-external |
| 332 | // because of this, but unique-external linkage suits us. |
John McCall | ee30102 | 2010-10-30 09:18:49 +0000 | [diff] [blame] | 333 | if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 334 | LVPair TypeLV = Var->getType()->getLinkageAndVisibility(); |
| 335 | if (TypeLV.first != ExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 336 | return LinkageInfo::uniqueExternal(); |
| 337 | if (!LV.visibilityExplicit()) |
| 338 | LV.mergeVisibility(TypeLV.second); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 339 | } |
| 340 | |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 341 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 342 | LV.setVisibility(HiddenVisibility, true); |
| 343 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 344 | if (!Context.getLangOptions().CPlusPlus && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 345 | (Var->getStorageClass() == SC_Extern || |
| 346 | Var->getStorageClass() == SC_PrivateExtern)) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 347 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 348 | // C99 6.2.2p4: |
| 349 | // For an identifier declared with the storage-class specifier |
| 350 | // extern in a scope in which a prior declaration of that |
| 351 | // identifier is visible, if the prior declaration specifies |
| 352 | // internal or external linkage, the linkage of the identifier |
| 353 | // at the later declaration is the same as the linkage |
| 354 | // specified at the prior declaration. If no prior declaration |
| 355 | // is visible, or if the prior declaration specifies no |
| 356 | // linkage, then the identifier has external linkage. |
| 357 | if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 358 | LinkageInfo PrevLV = getLVForDecl(PrevVar, F); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 359 | if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); |
| 360 | LV.mergeVisibility(PrevLV); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 364 | // - a function, unless it has internal linkage; or |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 365 | } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 67fa6d5 | 2010-10-28 07:07:52 +0000 | [diff] [blame] | 366 | // In theory, we can modify the function's LV by the LV of its |
| 367 | // type unless it has C linkage (see comment above about variables |
| 368 | // for justification). In practice, GCC doesn't do this, so it's |
| 369 | // just too painful to make work. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 370 | |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 371 | if (Function->getStorageClass() == SC_PrivateExtern) |
| 372 | LV.setVisibility(HiddenVisibility, true); |
| 373 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 374 | // C99 6.2.2p5: |
| 375 | // If the declaration of an identifier for a function has no |
| 376 | // storage-class specifier, its linkage is determined exactly |
| 377 | // as if it were declared with the storage-class specifier |
| 378 | // extern. |
| 379 | if (!Context.getLangOptions().CPlusPlus && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 380 | (Function->getStorageClass() == SC_Extern || |
| 381 | Function->getStorageClass() == SC_PrivateExtern || |
| 382 | Function->getStorageClass() == SC_None)) { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 383 | // C99 6.2.2p4: |
| 384 | // For an identifier declared with the storage-class specifier |
| 385 | // extern in a scope in which a prior declaration of that |
| 386 | // identifier is visible, if the prior declaration specifies |
| 387 | // internal or external linkage, the linkage of the identifier |
| 388 | // at the later declaration is the same as the linkage |
| 389 | // specified at the prior declaration. If no prior declaration |
| 390 | // is visible, or if the prior declaration specifies no |
| 391 | // linkage, then the identifier has external linkage. |
| 392 | if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 393 | LinkageInfo PrevLV = getLVForDecl(PrevFunc, F); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 394 | if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); |
| 395 | LV.mergeVisibility(PrevLV); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 399 | // In C++, then if the type of the function uses a type with |
| 400 | // unique-external linkage, it's not legally usable from outside |
| 401 | // this translation unit. However, we should use the C linkage |
| 402 | // rules instead for extern "C" declarations. |
| 403 | if (Context.getLangOptions().CPlusPlus && !Function->isExternC() && |
| 404 | Function->getType()->getLinkage() == UniqueExternalLinkage) |
| 405 | return LinkageInfo::uniqueExternal(); |
| 406 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 407 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 408 | = Function->getTemplateSpecializationInfo()) { |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 409 | LV.merge(getLVForDecl(SpecInfo->getTemplate(), |
| 410 | F.onlyTemplateVisibility())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 411 | const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments; |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 412 | LV.merge(getLVForTemplateArgumentList(TemplateArgs, F)); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 415 | // - a named class (Clause 9), or an unnamed class defined in a |
| 416 | // typedef declaration in which the class has the typedef name |
| 417 | // for linkage purposes (7.1.3); or |
| 418 | // - a named enumeration (7.2), or an unnamed enumeration |
| 419 | // defined in a typedef declaration in which the enumeration |
| 420 | // has the typedef name for linkage purposes (7.1.3); or |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 421 | } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) { |
| 422 | // Unnamed tags have no linkage. |
| 423 | if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 424 | return LinkageInfo::none(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 425 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 426 | // If this is a class template specialization, consider the |
| 427 | // linkage of the template and template arguments. |
| 428 | if (const ClassTemplateSpecializationDecl *Spec |
| 429 | = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 430 | // From the template. |
| 431 | LV.merge(getLVForDecl(Spec->getSpecializedTemplate(), |
| 432 | F.onlyTemplateVisibility())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 433 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 434 | // The arguments at which the template was instantiated. |
| 435 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 436 | LV.merge(getLVForTemplateArgumentList(TemplateArgs, F)); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 437 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 438 | |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 439 | // Consider -fvisibility unless the type has C linkage. |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 440 | if (F.ConsiderGlobalVisibility) |
| 441 | F.ConsiderGlobalVisibility = |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 442 | (Context.getLangOptions().CPlusPlus && |
| 443 | !Tag->getDeclContext()->isExternCContext()); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 444 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 445 | // - an enumerator belonging to an enumeration with external linkage; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 446 | } else if (isa<EnumConstantDecl>(D)) { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 447 | LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 448 | if (!isExternalLinkage(EnumLV.linkage())) |
| 449 | return LinkageInfo::none(); |
| 450 | LV.merge(EnumLV); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 451 | |
| 452 | // - a template, unless it is a function template that has |
| 453 | // internal linkage (Clause 14); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 454 | } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 455 | LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 456 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 457 | // - a namespace (7.3), unless it is declared within an unnamed |
| 458 | // namespace. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 459 | } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) { |
| 460 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 461 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 462 | // By extension, we assign external linkage to Objective-C |
| 463 | // interfaces. |
| 464 | } else if (isa<ObjCInterfaceDecl>(D)) { |
| 465 | // fallout |
| 466 | |
| 467 | // Everything not covered here has no linkage. |
| 468 | } else { |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 469 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | // If we ended up with non-external linkage, visibility should |
| 473 | // always be default. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 474 | if (LV.linkage() != ExternalLinkage) |
| 475 | return LinkageInfo(LV.linkage(), DefaultVisibility, false); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 476 | |
| 477 | // If we didn't end up with hidden visibility, consider attributes |
| 478 | // and -fvisibility. |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 479 | if (F.ConsiderGlobalVisibility) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 480 | LV.mergeVisibility(Context.getLangOptions().getVisibilityMode()); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 481 | |
| 482 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 483 | } |
| 484 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 485 | static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 486 | // Only certain class members have linkage. Note that fields don't |
| 487 | // really have linkage, but it's convenient to say they do for the |
| 488 | // purposes of calculating linkage of pointer-to-data-member |
| 489 | // template arguments. |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 490 | if (!(isa<CXXMethodDecl>(D) || |
| 491 | isa<VarDecl>(D) || |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 492 | isa<FieldDecl>(D) || |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 493 | (isa<TagDecl>(D) && |
| 494 | (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl())))) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 495 | return LinkageInfo::none(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 496 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 497 | LinkageInfo LV; |
| 498 | |
| 499 | // The flags we're going to use to compute the class's visibility. |
| 500 | LVFlags ClassF = F; |
| 501 | |
| 502 | // If we have an explicit visibility attribute, merge that in. |
| 503 | if (F.ConsiderVisibilityAttributes) { |
| 504 | if (const VisibilityAttr *VA = GetExplicitVisibility(D)) { |
| 505 | LV.mergeVisibility(GetVisibilityFromAttr(VA), true); |
| 506 | |
| 507 | // Ignore global visibility later, but not this attribute. |
| 508 | F.ConsiderGlobalVisibility = false; |
| 509 | |
| 510 | // Ignore both global visibility and attributes when computing our |
| 511 | // parent's visibility. |
| 512 | ClassF = F.onlyTemplateVisibility(); |
| 513 | } |
| 514 | } |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 515 | |
| 516 | // Class members only have linkage if their class has external |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 517 | // linkage. |
| 518 | LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF)); |
| 519 | if (!isExternalLinkage(LV.linkage())) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 520 | return LinkageInfo::none(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 521 | |
| 522 | // If the class already has unique-external linkage, we can't improve. |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 523 | if (LV.linkage() == UniqueExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 524 | return LinkageInfo::uniqueExternal(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 525 | |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 526 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 527 | // If the type of the function uses a type with unique-external |
| 528 | // linkage, it's not legally usable from outside this translation unit. |
| 529 | if (MD->getType()->getLinkage() == UniqueExternalLinkage) |
| 530 | return LinkageInfo::uniqueExternal(); |
| 531 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 532 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 533 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 534 | // If this is a method template specialization, use the linkage for |
| 535 | // the template parameters and arguments. |
| 536 | if (FunctionTemplateSpecializationInfo *Spec |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 537 | = MD->getTemplateSpecializationInfo()) { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 538 | LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F)); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 539 | LV.merge(getLVForTemplateParameterList( |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 540 | Spec->getTemplate()->getTemplateParameters())); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 541 | |
| 542 | TSK = Spec->getTemplateSpecializationKind(); |
| 543 | } else if (MemberSpecializationInfo *MSI = |
| 544 | MD->getMemberSpecializationInfo()) { |
| 545 | TSK = MSI->getTemplateSpecializationKind(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 546 | } |
| 547 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 548 | // If we're paying attention to global visibility, apply |
| 549 | // -finline-visibility-hidden if this is an inline method. |
| 550 | // |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 551 | // Note that ConsiderGlobalVisibility doesn't yet have information |
| 552 | // about whether containing classes have visibility attributes, |
| 553 | // and that's intentional. |
| 554 | if (TSK != TSK_ExplicitInstantiationDeclaration && |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 555 | F.ConsiderGlobalVisibility && |
John McCall | 66cbcf3 | 2010-11-01 01:29:57 +0000 | [diff] [blame] | 556 | MD->getASTContext().getLangOptions().InlineVisibilityHidden) { |
| 557 | // InlineVisibilityHidden only applies to definitions, and |
| 558 | // isInlined() only gives meaningful answers on definitions |
| 559 | // anyway. |
| 560 | const FunctionDecl *Def = 0; |
| 561 | if (MD->hasBody(Def) && Def->isInlined()) |
| 562 | LV.setVisibility(HiddenVisibility); |
| 563 | } |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 564 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 565 | // Note that in contrast to basically every other situation, we |
| 566 | // *do* apply -fvisibility to method declarations. |
| 567 | |
| 568 | } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 569 | if (const ClassTemplateSpecializationDecl *Spec |
| 570 | = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { |
| 571 | // Merge template argument/parameter information for member |
| 572 | // class template specializations. |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 573 | LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F)); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 574 | LV.merge(getLVForTemplateParameterList( |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 575 | Spec->getSpecializedTemplate()->getTemplateParameters())); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 576 | } |
| 577 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 578 | // Static data members. |
| 579 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
John McCall | ee30102 | 2010-10-30 09:18:49 +0000 | [diff] [blame] | 580 | // Modify the variable's linkage by its type, but ignore the |
| 581 | // type's visibility unless it's a definition. |
| 582 | LVPair TypeLV = VD->getType()->getLinkageAndVisibility(); |
| 583 | if (TypeLV.first != ExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 584 | LV.mergeLinkage(UniqueExternalLinkage); |
| 585 | if (!LV.visibilityExplicit()) |
| 586 | LV.mergeVisibility(TypeLV.second); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 587 | } |
| 588 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 589 | F.ConsiderGlobalVisibility &= !LV.visibilityExplicit(); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 590 | |
| 591 | // Apply -fvisibility if desired. |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 592 | if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) { |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 593 | LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode()); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 594 | } |
| 595 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 596 | return LV; |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 597 | } |
| 598 | |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 599 | static void clearLinkageForClass(const CXXRecordDecl *record) { |
| 600 | for (CXXRecordDecl::decl_iterator |
| 601 | i = record->decls_begin(), e = record->decls_end(); i != e; ++i) { |
| 602 | Decl *child = *i; |
| 603 | if (isa<NamedDecl>(child)) |
| 604 | cast<NamedDecl>(child)->ClearLinkageCache(); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | void NamedDecl::ClearLinkageCache() { |
| 609 | // Note that we can't skip clearing the linkage of children just |
| 610 | // because the parent doesn't have cached linkage: we don't cache |
| 611 | // when computing linkage for parent contexts. |
| 612 | |
| 613 | HasCachedLinkage = 0; |
| 614 | |
| 615 | // If we're changing the linkage of a class, we need to reset the |
| 616 | // linkage of child declarations, too. |
| 617 | if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this)) |
| 618 | clearLinkageForClass(record); |
| 619 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 620 | if (ClassTemplateDecl *temp = |
| 621 | dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) { |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 622 | // Clear linkage for the template pattern. |
| 623 | CXXRecordDecl *record = temp->getTemplatedDecl(); |
| 624 | record->HasCachedLinkage = 0; |
| 625 | clearLinkageForClass(record); |
| 626 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 627 | // We need to clear linkage for specializations, too. |
| 628 | for (ClassTemplateDecl::spec_iterator |
| 629 | i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) |
| 630 | i->ClearLinkageCache(); |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 631 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 632 | |
| 633 | // Clear cached linkage for function template decls, too. |
| 634 | if (FunctionTemplateDecl *temp = |
| 635 | dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) |
| 636 | for (FunctionTemplateDecl::spec_iterator |
| 637 | i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) |
| 638 | i->ClearLinkageCache(); |
| 639 | |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 642 | Linkage NamedDecl::getLinkage() const { |
| 643 | if (HasCachedLinkage) { |
Benjamin Kramer | 56ed792 | 2010-12-07 15:51:48 +0000 | [diff] [blame] | 644 | assert(Linkage(CachedLinkage) == |
| 645 | getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage()); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 646 | return Linkage(CachedLinkage); |
| 647 | } |
| 648 | |
| 649 | CachedLinkage = getLVForDecl(this, |
| 650 | LVFlags::CreateOnlyDeclLinkage()).linkage(); |
| 651 | HasCachedLinkage = 1; |
| 652 | return Linkage(CachedLinkage); |
| 653 | } |
| 654 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 655 | LinkageInfo NamedDecl::getLinkageAndVisibility() const { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 656 | LinkageInfo LI = getLVForDecl(this, LVFlags()); |
Benjamin Kramer | 56ed792 | 2010-12-07 15:51:48 +0000 | [diff] [blame] | 657 | assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage()); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 658 | HasCachedLinkage = 1; |
| 659 | CachedLinkage = LI.linkage(); |
| 660 | return LI; |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 661 | } |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 662 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 663 | static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) { |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 664 | // Objective-C: treat all Objective-C declarations as having external |
| 665 | // linkage. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 666 | switch (D->getKind()) { |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 667 | default: |
| 668 | break; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 669 | case Decl::TemplateTemplateParm: // count these as external |
| 670 | case Decl::NonTypeTemplateParm: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 671 | case Decl::ObjCAtDefsField: |
| 672 | case Decl::ObjCCategory: |
| 673 | case Decl::ObjCCategoryImpl: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 674 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 675 | case Decl::ObjCForwardProtocol: |
| 676 | case Decl::ObjCImplementation: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 677 | case Decl::ObjCMethod: |
| 678 | case Decl::ObjCProperty: |
| 679 | case Decl::ObjCPropertyImpl: |
| 680 | case Decl::ObjCProtocol: |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 681 | return LinkageInfo::external(); |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 684 | // Handle linkage for namespace-scope names. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 685 | if (D->getDeclContext()->getRedeclContext()->isFileContext()) |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 686 | return getLVForNamespaceScopeDecl(D, Flags); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 687 | |
| 688 | // C++ [basic.link]p5: |
| 689 | // In addition, a member function, static data member, a named |
| 690 | // class or enumeration of class scope, or an unnamed class or |
| 691 | // enumeration defined in a class-scope typedef declaration such |
| 692 | // that the class or enumeration has the typedef name for linkage |
| 693 | // purposes (7.1.3), has external linkage if the name of the class |
| 694 | // has external linkage. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 695 | if (D->getDeclContext()->isRecord()) |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 696 | return getLVForClassMember(D, Flags); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 697 | |
| 698 | // C++ [basic.link]p6: |
| 699 | // The name of a function declared in block scope and the name of |
| 700 | // an object declared by a block scope extern declaration have |
| 701 | // linkage. If there is a visible declaration of an entity with |
| 702 | // linkage having the same name and type, ignoring entities |
| 703 | // declared outside the innermost enclosing namespace scope, the |
| 704 | // block scope declaration declares that same entity and receives |
| 705 | // the linkage of the previous declaration. If there is more than |
| 706 | // one such matching entity, the program is ill-formed. Otherwise, |
| 707 | // if no matching entity is found, the block scope entity receives |
| 708 | // external linkage. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 709 | if (D->getLexicalDeclContext()->isFunctionOrMethod()) { |
| 710 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Chandler Carruth | 10aad44 | 2011-02-25 00:05:02 +0000 | [diff] [blame^] | 711 | if (Function->isInAnonymousNamespace() && !Function->isExternC()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 712 | return LinkageInfo::uniqueExternal(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 713 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 714 | LinkageInfo LV; |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 715 | if (Flags.ConsiderVisibilityAttributes) { |
| 716 | if (const VisibilityAttr *VA = GetExplicitVisibility(Function)) |
| 717 | LV.setVisibility(GetVisibilityFromAttr(VA)); |
| 718 | } |
| 719 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 720 | if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 721 | LinkageInfo PrevLV = getLVForDecl(Prev, Flags); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 722 | if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); |
| 723 | LV.mergeVisibility(PrevLV); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 727 | } |
| 728 | |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 729 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 730 | if (Var->getStorageClass() == SC_Extern || |
| 731 | Var->getStorageClass() == SC_PrivateExtern) { |
Chandler Carruth | 10aad44 | 2011-02-25 00:05:02 +0000 | [diff] [blame^] | 732 | if (Var->isInAnonymousNamespace() && !Var->isExternC()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 733 | return LinkageInfo::uniqueExternal(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 734 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 735 | LinkageInfo LV; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 736 | if (Var->getStorageClass() == SC_PrivateExtern) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 737 | LV.setVisibility(HiddenVisibility); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 738 | else if (Flags.ConsiderVisibilityAttributes) { |
| 739 | if (const VisibilityAttr *VA = GetExplicitVisibility(Var)) |
| 740 | LV.setVisibility(GetVisibilityFromAttr(VA)); |
| 741 | } |
| 742 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 743 | if (const VarDecl *Prev = Var->getPreviousDeclaration()) { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 744 | LinkageInfo PrevLV = getLVForDecl(Prev, Flags); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 745 | if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); |
| 746 | LV.mergeVisibility(PrevLV); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
| 753 | // C++ [basic.link]p6: |
| 754 | // Names not covered by these rules have no linkage. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 755 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 756 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 757 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 758 | std::string NamedDecl::getQualifiedNameAsString() const { |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 759 | return getQualifiedNameAsString(getASTContext().getLangOptions()); |
| 760 | } |
| 761 | |
| 762 | std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 763 | const DeclContext *Ctx = getDeclContext(); |
| 764 | |
| 765 | if (Ctx->isFunctionOrMethod()) |
| 766 | return getNameAsString(); |
| 767 | |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 768 | typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy; |
| 769 | ContextsTy Contexts; |
| 770 | |
| 771 | // Collect contexts. |
| 772 | while (Ctx && isa<NamedDecl>(Ctx)) { |
| 773 | Contexts.push_back(Ctx); |
| 774 | Ctx = Ctx->getParent(); |
| 775 | }; |
| 776 | |
| 777 | std::string QualName; |
| 778 | llvm::raw_string_ostream OS(QualName); |
| 779 | |
| 780 | for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); |
| 781 | I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | if (const ClassTemplateSpecializationDecl *Spec |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 783 | = dyn_cast<ClassTemplateSpecializationDecl>(*I)) { |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 784 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 785 | std::string TemplateArgsStr |
| 786 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 787 | TemplateArgs.data(), |
| 788 | TemplateArgs.size(), |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 789 | P); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 790 | OS << Spec->getName() << TemplateArgsStr; |
| 791 | } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) { |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 792 | if (ND->isAnonymousNamespace()) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 793 | OS << "<anonymous namespace>"; |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 794 | else |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 795 | OS << ND; |
| 796 | } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) { |
| 797 | if (!RD->getIdentifier()) |
| 798 | OS << "<anonymous " << RD->getKindName() << '>'; |
| 799 | else |
| 800 | OS << RD; |
| 801 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 802 | const FunctionProtoType *FT = 0; |
| 803 | if (FD->hasWrittenPrototype()) |
| 804 | FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>()); |
| 805 | |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 806 | OS << FD << '('; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 807 | if (FT) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 808 | unsigned NumParams = FD->getNumParams(); |
| 809 | for (unsigned i = 0; i < NumParams; ++i) { |
| 810 | if (i) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 811 | OS << ", "; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 812 | std::string Param; |
| 813 | FD->getParamDecl(i)->getType().getAsStringInternal(Param, P); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 814 | OS << Param; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | if (FT->isVariadic()) { |
| 818 | if (NumParams > 0) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 819 | OS << ", "; |
| 820 | OS << "..."; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 821 | } |
| 822 | } |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 823 | OS << ')'; |
| 824 | } else { |
| 825 | OS << cast<NamedDecl>(*I); |
| 826 | } |
| 827 | OS << "::"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 828 | } |
| 829 | |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 830 | if (getDeclName()) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 831 | OS << this; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 832 | else |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 833 | OS << "<anonymous>"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 834 | |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 835 | return OS.str(); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 838 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 839 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 840 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 841 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 842 | // We want to keep it, unless it nominates same namespace. |
| 843 | if (getKind() == Decl::UsingDirective) { |
| 844 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == |
| 845 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); |
| 846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 848 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 849 | // For function declarations, we keep track of redeclarations. |
| 850 | return FD->getPreviousDeclaration() == OldD; |
| 851 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 852 | // For function templates, the underlying function declarations are linked. |
| 853 | if (const FunctionTemplateDecl *FunctionTemplate |
| 854 | = dyn_cast<FunctionTemplateDecl>(this)) |
| 855 | if (const FunctionTemplateDecl *OldFunctionTemplate |
| 856 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 857 | return FunctionTemplate->getTemplatedDecl() |
| 858 | ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 860 | // For method declarations, we keep track of redeclarations. |
| 861 | if (isa<ObjCMethodDecl>(this)) |
| 862 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 864 | if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) |
| 865 | return true; |
| 866 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 867 | if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) |
| 868 | return cast<UsingShadowDecl>(this)->getTargetDecl() == |
| 869 | cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 870 | |
Argyrios Kyrtzidis | c80117e | 2010-11-04 08:48:52 +0000 | [diff] [blame] | 871 | if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) |
| 872 | return cast<UsingDecl>(this)->getTargetNestedNameDecl() == |
| 873 | cast<UsingDecl>(OldD)->getTargetNestedNameDecl(); |
| 874 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 875 | // For non-function declarations, if the declarations are of the |
| 876 | // same kind then this must be a redeclaration, or semantic analysis |
| 877 | // would not have given us the new declaration. |
| 878 | return this->getKind() == OldD->getKind(); |
| 879 | } |
| 880 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 881 | bool NamedDecl::hasLinkage() const { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 882 | return getLinkage() != NoLinkage; |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 883 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 884 | |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 885 | NamedDecl *NamedDecl::getUnderlyingDecl() { |
| 886 | NamedDecl *ND = this; |
| 887 | while (true) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 888 | if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 889 | ND = UD->getTargetDecl(); |
| 890 | else if (ObjCCompatibleAliasDecl *AD |
| 891 | = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 892 | return AD->getClassInterface(); |
| 893 | else |
| 894 | return ND; |
| 895 | } |
| 896 | } |
| 897 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 898 | bool NamedDecl::isCXXInstanceMember() const { |
| 899 | assert(isCXXClassMember() && |
| 900 | "checking whether non-member is instance member"); |
| 901 | |
| 902 | const NamedDecl *D = this; |
| 903 | if (isa<UsingShadowDecl>(D)) |
| 904 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 905 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 906 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 907 | return true; |
| 908 | if (isa<CXXMethodDecl>(D)) |
| 909 | return cast<CXXMethodDecl>(D)->isInstance(); |
| 910 | if (isa<FunctionTemplateDecl>(D)) |
| 911 | return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D) |
| 912 | ->getTemplatedDecl())->isInstance(); |
| 913 | return false; |
| 914 | } |
| 915 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 916 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 917 | // DeclaratorDecl Implementation |
| 918 | //===----------------------------------------------------------------------===// |
| 919 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 920 | template <typename DeclT> |
| 921 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 922 | if (decl->getNumTemplateParameterLists() > 0) |
| 923 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 924 | else |
| 925 | return decl->getInnerLocStart(); |
| 926 | } |
| 927 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 928 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 929 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 930 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 931 | return SourceLocation(); |
| 932 | } |
| 933 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 934 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier, |
| 935 | SourceRange QualifierRange) { |
| 936 | if (Qualifier) { |
| 937 | // Make sure the extended decl info is allocated. |
| 938 | if (!hasExtInfo()) { |
| 939 | // Save (non-extended) type source info pointer. |
| 940 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 941 | // Allocate external info struct. |
| 942 | DeclInfo = new (getASTContext()) ExtInfo; |
| 943 | // Restore savedTInfo into (extended) decl info. |
| 944 | getExtInfo()->TInfo = savedTInfo; |
| 945 | } |
| 946 | // Set qualifier info. |
| 947 | getExtInfo()->NNS = Qualifier; |
| 948 | getExtInfo()->NNSRange = QualifierRange; |
| 949 | } |
| 950 | else { |
| 951 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| 952 | assert(QualifierRange.isInvalid()); |
| 953 | if (hasExtInfo()) { |
| 954 | // Save type source info pointer. |
| 955 | TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; |
| 956 | // Deallocate the extended decl info. |
| 957 | getASTContext().Deallocate(getExtInfo()); |
| 958 | // Restore savedTInfo into (non-extended) decl info. |
| 959 | DeclInfo = savedTInfo; |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 964 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 965 | return getTemplateOrInnerLocStart(this); |
| 966 | } |
| 967 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 968 | void |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 969 | QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context, |
| 970 | unsigned NumTPLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 971 | TemplateParameterList **TPLists) { |
| 972 | assert((NumTPLists == 0 || TPLists != 0) && |
| 973 | "Empty array of template parameters with positive size!"); |
| 974 | assert((NumTPLists == 0 || NNS) && |
| 975 | "Nonempty array of template parameters with no qualifier!"); |
| 976 | |
| 977 | // Free previous template parameters (if any). |
| 978 | if (NumTemplParamLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 979 | Context.Deallocate(TemplParamLists); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 980 | TemplParamLists = 0; |
| 981 | NumTemplParamLists = 0; |
| 982 | } |
| 983 | // Set info on matched template parameter lists (if any). |
| 984 | if (NumTPLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 985 | TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 986 | NumTemplParamLists = NumTPLists; |
| 987 | for (unsigned i = NumTPLists; i-- > 0; ) |
| 988 | TemplParamLists[i] = TPLists[i]; |
| 989 | } |
| 990 | } |
| 991 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 992 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 993 | // VarDecl Implementation |
| 994 | //===----------------------------------------------------------------------===// |
| 995 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 996 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 997 | switch (SC) { |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 998 | case SC_None: break; |
| 999 | case SC_Auto: return "auto"; break; |
| 1000 | case SC_Extern: return "extern"; break; |
| 1001 | case SC_PrivateExtern: return "__private_extern__"; break; |
| 1002 | case SC_Register: return "register"; break; |
| 1003 | case SC_Static: return "static"; break; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | assert(0 && "Invalid storage class"); |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1010 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1011 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1012 | StorageClass S, StorageClass SCAsWritten) { |
| 1013 | return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1016 | void VarDecl::setStorageClass(StorageClass SC) { |
| 1017 | assert(isLegalForVariable(SC)); |
| 1018 | if (getStorageClass() != SC) |
| 1019 | ClearLinkageCache(); |
| 1020 | |
| 1021 | SClass = SC; |
| 1022 | } |
| 1023 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1024 | SourceLocation VarDecl::getInnerLocStart() const { |
| 1025 | SourceLocation Start = getTypeSpecStartLoc(); |
| 1026 | if (Start.isInvalid()) |
| 1027 | Start = getLocation(); |
| 1028 | return Start; |
| 1029 | } |
| 1030 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1031 | SourceRange VarDecl::getSourceRange() const { |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1032 | if (getInit()) |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1033 | return SourceRange(getOuterLocStart(), getInit()->getLocEnd()); |
| 1034 | return SourceRange(getOuterLocStart(), getLocation()); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1037 | bool VarDecl::isExternC() const { |
| 1038 | ASTContext &Context = getASTContext(); |
| 1039 | if (!Context.getLangOptions().CPlusPlus) |
| 1040 | return (getDeclContext()->isTranslationUnit() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1041 | getStorageClass() != SC_Static) || |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1042 | (getDeclContext()->isFunctionOrMethod() && hasExternalStorage()); |
| 1043 | |
Chandler Carruth | 10aad44 | 2011-02-25 00:05:02 +0000 | [diff] [blame^] | 1044 | const DeclContext *DC = getDeclContext(); |
| 1045 | if (DC->isFunctionOrMethod()) |
| 1046 | return false; |
| 1047 | |
| 1048 | for (; !DC->isTranslationUnit(); DC = DC->getParent()) { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1049 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 1050 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1051 | return getStorageClass() != SC_Static; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1052 | |
| 1053 | break; |
| 1054 | } |
| 1055 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | return false; |
| 1059 | } |
| 1060 | |
| 1061 | VarDecl *VarDecl::getCanonicalDecl() { |
| 1062 | return getFirstDeclaration(); |
| 1063 | } |
| 1064 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1065 | VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const { |
| 1066 | // C++ [basic.def]p2: |
| 1067 | // A declaration is a definition unless [...] it contains the 'extern' |
| 1068 | // specifier or a linkage-specification and neither an initializer [...], |
| 1069 | // it declares a static data member in a class declaration [...]. |
| 1070 | // C++ [temp.expl.spec]p15: |
| 1071 | // An explicit specialization of a static data member of a template is a |
| 1072 | // definition if the declaration includes an initializer; otherwise, it is |
| 1073 | // a declaration. |
| 1074 | if (isStaticDataMember()) { |
| 1075 | if (isOutOfLine() && (hasInit() || |
| 1076 | getTemplateSpecializationKind() != TSK_ExplicitSpecialization)) |
| 1077 | return Definition; |
| 1078 | else |
| 1079 | return DeclarationOnly; |
| 1080 | } |
| 1081 | // C99 6.7p5: |
| 1082 | // A definition of an identifier is a declaration for that identifier that |
| 1083 | // [...] causes storage to be reserved for that object. |
| 1084 | // Note: that applies for all non-file-scope objects. |
| 1085 | // C99 6.9.2p1: |
| 1086 | // If the declaration of an identifier for an object has file scope and an |
| 1087 | // initializer, the declaration is an external definition for the identifier |
| 1088 | if (hasInit()) |
| 1089 | return Definition; |
| 1090 | // AST for 'extern "C" int foo;' is annotated with 'extern'. |
| 1091 | if (hasExternalStorage()) |
| 1092 | return DeclarationOnly; |
Fariborz Jahanian | 2bf6d7b | 2010-06-21 16:08:37 +0000 | [diff] [blame] | 1093 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1094 | if (getStorageClassAsWritten() == SC_Extern || |
| 1095 | getStorageClassAsWritten() == SC_PrivateExtern) { |
Fariborz Jahanian | 2bf6d7b | 2010-06-21 16:08:37 +0000 | [diff] [blame] | 1096 | for (const VarDecl *PrevVar = getPreviousDeclaration(); |
| 1097 | PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) { |
| 1098 | if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit()) |
| 1099 | return DeclarationOnly; |
| 1100 | } |
| 1101 | } |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1102 | // C99 6.9.2p2: |
| 1103 | // A declaration of an object that has file scope without an initializer, |
| 1104 | // and without a storage class specifier or the scs 'static', constitutes |
| 1105 | // a tentative definition. |
| 1106 | // No such thing in C++. |
| 1107 | if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl()) |
| 1108 | return TentativeDefinition; |
| 1109 | |
| 1110 | // What's left is (in C, block-scope) declarations without initializers or |
| 1111 | // external storage. These are definitions. |
| 1112 | return Definition; |
| 1113 | } |
| 1114 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1115 | VarDecl *VarDecl::getActingDefinition() { |
| 1116 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 1117 | if (Kind != TentativeDefinition) |
| 1118 | return 0; |
| 1119 | |
Chris Lattner | f0ed9ef | 2010-06-14 18:31:46 +0000 | [diff] [blame] | 1120 | VarDecl *LastTentative = 0; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1121 | VarDecl *First = getFirstDeclaration(); |
| 1122 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 1123 | I != E; ++I) { |
| 1124 | Kind = (*I)->isThisDeclarationADefinition(); |
| 1125 | if (Kind == Definition) |
| 1126 | return 0; |
| 1127 | else if (Kind == TentativeDefinition) |
| 1128 | LastTentative = *I; |
| 1129 | } |
| 1130 | return LastTentative; |
| 1131 | } |
| 1132 | |
| 1133 | bool VarDecl::isTentativeDefinitionNow() const { |
| 1134 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 1135 | if (Kind != TentativeDefinition) |
| 1136 | return false; |
| 1137 | |
| 1138 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1139 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 1140 | return false; |
| 1141 | } |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1142 | return true; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1145 | VarDecl *VarDecl::getDefinition() { |
Sebastian Redl | e2c52d2 | 2010-02-02 17:55:12 +0000 | [diff] [blame] | 1146 | VarDecl *First = getFirstDeclaration(); |
| 1147 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 1148 | I != E; ++I) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1149 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 1150 | return *I; |
| 1151 | } |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1155 | VarDecl::DefinitionKind VarDecl::hasDefinition() const { |
| 1156 | DefinitionKind Kind = DeclarationOnly; |
| 1157 | |
| 1158 | const VarDecl *First = getFirstDeclaration(); |
| 1159 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 1160 | I != E; ++I) |
| 1161 | Kind = std::max(Kind, (*I)->isThisDeclarationADefinition()); |
| 1162 | |
| 1163 | return Kind; |
| 1164 | } |
| 1165 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1166 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1167 | redecl_iterator I = redecls_begin(), E = redecls_end(); |
| 1168 | while (I != E && !I->getInit()) |
| 1169 | ++I; |
| 1170 | |
| 1171 | if (I != E) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1172 | D = *I; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1173 | return I->getInit(); |
| 1174 | } |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1178 | bool VarDecl::isOutOfLine() const { |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1179 | if (Decl::isOutOfLine()) |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1180 | return true; |
Chandler Carruth | 8761d68 | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 1181 | |
| 1182 | if (!isStaticDataMember()) |
| 1183 | return false; |
| 1184 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1185 | // If this static data member was instantiated from a static data member of |
| 1186 | // a class template, check whether that static data member was defined |
| 1187 | // out-of-line. |
| 1188 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 1189 | return VD->isOutOfLine(); |
| 1190 | |
| 1191 | return false; |
| 1192 | } |
| 1193 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1194 | VarDecl *VarDecl::getOutOfLineDefinition() { |
| 1195 | if (!isStaticDataMember()) |
| 1196 | return 0; |
| 1197 | |
| 1198 | for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); |
| 1199 | RD != RDEnd; ++RD) { |
| 1200 | if (RD->getLexicalDeclContext()->isFileContext()) |
| 1201 | return *RD; |
| 1202 | } |
| 1203 | |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1207 | void VarDecl::setInit(Expr *I) { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1208 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| 1209 | Eval->~EvaluatedStmt(); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1210 | getASTContext().Deallocate(Eval); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | Init = I; |
| 1214 | } |
| 1215 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1216 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1217 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1218 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
| 1219 | |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1223 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1224 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1225 | return MSI->getTemplateSpecializationKind(); |
| 1226 | |
| 1227 | return TSK_Undeclared; |
| 1228 | } |
| 1229 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1230 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1231 | return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 1232 | } |
| 1233 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1234 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 1235 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1236 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1237 | assert(MSI && "Not an instantiated static data member?"); |
| 1238 | MSI->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1239 | if (TSK != TSK_ExplicitSpecialization && |
| 1240 | PointOfInstantiation.isValid() && |
| 1241 | MSI->getPointOfInstantiation().isInvalid()) |
| 1242 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1245 | //===----------------------------------------------------------------------===// |
| 1246 | // ParmVarDecl Implementation |
| 1247 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1248 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1249 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
| 1250 | SourceLocation L, IdentifierInfo *Id, |
| 1251 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1252 | StorageClass S, StorageClass SCAsWritten, |
| 1253 | Expr *DefArg) { |
| 1254 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, |
| 1255 | S, SCAsWritten, DefArg); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1258 | Expr *ParmVarDecl::getDefaultArg() { |
| 1259 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 1260 | assert(!hasUninstantiatedDefaultArg() && |
| 1261 | "Default argument is not yet instantiated!"); |
| 1262 | |
| 1263 | Expr *Arg = getInit(); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1264 | if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg)) |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1265 | return E->getSubExpr(); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1266 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1267 | return Arg; |
| 1268 | } |
| 1269 | |
| 1270 | unsigned ParmVarDecl::getNumDefaultArgTemporaries() const { |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1271 | if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit())) |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1272 | return E->getNumTemporaries(); |
| 1273 | |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 1274 | return 0; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1277 | CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) { |
| 1278 | assert(getNumDefaultArgTemporaries() && |
| 1279 | "Default arguments does not have any temporaries!"); |
| 1280 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1281 | ExprWithCleanups *E = cast<ExprWithCleanups>(getInit()); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1282 | return E->getTemporary(i); |
| 1283 | } |
| 1284 | |
| 1285 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 1286 | if (const Expr *E = getInit()) |
| 1287 | return E->getSourceRange(); |
| 1288 | |
| 1289 | if (hasUninstantiatedDefaultArg()) |
| 1290 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 1291 | |
| 1292 | return SourceRange(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Douglas Gregor | 1fe85ea | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 1295 | bool ParmVarDecl::isParameterPack() const { |
| 1296 | return isa<PackExpansionType>(getType()); |
| 1297 | } |
| 1298 | |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1299 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1300 | // FunctionDecl Implementation |
| 1301 | //===----------------------------------------------------------------------===// |
| 1302 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1303 | void FunctionDecl::getNameForDiagnostic(std::string &S, |
| 1304 | const PrintingPolicy &Policy, |
| 1305 | bool Qualified) const { |
| 1306 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 1307 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 1308 | if (TemplateArgs) |
| 1309 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 1310 | TemplateArgs->data(), |
| 1311 | TemplateArgs->size(), |
| 1312 | Policy); |
| 1313 | |
| 1314 | } |
| 1315 | |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1316 | bool FunctionDecl::isVariadic() const { |
| 1317 | if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>()) |
| 1318 | return FT->isVariadic(); |
| 1319 | return false; |
| 1320 | } |
| 1321 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1322 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
| 1323 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1324 | if (I->Body) { |
| 1325 | Definition = *I; |
| 1326 | return true; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | return false; |
| 1331 | } |
| 1332 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1333 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 1334 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1335 | if (I->Body) { |
| 1336 | Definition = *I; |
| 1337 | return I->Body.get(getASTContext().getExternalSource()); |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1344 | void FunctionDecl::setBody(Stmt *B) { |
| 1345 | Body = B; |
Douglas Gregor | b5f35ba | 2010-12-06 17:49:01 +0000 | [diff] [blame] | 1346 | if (B) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1347 | EndRangeLoc = B->getLocEnd(); |
| 1348 | } |
| 1349 | |
Douglas Gregor | 2138664 | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 1350 | void FunctionDecl::setPure(bool P) { |
| 1351 | IsPure = P; |
| 1352 | if (P) |
| 1353 | if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
| 1354 | Parent->markedVirtualFunctionPure(); |
| 1355 | } |
| 1356 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 1357 | bool FunctionDecl::isMain() const { |
| 1358 | ASTContext &Context = getASTContext(); |
John McCall | 07a5c22 | 2009-08-15 02:09:25 +0000 | [diff] [blame] | 1359 | return !Context.getLangOptions().Freestanding && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1360 | getDeclContext()->getRedeclContext()->isTranslationUnit() && |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 1361 | getIdentifier() && getIdentifier()->isStr("main"); |
| 1362 | } |
| 1363 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 1364 | bool FunctionDecl::isExternC() const { |
| 1365 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1366 | // In C, any non-static, non-overloadable function has external |
| 1367 | // linkage. |
| 1368 | if (!Context.getLangOptions().CPlusPlus) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1369 | return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1370 | |
Chandler Carruth | 10aad44 | 2011-02-25 00:05:02 +0000 | [diff] [blame^] | 1371 | const DeclContext *DC = getDeclContext(); |
| 1372 | if (DC->isRecord()) |
| 1373 | return false; |
| 1374 | |
| 1375 | for (; !DC->isTranslationUnit(); DC = DC->getParent()) { |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1376 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 1377 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1378 | return getStorageClass() != SC_Static && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1379 | !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1380 | |
| 1381 | break; |
| 1382 | } |
| 1383 | } |
| 1384 | |
Douglas Gregor | 0bab54c | 2010-10-21 16:57:46 +0000 | [diff] [blame] | 1385 | return isMain(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1388 | bool FunctionDecl::isGlobal() const { |
| 1389 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 1390 | return Method->isStatic(); |
| 1391 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1392 | if (getStorageClass() == SC_Static) |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1393 | return false; |
| 1394 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1396 | DC->isNamespace(); |
| 1397 | DC = DC->getParent()) { |
| 1398 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 1399 | if (!Namespace->getDeclName()) |
| 1400 | return false; |
| 1401 | break; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | return true; |
| 1406 | } |
| 1407 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1408 | void |
| 1409 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| 1410 | redeclarable_base::setPreviousDeclaration(PrevDecl); |
| 1411 | |
| 1412 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 1413 | FunctionTemplateDecl *PrevFunTmpl |
| 1414 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 1415 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 1416 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 1417 | } |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1418 | |
| 1419 | if (PrevDecl->IsInline) |
| 1420 | IsInline = true; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | const FunctionDecl *FunctionDecl::getCanonicalDecl() const { |
| 1424 | return getFirstDeclaration(); |
| 1425 | } |
| 1426 | |
| 1427 | FunctionDecl *FunctionDecl::getCanonicalDecl() { |
| 1428 | return getFirstDeclaration(); |
| 1429 | } |
| 1430 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1431 | void FunctionDecl::setStorageClass(StorageClass SC) { |
| 1432 | assert(isLegalForFunction(SC)); |
| 1433 | if (getStorageClass() != SC) |
| 1434 | ClearLinkageCache(); |
| 1435 | |
| 1436 | SClass = SC; |
| 1437 | } |
| 1438 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1439 | /// \brief Returns a value indicating whether this function |
| 1440 | /// corresponds to a builtin function. |
| 1441 | /// |
| 1442 | /// The function corresponds to a built-in function if it is |
| 1443 | /// declared at translation scope or within an extern "C" block and |
| 1444 | /// its name matches with the name of a builtin. The returned value |
| 1445 | /// 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] | 1446 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1447 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1448 | unsigned FunctionDecl::getBuiltinID() const { |
| 1449 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1450 | if (!getIdentifier() || !getIdentifier()->getBuiltinID()) |
| 1451 | return 0; |
| 1452 | |
| 1453 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| 1454 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 1455 | return BuiltinID; |
| 1456 | |
| 1457 | // This function has the name of a known C library |
| 1458 | // function. Determine whether it actually refers to the C library |
| 1459 | // function or whether it just has the same name. |
| 1460 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1461 | // If this is a static function, it's not a builtin. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1462 | if (getStorageClass() == SC_Static) |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1463 | return 0; |
| 1464 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1465 | // If this function is at translation-unit scope and we're not in |
| 1466 | // C++, it refers to the C library function. |
| 1467 | if (!Context.getLangOptions().CPlusPlus && |
| 1468 | getDeclContext()->isTranslationUnit()) |
| 1469 | return BuiltinID; |
| 1470 | |
| 1471 | // If the function is in an extern "C" linkage specification and is |
| 1472 | // not marked "overloadable", it's the real function. |
| 1473 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1475 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1476 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1477 | return BuiltinID; |
| 1478 | |
| 1479 | // Not a builtin |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1480 | return 0; |
| 1481 | } |
| 1482 | |
| 1483 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1484 | /// getNumParams - Return the number of parameters this function must have |
Bob Wilson | 8dbfbf4 | 2011-01-10 18:23:55 +0000 | [diff] [blame] | 1485 | /// based on its FunctionType. This is the length of the ParamInfo array |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1486 | /// after it has been created. |
| 1487 | unsigned FunctionDecl::getNumParams() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1488 | const FunctionType *FT = getType()->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1489 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 1490 | return 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1491 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1495 | void FunctionDecl::setParams(ASTContext &C, |
| 1496 | ParmVarDecl **NewParamInfo, unsigned NumParams) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1497 | assert(ParamInfo == 0 && "Already has param info!"); |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 1498 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1500 | // Zero params -> null pointer. |
| 1501 | if (NumParams) { |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1502 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 1503 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1504 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1505 | |
Argyrios Kyrtzidis | 96888cc | 2009-06-23 00:42:00 +0000 | [diff] [blame] | 1506 | // Update source range. The check below allows us to set EndRangeLoc before |
| 1507 | // setting the parameters. |
Argyrios Kyrtzidis | cb5f8f5 | 2009-06-23 00:42:15 +0000 | [diff] [blame] | 1508 | if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1509 | EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1513 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 1514 | /// needed to call this function. This may be fewer than the number of |
| 1515 | /// function parameters, if some of the parameters have default |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 1516 | /// arguments (in C++) or the last parameter is a parameter pack. |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1517 | unsigned FunctionDecl::getMinRequiredArguments() const { |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 1518 | if (!getASTContext().getLangOptions().CPlusPlus) |
| 1519 | return getNumParams(); |
| 1520 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 1521 | unsigned NumRequiredArgs = getNumParams(); |
| 1522 | |
| 1523 | // If the last parameter is a parameter pack, we don't need an argument for |
| 1524 | // it. |
| 1525 | if (NumRequiredArgs > 0 && |
| 1526 | getParamDecl(NumRequiredArgs - 1)->isParameterPack()) |
| 1527 | --NumRequiredArgs; |
| 1528 | |
| 1529 | // If this parameter has a default argument, we don't need an argument for |
| 1530 | // it. |
| 1531 | while (NumRequiredArgs > 0 && |
| 1532 | getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1533 | --NumRequiredArgs; |
| 1534 | |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 1535 | // We might have parameter packs before the end. These can't be deduced, |
| 1536 | // but they can still handle multiple arguments. |
| 1537 | unsigned ArgIdx = NumRequiredArgs; |
| 1538 | while (ArgIdx > 0) { |
| 1539 | if (getParamDecl(ArgIdx - 1)->isParameterPack()) |
| 1540 | NumRequiredArgs = ArgIdx; |
| 1541 | |
| 1542 | --ArgIdx; |
| 1543 | } |
| 1544 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1545 | return NumRequiredArgs; |
| 1546 | } |
| 1547 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1548 | bool FunctionDecl::isInlined() const { |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1549 | if (IsInline) |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1550 | return true; |
Anders Carlsson | 48eda2c | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 1551 | |
| 1552 | if (isa<CXXMethodDecl>(this)) { |
| 1553 | if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified()) |
| 1554 | return true; |
| 1555 | } |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1556 | |
| 1557 | switch (getTemplateSpecializationKind()) { |
| 1558 | case TSK_Undeclared: |
| 1559 | case TSK_ExplicitSpecialization: |
| 1560 | return false; |
| 1561 | |
| 1562 | case TSK_ImplicitInstantiation: |
| 1563 | case TSK_ExplicitInstantiationDeclaration: |
| 1564 | case TSK_ExplicitInstantiationDefinition: |
| 1565 | // Handle below. |
| 1566 | break; |
| 1567 | } |
| 1568 | |
| 1569 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1570 | bool HasPattern = false; |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1571 | if (PatternDecl) |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1572 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1573 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1574 | if (HasPattern && PatternDecl) |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1575 | return PatternDecl->isInlined(); |
| 1576 | |
| 1577 | return false; |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1580 | /// \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] | 1581 | /// definition will be externally visible. |
| 1582 | /// |
| 1583 | /// Inline function definitions are always available for inlining optimizations. |
| 1584 | /// However, depending on the language dialect, declaration specifiers, and |
| 1585 | /// attributes, the definition of an inline function may or may not be |
| 1586 | /// "externally" visible to other translation units in the program. |
| 1587 | /// |
| 1588 | /// In C99, inline definitions are not externally visible by default. However, |
Mike Stump | 1e5fd7f | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 1589 | /// 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] | 1590 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 1591 | /// |
| 1592 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 1593 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 1594 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 1595 | /// an externally visible symbol, but "extern inline" will not create an |
| 1596 | /// externally visible symbol. |
| 1597 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
| 1598 | assert(isThisDeclarationADefinition() && "Must have the function definition"); |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1599 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1600 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1601 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1602 | if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) { |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1603 | // If it's not the case that both 'inline' and 'extern' are |
| 1604 | // specified on the definition, then this inline definition is |
| 1605 | // externally visible. |
| 1606 | if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern)) |
| 1607 | return true; |
| 1608 | |
| 1609 | // If any declaration is 'inline' but not 'extern', then this definition |
| 1610 | // is externally visible. |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1611 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 1612 | Redecl != RedeclEnd; |
| 1613 | ++Redecl) { |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1614 | if (Redecl->isInlineSpecified() && |
| 1615 | Redecl->getStorageClassAsWritten() != SC_Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1616 | return true; |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1617 | } |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1618 | |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 1619 | return false; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | // C99 6.7.4p6: |
| 1623 | // [...] If all of the file scope declarations for a function in a |
| 1624 | // translation unit include the inline function specifier without extern, |
| 1625 | // then the definition in that translation unit is an inline definition. |
| 1626 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 1627 | Redecl != RedeclEnd; |
| 1628 | ++Redecl) { |
| 1629 | // Only consider file-scope declarations in this test. |
| 1630 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 1631 | continue; |
| 1632 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1633 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1634 | return true; // Not an inline definition |
| 1635 | } |
| 1636 | |
| 1637 | // C99 6.7.4p6: |
| 1638 | // An inline definition does not provide an external definition for the |
| 1639 | // function, and does not forbid an external definition in another |
| 1640 | // translation unit. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 1641 | return false; |
| 1642 | } |
| 1643 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1644 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 1645 | /// function represents, if any. |
| 1646 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1647 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 1648 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1649 | else |
| 1650 | return OO_None; |
| 1651 | } |
| 1652 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 1653 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 1654 | /// represents, if any. |
| 1655 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 1656 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 1657 | return getDeclName().getCXXLiteralIdentifier(); |
| 1658 | else |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
Argyrios Kyrtzidis | d091355 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 1662 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 1663 | if (TemplateOrSpecialization.isNull()) |
| 1664 | return TK_NonTemplate; |
| 1665 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
| 1666 | return TK_FunctionTemplate; |
| 1667 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
| 1668 | return TK_MemberSpecialization; |
| 1669 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
| 1670 | return TK_FunctionTemplateSpecialization; |
| 1671 | if (TemplateOrSpecialization.is |
| 1672 | <DependentFunctionTemplateSpecializationInfo*>()) |
| 1673 | return TK_DependentFunctionTemplateSpecialization; |
| 1674 | |
| 1675 | assert(false && "Did we miss a TemplateOrSpecialization type?"); |
| 1676 | return TK_NonTemplate; |
| 1677 | } |
| 1678 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1679 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1680 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1681 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| 1682 | |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1686 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 1687 | return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 1688 | } |
| 1689 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1690 | void |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1691 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 1692 | FunctionDecl *FD, |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1693 | TemplateSpecializationKind TSK) { |
| 1694 | assert(TemplateOrSpecialization.isNull() && |
| 1695 | "Member function is already a specialization"); |
| 1696 | MemberSpecializationInfo *Info |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1697 | = new (C) MemberSpecializationInfo(FD, TSK); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1698 | TemplateOrSpecialization = Info; |
| 1699 | } |
| 1700 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1701 | bool FunctionDecl::isImplicitlyInstantiable() const { |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 1702 | // If the function is invalid, it can't be implicitly instantiated. |
| 1703 | if (isInvalidDecl()) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1704 | return false; |
| 1705 | |
| 1706 | switch (getTemplateSpecializationKind()) { |
| 1707 | case TSK_Undeclared: |
| 1708 | case TSK_ExplicitSpecialization: |
| 1709 | case TSK_ExplicitInstantiationDefinition: |
| 1710 | return false; |
| 1711 | |
| 1712 | case TSK_ImplicitInstantiation: |
| 1713 | return true; |
| 1714 | |
| 1715 | case TSK_ExplicitInstantiationDeclaration: |
| 1716 | // Handled below. |
| 1717 | break; |
| 1718 | } |
| 1719 | |
| 1720 | // Find the actual template from which we will instantiate. |
| 1721 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1722 | bool HasPattern = false; |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1723 | if (PatternDecl) |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1724 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1725 | |
| 1726 | // C++0x [temp.explicit]p9: |
| 1727 | // Except for inline functions, other explicit instantiation declarations |
| 1728 | // have the effect of suppressing the implicit instantiation of the entity |
| 1729 | // to which they refer. |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1730 | if (!HasPattern || !PatternDecl) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1731 | return true; |
| 1732 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1733 | return PatternDecl->isInlined(); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
| 1737 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 1738 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 1739 | // If we have hit a point where the user provided a specialization of |
| 1740 | // this template, we're done looking. |
| 1741 | if (Primary->isMemberSpecialization()) |
| 1742 | break; |
| 1743 | |
| 1744 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 1745 | } |
| 1746 | |
| 1747 | return Primary->getTemplatedDecl(); |
| 1748 | } |
| 1749 | |
| 1750 | return getInstantiatedFromMemberFunction(); |
| 1751 | } |
| 1752 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1753 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1755 | = TemplateOrSpecialization |
| 1756 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1757 | return Info->Template.getPointer(); |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1758 | } |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
| 1762 | const TemplateArgumentList * |
| 1763 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1765 | = TemplateOrSpecialization |
| 1766 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1767 | return Info->TemplateArguments; |
| 1768 | } |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 1772 | const TemplateArgumentListInfo * |
| 1773 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 1774 | if (FunctionTemplateSpecializationInfo *Info |
| 1775 | = TemplateOrSpecialization |
| 1776 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 1777 | return Info->TemplateArgumentsAsWritten; |
| 1778 | } |
| 1779 | return 0; |
| 1780 | } |
| 1781 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | void |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1783 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
| 1784 | FunctionTemplateDecl *Template, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1785 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1786 | void *InsertPos, |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 1787 | TemplateSpecializationKind TSK, |
Argyrios Kyrtzidis | 7b081c8 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 1788 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 1789 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1790 | assert(TSK != TSK_Undeclared && |
| 1791 | "Must specify the type of function template specialization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1792 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1793 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1794 | if (!Info) |
Argyrios Kyrtzidis | a626a3d | 2010-09-09 11:28:23 +0000 | [diff] [blame] | 1795 | Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK, |
| 1796 | TemplateArgs, |
| 1797 | TemplateArgsAsWritten, |
| 1798 | PointOfInstantiation); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1799 | TemplateOrSpecialization = Info; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1800 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1801 | // Insert this function template specialization into the set of known |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1802 | // function template specializations. |
| 1803 | if (InsertPos) |
| 1804 | Template->getSpecializations().InsertNode(Info, InsertPos); |
| 1805 | else { |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1806 | // Try to insert the new node. If there is an existing node, leave it, the |
| 1807 | // set will contain the canonical decls while |
| 1808 | // FunctionTemplateDecl::findSpecialization will return |
| 1809 | // the most recent redeclarations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1810 | FunctionTemplateSpecializationInfo *Existing |
| 1811 | = Template->getSpecializations().GetOrInsertNode(Info); |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1812 | (void)Existing; |
| 1813 | assert((!Existing || Existing->Function->isCanonicalDecl()) && |
| 1814 | "Set is supposed to only contain canonical decls"); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1815 | } |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1818 | void |
| 1819 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 1820 | const UnresolvedSetImpl &Templates, |
| 1821 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1822 | assert(TemplateOrSpecialization.isNull()); |
| 1823 | size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo); |
| 1824 | Size += Templates.size() * sizeof(FunctionTemplateDecl*); |
John McCall | 21c0160 | 2010-04-13 22:18:28 +0000 | [diff] [blame] | 1825 | Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1826 | void *Buffer = Context.Allocate(Size); |
| 1827 | DependentFunctionTemplateSpecializationInfo *Info = |
| 1828 | new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates, |
| 1829 | TemplateArgs); |
| 1830 | TemplateOrSpecialization = Info; |
| 1831 | } |
| 1832 | |
| 1833 | DependentFunctionTemplateSpecializationInfo:: |
| 1834 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 1835 | const TemplateArgumentListInfo &TArgs) |
| 1836 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
| 1837 | |
| 1838 | d.NumTemplates = Ts.size(); |
| 1839 | d.NumArgs = TArgs.size(); |
| 1840 | |
| 1841 | FunctionTemplateDecl **TsArray = |
| 1842 | const_cast<FunctionTemplateDecl**>(getTemplates()); |
| 1843 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 1844 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 1845 | |
| 1846 | TemplateArgumentLoc *ArgsArray = |
| 1847 | const_cast<TemplateArgumentLoc*>(getTemplateArgs()); |
| 1848 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 1849 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 1850 | } |
| 1851 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1852 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | // For a function template specialization, query the specialization |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1854 | // information object. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1855 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1856 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1857 | if (FTSInfo) |
| 1858 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1860 | MemberSpecializationInfo *MSInfo |
| 1861 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 1862 | if (MSInfo) |
| 1863 | return MSInfo->getTemplateSpecializationKind(); |
| 1864 | |
| 1865 | return TSK_Undeclared; |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | void |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1869 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 1870 | SourceLocation PointOfInstantiation) { |
| 1871 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 1872 | = TemplateOrSpecialization.dyn_cast< |
| 1873 | FunctionTemplateSpecializationInfo*>()) { |
| 1874 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 1875 | if (TSK != TSK_ExplicitSpecialization && |
| 1876 | PointOfInstantiation.isValid() && |
| 1877 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 1878 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1879 | } else if (MemberSpecializationInfo *MSInfo |
| 1880 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 1881 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1882 | if (TSK != TSK_ExplicitSpecialization && |
| 1883 | PointOfInstantiation.isValid() && |
| 1884 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 1885 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1886 | } else |
| 1887 | assert(false && "Function cannot have a template specialization kind"); |
| 1888 | } |
| 1889 | |
| 1890 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1891 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 1892 | = TemplateOrSpecialization.dyn_cast< |
| 1893 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1894 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1895 | else if (MemberSpecializationInfo *MSInfo |
| 1896 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1897 | return MSInfo->getPointOfInstantiation(); |
| 1898 | |
| 1899 | return SourceLocation(); |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1902 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1903 | if (Decl::isOutOfLine()) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1904 | return true; |
| 1905 | |
| 1906 | // If this function was instantiated from a member function of a |
| 1907 | // class template, check whether that member function was defined out-of-line. |
| 1908 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 1909 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1910 | if (FD->hasBody(Definition)) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1911 | return Definition->isOutOfLine(); |
| 1912 | } |
| 1913 | |
| 1914 | // If this function was instantiated from a function template, |
| 1915 | // check whether that function template was defined out-of-line. |
| 1916 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 1917 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1918 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1919 | return Definition->isOutOfLine(); |
| 1920 | } |
| 1921 | |
| 1922 | return false; |
| 1923 | } |
| 1924 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1925 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1926 | // FieldDecl Implementation |
| 1927 | //===----------------------------------------------------------------------===// |
| 1928 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1929 | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
| 1930 | SourceLocation L, IdentifierInfo *Id, QualType T, |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1931 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable) { |
| 1932 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable); |
| 1933 | } |
| 1934 | |
| 1935 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 1936 | if (!isImplicit() || getDeclName()) |
| 1937 | return false; |
| 1938 | |
| 1939 | if (const RecordType *Record = getType()->getAs<RecordType>()) |
| 1940 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 1941 | |
| 1942 | return false; |
| 1943 | } |
| 1944 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1945 | unsigned FieldDecl::getFieldIndex() const { |
| 1946 | if (CachedFieldIndex) return CachedFieldIndex - 1; |
| 1947 | |
| 1948 | unsigned index = 0; |
| 1949 | RecordDecl::field_iterator |
| 1950 | i = getParent()->field_begin(), e = getParent()->field_end(); |
| 1951 | while (true) { |
| 1952 | assert(i != e && "failed to find field in parent!"); |
| 1953 | if (*i == this) |
| 1954 | break; |
| 1955 | |
| 1956 | ++i; |
| 1957 | ++index; |
| 1958 | } |
| 1959 | |
| 1960 | CachedFieldIndex = index + 1; |
| 1961 | return index; |
| 1962 | } |
| 1963 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1964 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1965 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1966 | //===----------------------------------------------------------------------===// |
| 1967 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1968 | SourceLocation TagDecl::getOuterLocStart() const { |
| 1969 | return getTemplateOrInnerLocStart(this); |
| 1970 | } |
| 1971 | |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 1972 | SourceRange TagDecl::getSourceRange() const { |
| 1973 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1974 | return SourceRange(getOuterLocStart(), E); |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 1977 | TagDecl* TagDecl::getCanonicalDecl() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1978 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1981 | void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) { |
| 1982 | TypedefDeclOrQualifier = TDD; |
| 1983 | if (TypeForDecl) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1984 | const_cast<Type*>(TypeForDecl)->ClearLinkageCache(); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1985 | ClearLinkageCache(); |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1988 | void TagDecl::startDefinition() { |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 1989 | IsBeingDefined = true; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1990 | |
| 1991 | if (isa<CXXRecordDecl>(this)) { |
| 1992 | CXXRecordDecl *D = cast<CXXRecordDecl>(this); |
| 1993 | struct CXXRecordDecl::DefinitionData *Data = |
| 1994 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
John McCall | 2243288 | 2010-03-26 21:56:38 +0000 | [diff] [blame] | 1995 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 1996 | cast<CXXRecordDecl>(*I)->DefinitionData = Data; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1997 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | void TagDecl::completeDefinition() { |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2001 | assert((!isa<CXXRecordDecl>(this) || |
| 2002 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 2003 | "definition completed but not started"); |
| 2004 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2005 | IsDefinition = true; |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 2006 | IsBeingDefined = false; |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 2007 | |
| 2008 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2009 | L->CompletedTagDefinition(this); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2012 | TagDecl* TagDecl::getDefinition() const { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2013 | if (isDefinition()) |
| 2014 | return const_cast<TagDecl *>(this); |
Andrew Trick | 220a9c8 | 2010-10-19 21:54:32 +0000 | [diff] [blame] | 2015 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
| 2016 | return CXXRD->getDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | |
| 2018 | for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2019 | R != REnd; ++R) |
| 2020 | if (R->isDefinition()) |
| 2021 | return *R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2023 | return 0; |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2026 | void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier, |
| 2027 | SourceRange QualifierRange) { |
| 2028 | if (Qualifier) { |
| 2029 | // Make sure the extended qualifier info is allocated. |
| 2030 | if (!hasExtInfo()) |
| 2031 | TypedefDeclOrQualifier = new (getASTContext()) ExtInfo; |
| 2032 | // Set qualifier info. |
| 2033 | getExtInfo()->NNS = Qualifier; |
| 2034 | getExtInfo()->NNSRange = QualifierRange; |
| 2035 | } |
| 2036 | else { |
| 2037 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| 2038 | assert(QualifierRange.isInvalid()); |
| 2039 | if (hasExtInfo()) { |
| 2040 | getASTContext().Deallocate(getExtInfo()); |
| 2041 | TypedefDeclOrQualifier = (TypedefDecl*) 0; |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2046 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2047 | // EnumDecl Implementation |
| 2048 | //===----------------------------------------------------------------------===// |
| 2049 | |
| 2050 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 2051 | IdentifierInfo *Id, SourceLocation TKL, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2052 | EnumDecl *PrevDecl, bool IsScoped, |
| 2053 | bool IsScopedUsingClassTag, bool IsFixed) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2054 | EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2055 | IsScoped, IsScopedUsingClassTag, IsFixed); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2056 | C.getTypeDeclType(Enum, PrevDecl); |
| 2057 | return Enum; |
| 2058 | } |
| 2059 | |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 2060 | EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2061 | return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2062 | false, false, false); |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2065 | void EnumDecl::completeDefinition(QualType NewType, |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 2066 | QualType NewPromotionType, |
| 2067 | unsigned NumPositiveBits, |
| 2068 | unsigned NumNegativeBits) { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2069 | assert(!isDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2070 | if (!IntegerType) |
| 2071 | IntegerType = NewType.getTypePtr(); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2072 | PromotionType = NewPromotionType; |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 2073 | setNumPositiveBits(NumPositiveBits); |
| 2074 | setNumNegativeBits(NumNegativeBits); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2075 | TagDecl::completeDefinition(); |
| 2076 | } |
| 2077 | |
| 2078 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2079 | // RecordDecl Implementation |
| 2080 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2081 | |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 2082 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2083 | IdentifierInfo *Id, RecordDecl *PrevDecl, |
| 2084 | SourceLocation TKL) |
| 2085 | : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2086 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2087 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2088 | HasObjectMember = false; |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2089 | LoadedFieldsFromExternalStorage = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2090 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2093 | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2094 | SourceLocation L, IdentifierInfo *Id, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 2095 | SourceLocation TKL, RecordDecl* PrevDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2096 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2097 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2098 | C.getTypeDeclType(R, PrevDecl); |
| 2099 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2102 | RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) { |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 2103 | return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0, |
| 2104 | SourceLocation()); |
| 2105 | } |
| 2106 | |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 2107 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2108 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 2109 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 2110 | } |
| 2111 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2112 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
| 2113 | if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage) |
| 2114 | LoadFieldsFromExternalStorage(); |
| 2115 | |
| 2116 | return field_iterator(decl_iterator(FirstDecl)); |
| 2117 | } |
| 2118 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2119 | /// completeDefinition - Notes that the definition of this type is now |
| 2120 | /// complete. |
| 2121 | void RecordDecl::completeDefinition() { |
| 2122 | assert(!isDefinition() && "Cannot redefine record!"); |
| 2123 | TagDecl::completeDefinition(); |
| 2124 | } |
| 2125 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2126 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 2127 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 2128 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 2129 | |
| 2130 | // Notify that we have a RecordDecl doing some initialization. |
| 2131 | ExternalASTSource::Deserializing TheFields(Source); |
| 2132 | |
| 2133 | llvm::SmallVector<Decl*, 64> Decls; |
| 2134 | if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) |
| 2135 | return; |
| 2136 | |
| 2137 | #ifndef NDEBUG |
| 2138 | // Check that all decls we got were FieldDecls. |
| 2139 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
| 2140 | assert(isa<FieldDecl>(Decls[i])); |
| 2141 | #endif |
| 2142 | |
| 2143 | LoadedFieldsFromExternalStorage = true; |
| 2144 | |
| 2145 | if (Decls.empty()) |
| 2146 | return; |
| 2147 | |
| 2148 | llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls); |
| 2149 | } |
| 2150 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 2151 | //===----------------------------------------------------------------------===// |
| 2152 | // BlockDecl Implementation |
| 2153 | //===----------------------------------------------------------------------===// |
| 2154 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2155 | void BlockDecl::setParams(ParmVarDecl **NewParamInfo, |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2156 | unsigned NParms) { |
| 2157 | assert(ParamInfo == 0 && "Already has param info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2159 | // Zero params -> null pointer. |
| 2160 | if (NParms) { |
| 2161 | NumParams = NParms; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2162 | void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2163 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
| 2164 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 2165 | } |
| 2166 | } |
| 2167 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2168 | void BlockDecl::setCaptures(ASTContext &Context, |
| 2169 | const Capture *begin, |
| 2170 | const Capture *end, |
| 2171 | bool capturesCXXThis) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2172 | CapturesCXXThis = capturesCXXThis; |
| 2173 | |
| 2174 | if (begin == end) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2175 | NumCaptures = 0; |
| 2176 | Captures = 0; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2177 | return; |
| 2178 | } |
| 2179 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2180 | NumCaptures = end - begin; |
| 2181 | |
| 2182 | // Avoid new Capture[] because we don't want to provide a default |
| 2183 | // constructor. |
| 2184 | size_t allocationSize = NumCaptures * sizeof(Capture); |
| 2185 | void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*)); |
| 2186 | memcpy(buffer, begin, allocationSize); |
| 2187 | Captures = static_cast<Capture*>(buffer); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2188 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2189 | |
Douglas Gregor | 2fcbcef | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 2190 | SourceRange BlockDecl::getSourceRange() const { |
| 2191 | return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation()); |
| 2192 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2193 | |
| 2194 | //===----------------------------------------------------------------------===// |
| 2195 | // Other Decl Allocation/Deallocation Method Implementations |
| 2196 | //===----------------------------------------------------------------------===// |
| 2197 | |
| 2198 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 2199 | return new (C) TranslationUnitDecl(C); |
| 2200 | } |
| 2201 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2202 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| 2203 | SourceLocation L, IdentifierInfo *II) { |
| 2204 | return new (C) LabelDecl(DC, L, II, 0); |
| 2205 | } |
| 2206 | |
| 2207 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2208 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 2209 | SourceLocation L, IdentifierInfo *Id) { |
| 2210 | return new (C) NamespaceDecl(DC, L, Id); |
| 2211 | } |
| 2212 | |
Douglas Gregor | 06c9193 | 2010-10-27 19:49:05 +0000 | [diff] [blame] | 2213 | NamespaceDecl *NamespaceDecl::getNextNamespace() { |
| 2214 | return dyn_cast_or_null<NamespaceDecl>( |
| 2215 | NextNamespace.get(getASTContext().getExternalSource())); |
| 2216 | } |
| 2217 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2218 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
John McCall | 201e519 | 2011-02-22 22:25:56 +0000 | [diff] [blame] | 2219 | SourceLocation loc, |
| 2220 | IdentifierInfo *name, |
| 2221 | QualType type) { |
| 2222 | return new (C) ImplicitParamDecl(DC, loc, name, type); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2226 | const DeclarationNameInfo &NameInfo, |
| 2227 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2228 | StorageClass S, StorageClass SCAsWritten, |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2229 | bool isInlineSpecified, |
| 2230 | bool hasWrittenPrototype) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2231 | FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo, |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2232 | S, SCAsWritten, isInlineSpecified); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2233 | New->HasWrittenPrototype = hasWrittenPrototype; |
| 2234 | return New; |
| 2235 | } |
| 2236 | |
| 2237 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| 2238 | return new (C) BlockDecl(DC, L); |
| 2239 | } |
| 2240 | |
| 2241 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 2242 | SourceLocation L, |
| 2243 | IdentifierInfo *Id, QualType T, |
| 2244 | Expr *E, const llvm::APSInt &V) { |
| 2245 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
| 2246 | } |
| 2247 | |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 2248 | IndirectFieldDecl * |
| 2249 | IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 2250 | IdentifierInfo *Id, QualType T, NamedDecl **CH, |
| 2251 | unsigned CHS) { |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2252 | return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS); |
| 2253 | } |
| 2254 | |
Douglas Gregor | 8e7139c | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 2255 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 2256 | SourceLocation End = getLocation(); |
| 2257 | if (Init) |
| 2258 | End = Init->getLocEnd(); |
| 2259 | return SourceRange(getLocation(), End); |
| 2260 | } |
| 2261 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2262 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
| 2263 | SourceLocation L, IdentifierInfo *Id, |
| 2264 | TypeSourceInfo *TInfo) { |
| 2265 | return new (C) TypedefDecl(DC, L, Id, TInfo); |
| 2266 | } |
| 2267 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2268 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
| 2269 | SourceLocation L, |
| 2270 | StringLiteral *Str) { |
| 2271 | return new (C) FileScopeAsmDecl(DC, L, Str); |
| 2272 | } |