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