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