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