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